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?
Solved! Go to Solution.
Views
Replies
Total Likes
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:
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
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:
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
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.
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.
Views
Likes
Replies