Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>@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. } </code></pre> <p>sling.servlet.selectors would be the selector name you would want to set. For example:</p> <pre><code>//in the servlet @Property(name="sling.servlet.selectors", value="resize") //in the jsp image.setSelector("resize"); </code></pre> <p>Within your class, you would want to override the writeLayer method. Something like this:</p> <pre><code>@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(); } </code></pre> <p>In our custom solution we handled everything in the writeLayer method and not the createLayer method. So we overwrote createLayer.</p> <pre><code>@Override protected Layer createLayer(AbstractImageServlet.ImageContext c) throws RepositoryException, IOException { // don't create the layer yet. handle everything later return null; } </code></pre> <p>We also overwrote createImageResource</p> <pre><code>/** * {@inheritDoc} * * Override default ImageResource creation to support assets */ @Override protected ImageResource createImageResource(Resource resource) { return new Image(resource); } </code></pre> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload