Search in sources :

Example 26 with Layer

use of com.day.image.Layer in project acs-aem-commons by Adobe-Consulting-Services.

the class AddWatermarkToRenditionProcess method getLayer.

private Layer getLayer(String path, WorkflowSession session) throws LoginException {
    if (path != null) {
        ResourceResolver resolver = workflowHelper.getResourceResolver(session);
        Resource resource = resolver.getResource(path);
        if (resource != null) {
            Layer layer = resource.adaptTo(Layer.class);
            if (layer != null) {
                return layer;
            } else {
                logInvalidWatermark(path);
            }
        } else {
            logMissingWatermark(path);
        }
    }
    return null;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Layer(com.day.image.Layer)

Example 27 with Layer

use of com.day.image.Layer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testHorizontalAndVerticalFlip.

private void testHorizontalAndVerticalFlip(Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair) throws IOException {
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] { "2000" });
    servlet.doGet(request, response);
    Assertions.assertEquals(HttpServletResponse.SC_OK, response.getStatus(), "Expected a 200 response.");
    Layer layer = new Layer(this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.png"));
    layer.flipHorizontally();
    layer.flipVertically();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    layer.write(StandardImageHandler.PNG1_MIMETYPE, 1.0, outputStream);
    Assertions.assertArrayEquals(outputStream.toByteArray(), response.getOutput());
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Layer(com.day.image.Layer)

Example 28 with Layer

use of com.day.image.Layer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServlet method transformAndStreamFile.

private void transformAndStreamFile(SlingHttpServletResponse response, ValueMap componentProperties, int resizeWidth, double quality, Resource imageFile, String imageType, String imageName) throws IOException {
    try (InputStream is = imageFile.adaptTo(InputStream.class)) {
        if ("gif".equalsIgnoreCase(mimeTypeService.getExtension(imageType)) || "svg".equalsIgnoreCase(mimeTypeService.getExtension(imageType))) {
            LOGGER.debug("GIF or SVG file detected; will render the original file.");
            if (is != null) {
                stream(response, is, imageType, imageName);
            }
            return;
        }
        int rotationAngle = getRotation(componentProperties);
        Rectangle rectangle = getCropRect(componentProperties);
        boolean flipHorizontally = componentProperties.get(Image.PN_FLIP_HORIZONTAL, Boolean.FALSE);
        boolean flipVertically = componentProperties.get(Image.PN_FLIP_VERTICAL, Boolean.FALSE);
        if (is != null) {
            if (rotationAngle != 0 || rectangle != null || resizeWidth > 0 || flipHorizontally || flipVertically) {
                Layer layer = new Layer(is);
                if (rectangle != null) {
                    layer.crop(rectangle);
                    LOGGER.debug("Applied cropping transformation.");
                }
                if (rotationAngle != 0) {
                    layer.rotate(rotationAngle);
                    LOGGER.debug("Applied rotation transformation ({} degrees).", rotationAngle);
                }
                if (flipHorizontally) {
                    layer.flipHorizontally();
                }
                if (flipVertically) {
                    layer.flipVertically();
                }
                if (layer.getBackground().getTransparency() != Transparency.OPAQUE && ("jpg".equalsIgnoreCase(mimeTypeService.getExtension(imageType)) || "jpeg".equalsIgnoreCase(mimeTypeService.getExtension(imageType)))) {
                    LOGGER.debug("Adding default (white) background to a transparent JPG: {}", imageFile.getPath());
                    layer.setBackground(Color.white);
                }
                resizeAndStreamLayer(response, layer, imageType, resizeWidth, quality);
            } else {
                LOGGER.debug("No need to perform any processing on file {}; rendering.", imageFile.getPath());
                stream(response, is, imageType, imageName);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Layer(com.day.image.Layer)

Example 29 with Layer

use of com.day.image.Layer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServlet method streamOrConvert.

/**
 * Try to stream the rendition if it matches the mime-type or convert it. If conversion fails, stream the rendition as is.
 *
 * @param response the {@link HttpServletResponse} to write the rendition to
 * @param rendition the rendition to stream
 * @param imageType the image mime type
 * @param imageName the image name
 * @param resizeWidth the width to resize the rendition to
 * @param quality the quality to use when converting the rendition
 * @throws IOException
 */
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Scanning generated code of try-with-resources")
private void streamOrConvert(@NotNull SlingHttpServletResponse response, @NotNull EnhancedRendition rendition, @NotNull String imageType, String imageName, int resizeWidth, double quality) throws IOException {
    Dimension dimension = rendition.getDimension();
    if (rendition.getMimeType().equals(imageType)) {
        LOGGER.debug("Found rendition {}/{} has a width of {}px and does not require a resize for requested width of {}px", rendition.getAsset().getPath(), rendition.getName(), dimension != null ? dimension.getWidth() : null, resizeWidth);
        try (InputStream is = rendition.getStream()) {
            if (is != null) {
                stream(response, is, imageType, imageName);
            }
        }
    } else {
        Layer layer = getLayer(rendition);
        if (layer == null) {
            LOGGER.warn("Found rendition {}/{} has a width of {}px and does not require a resize for requested width of {}px " + "but the rendition is not of the requested type {}, cannot convert so serving as is", rendition.getAsset().getPath(), rendition.getName(), dimension != null ? dimension.getWidth() : null, resizeWidth, imageType);
            try (InputStream is = rendition.getStream()) {
                if (is != null) {
                    stream(response, is, rendition.getMimeType(), imageName);
                }
            }
        } else {
            LOGGER.debug("Found rendition {}/{} has a width of {}px and does not require a resize for requested width of {}px " + "but the rendition is not of the requested type {}, need to convert", rendition.getAsset().getPath(), rendition.getName(), dimension != null ? dimension.getWidth() : null, resizeWidth, imageType);
            resizeAndStreamLayer(response, layer, imageType, 0, quality);
        }
    }
}
Also used : InputStream(java.io.InputStream) Layer(com.day.image.Layer) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Layer (com.day.image.Layer)29 ValueMap (org.apache.sling.api.resource.ValueMap)10 Test (org.junit.Test)10 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)9 InputStream (java.io.InputStream)5 Resource (org.apache.sling.api.resource.Resource)4 Rendition (com.day.cq.dam.api.Rendition)3 Color (java.awt.Color)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Asset (com.day.cq.dam.api.Asset)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)2 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)2 NamedImageTransformer (com.adobe.acs.commons.images.NamedImageTransformer)1 WorkflowHelper (com.adobe.acs.commons.util.WorkflowHelper)1 AbstractImageTest (com.adobe.cq.wcm.core.components.internal.models.v1.AbstractImageTest)1 DownloadResource (com.day.cq.commons.DownloadResource)1 ImageResource (com.day.cq.commons.ImageResource)1