Assets custom rendition in AEM | Community
Skip to main content
Adobe Champion
March 1, 2023
Solved

Assets custom rendition in AEM

  • March 1, 2023
  • 1 reply
  • 3327 views

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

 

 

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

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?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

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.

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

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
March 2, 2023

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.

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
P_V_NairAdobe ChampionAuthor
Adobe Champion
March 2, 2023

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.