0

We have a requirement where we want to abort publishing and throw an alert if page has a image which is more than 200 KB.

I am trying below event system code :

[TcmExtension("PageEventHandlerExtension")]
public class PageEventHandler : TcmExtension
{
    public PageEventHandler()
    {
        Subscribe();
    }

    public void Subscribe()
    {
        EventSystem.Subscribe<Page, PublishOrUnPublishEventArgs>(PagePublishOrUnpublishAction, EventPhases.Initiated);
    }

    private static void PagePublishOrUnpublishAction(Page page, PublishOrUnPublishEventArgs args, EventPhases phase)
    {
        //Logic to fetch images from Page object and see the size of image is greater then 200KB
    }
}

is it possible to fetch all publishable images from Page Object ?

3
  • 1
    You may also wish to consider tackling this from the input side. There's some events system code that might be interesting here github.com/TridionPractice/tridion-practice/wiki/… Jan 28, 2020 at 9:15
  • Is this about a specific page or all images in a CMS environment? Jan 28, 2020 at 23:08
  • I am not sure about your requirement, but my suggestion implement an event system that will throw an error or alert at the time of the multimedia component creation. Jan 31, 2020 at 17:04

3 Answers 3

1

A page does usually not contain any direct reference to an image (it could, through page metadata).

A page does contain a list of component presentations, and a component presentation contains a component which may contain a field linking to an image.

So, what you could do is - go through all fields of the page metadata to check if a field is a multimedia field, and fetch it's image - go through all componentpresentations, and for each component, check if it contains a multimedia field and check the image (also don't forget the metadata).

Note that this process might take some time, so it might have impact on the time it takes to perform publishing (should be OK if you publish one or two pages per day, but it will have an impact if you publish thousands of pages per hour).

So, an alternative option might be to either do validation when creating a multimedia image (like Dominic already suggests in his comment), or to provide some code in your webapp to make sure images you serve are resized and compressed correctly.

This all depends on what your use case.

2

I would try to accomplish this at the template level, rather than event system. In order for Tridion to deploy an image (binary) it needs to call Engine.AddBinary(...) and if you are using your own templates you are doing that already for any binary you are publishing. If you are using other frameworks, such as DD4T or DXA, there's also a common place for this, where the templating code eventually calls the AddBinary method. I would implement your size check there. Let me know if you need code samples and if so, make sure you mention how are you currently publishing your pages/components.

Alternatively, you could also explore using a custom resolver, cause that's really the only place where you can "check" all the components being published. Then it is there where you do your due diligence and check in all involved items for images which are larger than 200KB, but I don't think you wanna kill flies with a shotgun...

2
  • Templating would be a good place to catch the implicit publishing of images, change the image sizes via code, or skip the images while still publishing items. However, I believe a custom resolver won't catch images unless they're dynamic themselves (Published with a "dynamic" Component Template). It is the template code that places image into delivery as you describe with AddBinary() and not the publishing resolver, in nearly most cases. Refer to my older post on this for the difference: createandbreak.net/2013/10/…. Jan 28, 2020 at 23:03
  • @AlvinReyes, you are right, images are not there per se, but you could check all the resolved components for their multimedia component link fields, etc... If you do it on a single page template, but the page triggers the publishing of another component or page where such logic isn't present, you could end up with components in the publishing transaction that were not evaluated by the original TBB which trigger the publishing transaction... Ideally all your templates should have a TBB that check for large images, was just pointing out where you could "look" :) Jan 30, 2020 at 13:19
2

You could create a custom renderer, register it as the last one in Tridion.ContentManager.config and let it check RenderedItem.Binaries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.