Expand my Community achievements bar.

SOLVED

Assets custom rendition in AEM

Avatar

Adobe Champion

I used the OOB functionality to create a custom rendition of 300*300 jpeg image as below with .jpeg extension.

 

P_V_Nair_0-1677680845738.png

 

P_V_Nair_1-1677680862352.png

And the rendition is being created for an image as below as expected.

P_V_Nair_2-1677680919614.png

The custom rendition is created as 300*300 with jpeg extension.

Can someone help me figure out how I can create this rendition with .jpg instead of .jpeg extension?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @P_V_Nair,

Unfortunately this is not an easy task. OOTB workflow steps are using com.day.cq.dam.api.renditions.RenditionMaker to generate renditions it use com.day.cq.dam.api.renditions.RenditionTemplate which defines params that will be used to generate rendition including mimeType and extension. MimeType and extension of rendition base on mimeType field you can set in workflow step arguments. However this will be mapped using list of MIME Types you can find under /system/console/mimetypes. You can register your own mime type and connect it with specific extension. There is one exception this will not work in case the mime type or extension is already registered. This is the case with jpg extension that is correlated with image/jpeg.

mime-types.jpg

Nevertheless, there are some programmatic options:

  • You can create custom workflow step and custom Rendition Template implementation that will handle jpg generation. This will be recommended approach.
  • Other option is a shortcut, workflows allows to use ecma script, you can use below script that will generate jpg rendition it hardcoded values - but it will give you an idea how to deal with it.
    var workflowData = graniteWorkItem.getWorkflowData();
    
    if (workflowData.getPayloadType() == "JCR_PATH") {
        var path = workflowData.getPayload().toString();
        var resourceResolver = graniteWorkflowSession.adaptTo(Packages.org.apache.sling.api.resource.ResourceResolver);
        var asset = resourceResolver.getResource(path).adaptTo(Packages.com.day.cq.dam.api.Asset);
    
        var renditionMaker = sling.getService(Packages.com.day.cq.dam.api.renditions.RenditionMaker);
        if (renditionMaker != null) {
            var template = renditionMaker.createWebRenditionTemplate(asset, 200, 200, 100, "image/jpg", null);
            var renditionName = "cq5dam.web." + 200 + "." + 200 + ".jpg";
    
            // using java reflection to set rendition name value in the Rendition Template
    		var field = template.getClass().getDeclaredField("renditionName");
            if (field != null) {
                field.setAccessible(true);
                field.set(template, renditionName);
                field.setAccessible(false);
    
                // generating rendition
                renditionMaker.generateRenditions(asset, template);
            }
        }
    }​
    Above code generates rendition with dimension 200px x 200px in image/jpg format and jpg extension

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @P_V_Nair,

Unfortunately this is not an easy task. OOTB workflow steps are using com.day.cq.dam.api.renditions.RenditionMaker to generate renditions it use com.day.cq.dam.api.renditions.RenditionTemplate which defines params that will be used to generate rendition including mimeType and extension. MimeType and extension of rendition base on mimeType field you can set in workflow step arguments. However this will be mapped using list of MIME Types you can find under /system/console/mimetypes. You can register your own mime type and connect it with specific extension. There is one exception this will not work in case the mime type or extension is already registered. This is the case with jpg extension that is correlated with image/jpeg.

mime-types.jpg

Nevertheless, there are some programmatic options:

  • You can create custom workflow step and custom Rendition Template implementation that will handle jpg generation. This will be recommended approach.
  • Other option is a shortcut, workflows allows to use ecma script, you can use below script that will generate jpg rendition it hardcoded values - but it will give you an idea how to deal with it.
    var workflowData = graniteWorkItem.getWorkflowData();
    
    if (workflowData.getPayloadType() == "JCR_PATH") {
        var path = workflowData.getPayload().toString();
        var resourceResolver = graniteWorkflowSession.adaptTo(Packages.org.apache.sling.api.resource.ResourceResolver);
        var asset = resourceResolver.getResource(path).adaptTo(Packages.com.day.cq.dam.api.Asset);
    
        var renditionMaker = sling.getService(Packages.com.day.cq.dam.api.renditions.RenditionMaker);
        if (renditionMaker != null) {
            var template = renditionMaker.createWebRenditionTemplate(asset, 200, 200, 100, "image/jpg", null);
            var renditionName = "cq5dam.web." + 200 + "." + 200 + ".jpg";
    
            // using java reflection to set rendition name value in the Rendition Template
    		var field = template.getClass().getDeclaredField("renditionName");
            if (field != null) {
                field.setAccessible(true);
                field.set(template, renditionName);
                field.setAccessible(false);
    
                // generating rendition
                renditionMaker.generateRenditions(asset, template);
            }
        }
    }​
    Above code generates rendition with dimension 200px x 200px in image/jpg format and jpg extension

Avatar

Adobe Champion

Thanks @lukasz-m  for this detailed explanation. I will work on this and see if this approach will work for me. But definitely gives an idea on how to approach it.

Avatar

Level 1

Does this do a simple resample and crop, or can it use "sensei" to detect a face for where to place the crop square and then resample that to a given pixel size? Automating the cropping of headshots to tight face shots for profile pix would be awesome.