Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Render image dynamically in CQ

Avatar

Level 9

Hi All,

In post http://cq5cms.blogspot.in/2014/07/renders-image-dynamically-in-cq5.html, the below mentioned code is written.

Is it something like

- This code will automatically create renditions of images, when they are uploaded in DAM.

- Also, renditions can also be created by running custom workflows on DAM images? Any references to this would be helpful.

Any thoughts on this will be helpful.

import java.awt.Dimension;
import java.io.IOException;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.commons.ImageHelper;
import com.day.image.Layer;

/**
 * Renders the image dynamically
 * Usage: /content/geometrixx/en/products/circle.thumbnail.500.500.jpg
 */
@Component
@Service
@org.apache.felix.scr.annotations.Properties({
      @org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value="sling/servlet/default"),
      @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"resize","thumb","thumbnail"}),
      @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"jpg", "png", "gif"}),
      @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value="GET")
})
public class CustomThumbnailServlet extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 1L;
 private final Logger log = LoggerFactory.getLogger(this.getClass());

 private final String ORIGINAL_PATH = "/jcr:content/renditions/original/jcr:content";
 private static final Dimension DEFAULT_DIMENSION = new Dimension(100, 100);
 
 @Override
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
try { 
  String[] selectors = request.getRequestPathInfo().getSelectors();
  String extension = request.getRequestPathInfo().getExtension();
  String imagePath = request.getRequestPathInfo().getResourcePath().substring(0, request.getRequestPathInfo().getResourcePath().indexOf("."));
  int maxHeight = selectors.length > 1 ? Integer.valueOf(selectors[1]).intValue() : (int)DEFAULT_DIMENSION.getHeight();
  int maxWidth = selectors.length > 2 ? Integer.valueOf(selectors[2]).intValue() : (int)DEFAULT_DIMENSION.getWidth();    
  boolean margin = (selectors.length > 3) && (selectors[3].equals("margin"));
 
  Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
  Node imageNode = session.getNode(imagePath+"."+extension+ORIGINAL_PATH);
 
  Property data = imageNode.getProperty("jcr:data");
      Layer imageLayer = ImageHelper.createLayer(imageNode.getSession(), imageNode.getPath());
  imageLayer.resize(maxWidth, maxHeight);
 
  response.setContentLength((int)data.getLength());
  response.setContentType(imageLayer.getMimeType());
  imageLayer.write(imageLayer.getMimeType(), imageLayer.getMimeType().equals("image/gif") ? 255 : 1.0, response.getOutputStream());
 
} catch (PathNotFoundException e) {
e.printStackTrace();
} catch (RepositoryException e) {
e.printStackTrace();
}  
  }
}

1 Accepted Solution

Avatar

Correct answer by
Employee
3 Replies

Avatar

Level 10

All they are doing in this servlet is using the com.day.image.Layer API to manipulate images that are uploaded.

https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/image/package-summary.html

WIth AEM - you have access to APIs that can dynamically re size and manipulate images, logos, etc, 

See this topic for more information about using these APIs:

http://docs.adobe.com/docs/en/aem/6-0/develop/the-basics/website.html#Creating the Logo Component

Avatar

Correct answer by
Employee

Details on running custom DAM workflows can be found here:https://docs.adobe.com/docs/en/aem/6-0/develop/extending/assets/media-handlers.html

 

For creating different renditions when uploading an asset, the following pages provide simple use cases:

http://www.albinsblog.com/2015/04/how-to-create-different-image-renditions-in-adobecq5.html#.VXCkZFx...

http://blogs.adobe.com/contentmanagement/tag/dam-update-asset/ 

Avatar

Level 9

Hi All,

Thanks a lot for your reply.