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;
}
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());
}
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);
}
}
}
}
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);
}
}
}
Aggregations