0

I am looking to resize image from the DAM, my image path is stored in pathfield which direclty links to DAM asset. Where image is "/content/dam/....jpg"; I have access to DAM path, I need to get this image resized and displayed using selector. I tried the code using the foundation image component, it didn't work for me.

 Resource rsc = resourceResolver.getResource(image);
                    Image img3 = new Image(rsc);

                    img3.setSrc(img3.getPath());
                    img3.setSelector(".myselector");
                    img3.setFileReference(image);

                    img3.draw(out);
user1130906
  • 101
  • 4
  • 16

1 Answers1

1

If you want to use a selector you will need to create a servlet for it that extends the AbstractImageServlet. You would start out with something like this:

@Component
@Service
@Properties({
      @Property(name="sling.servlet.resourceTypes", value="sling/servlet/default"),
      @Property(name="sling.servlet.selectors", value="resize"),
      @Property(name="sling.servlet.extensions", value={"jpg", "png", "gif"}),
      @Property(name="sling.servlet.methods", value="GET")
})

public class ImageResizeServlet extends AbstractImageServlet {
//... code.
}

sling.servlet.selectors would be the selector name you would want to set. For example:

//in the servlet
@Property(name="sling.servlet.selectors", value="resize")

//in the jsp
image.setSelector("resize");

Within your class, you would want to override the writeLayer method. Something like this:

@Override
protected void writeLayer(SlingHttpServletRequest req,
                          SlingHttpServletResponse resp,
                          AbstractImageServlet.ImageContext c, Layer layer)
        throws IOException, RepositoryException {
    // set new width and height    
    int width = 100;
    int height = 100;

    Image image = new Image(c.resource);
    if (!image.hasContent() || width == 0) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // get style and set constraints
    image.loadStyleData(c.style);

    // get pure layer
    layer = image.getLayer(true, true, true);

    Layer newLayer = ImageHelper.resize(layer, new Dimension(width, 0), null, null);
    if (newLayer != null) {
        layer = newLayer;
    }

    String mimeType = image.getMimeType();
    if (ImageHelper.getExtensionFromType(mimeType) == null) {
        // get default mime type
        mimeType = "image/png";
    }
    resp.setContentType(mimeType);

    layer.write(mimeType, mimeType.equals("image/gif") ? 255 : 1.0, resp.getOutputStream());

    resp.flushBuffer();
}

In our custom solution we handled everything in the writeLayer method and not the createLayer method. So we overwrote createLayer.

@Override
protected Layer createLayer(AbstractImageServlet.ImageContext c)
        throws RepositoryException, IOException {
    // don't create the layer yet. handle everything later
    return null;
}

We also overwrote createImageResource

/**
 * {@inheritDoc}
 *
 * Override default ImageResource creation to support assets
 */
@Override
protected ImageResource createImageResource(Resource resource) {
    return new Image(resource);
}

Hope that helps.

Woodifer
  • 1,019
  • 1
  • 7
  • 15
  • Hi Woodifier, Thanks for responding. I am getting resource as null in java class, it could be somethings that missed out. what are the things that need to be setup in jsp before calling the selector class. I appreciate if you let me know with some example. – user1130906 May 29 '13 at 02:38
  • Your code looked good for the jsp. You just need to change the selector to be the one you defined. I pulled the code above from a recent project I was working on. I added a few things above that should get it working properly. – Woodifer May 29 '13 at 15:28
  • Here is an additional example: http://balawcm.wordpress.com/2012/11/14/develop-a-custom-cq-selector/ This person only overwrites the createLayer method. – Woodifer May 29 '13 at 15:34
  • Just an update on what I found. It looks like from documentation, for a DAM link, the way that CQ5 expects to see the file reference is of the form /content/dam…cq5.thumbnail.390.300.relateditem.png The problem with this is that none of our selectors live in the DAM, so CQ cannot resolve them. The question is how to address this. – user1130906 May 29 '13 at 17:44
  • Are you just trying to get additional thumbnail options? – Woodifer May 29 '13 at 20:23