Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SITES-12764 - [Image] Support for relative crop coordinates #2518

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal.helper.image;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
Expand All @@ -32,12 +36,15 @@
import com.adobe.cq.wcm.spi.AssetDelivery;
import com.day.cq.commons.DownloadResource;
import com.day.cq.commons.ImageResource;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;

public class AssetDeliveryHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(AssetDeliveryHelper.class);

private static String COMMA = ",";
private static String PERCENTAGE = "p";
private static String WIDTH_PARAMETER = "width";
private static String QUALITY_PARAMETER = "quality";
private static String CROP_PARAMETER = "c";
Expand Down Expand Up @@ -104,6 +111,32 @@ public static String getSrc(@NotNull AssetDelivery assetDelivery, @NotNull Reso
return null;
}

// we have to get the with and height of the web rendition to calculate relative crop parameter
if (StringUtils.isNotEmpty(componentProperties.get(ImageResource.PN_IMAGE_CROP, String.class))) {
Asset asset = assetResource.adaptTo(Asset.class);
if (asset != null) {

Rendition assetRendition = asset.getRendition(asset1 -> {
for (Rendition rendition : asset1.getRenditions()) {
if (rendition.getName().startsWith("cq5dam.web")) {
return rendition;
}
}
return null;
});
if (assetRendition != null) {
try {
BufferedImage image = ImageIO.read(assetRendition.getStream());
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
params.put("imageHeight", imageHeight);
params.put("imageWidth", imageWidth);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
}
}
params.put(PATH_PARAMETER, assetPath);
params.put(SEO_PARAMETER, imageName);
params.put(FORMAT_PARAMETER, extension);
Expand Down Expand Up @@ -136,7 +169,7 @@ private static void addWidthParameter(@NotNull Map<String, Object> params, @NotN
}

private static void addCropParameter(@NotNull Map<String, Object> params, @NotNull ValueMap componentProperties) {
String cropParameter = getCropRect(componentProperties);
String cropParameter = getCropRect(componentProperties, params);
if (!StringUtils.isEmpty(cropParameter)) {
params.put(CROP_PARAMETER, cropParameter);
}
Expand All @@ -160,31 +193,49 @@ private static void addFlipParameter(@NotNull Map<String, Object> params, @NotNu
* Retrieves the cropping rectangle, if one is defined for the image.
*
* @param properties the image component's properties
* @param params image parameter
* @return the cropping parameters, if one is found, {@code null} otherwise
*/
private static String getCropRect(@NotNull ValueMap properties) {
private static String getCropRect(@NotNull ValueMap properties, Map<String, Object> params) {
String csv = properties.get(ImageResource.PN_IMAGE_CROP, String.class);
String cropRect = StringUtils.EMPTY;
if (StringUtils.isNotEmpty(csv)) {
try {
int ratio = csv.indexOf('/');
if (ratio >= 0) {
// skip ratio
csv = csv.substring(0, ratio);
}
int imageHeight = (int)params.getOrDefault("imageHeight", 0);
int imageWidth = (int)params.getOrDefault("imageWidth", 0);

String[] coords = csv.split(",");
int x1 = Integer.parseInt(coords[0]);
int y1 = Integer.parseInt(coords[1]);
int x2 = Integer.parseInt(coords[2]);
int y2 = Integer.parseInt(coords[3]);
int width = x2-x1;
int height = y2-y1;
return x1 + COMMA + y1 + COMMA + width + COMMA + height;
double x1 = Integer.parseInt(coords[0]);
double y1 = Integer.parseInt(coords[1]);
double x2 = Integer.parseInt(coords[2]);
double y2 = Integer.parseInt(coords[3]);
if (imageHeight > 0 && imageWidth > 0) {
double width = round( (x2 - x1) / imageWidth * 100);
double height = round((y2-y1) / imageHeight * 100);
x1 = round(( x1 / imageWidth * 100));
y1 = round( y1 / imageHeight * 100);
cropRect = x1 + PERCENTAGE + COMMA + y1 + PERCENTAGE + COMMA + width + PERCENTAGE + COMMA + height + PERCENTAGE;
}
else {
double width = round(x2-x1);
double height = round(y2-y1);
cropRect = x1 + COMMA + y1 + COMMA + width + COMMA + height;
}
} catch (RuntimeException e) {
LOGGER.warn(String.format("Invalid cropping rectangle %s.", csv), e);
}
}
return null;
return cropRect;
}

private static double round(double value) {
int scale = (int) Math.pow(10, 1);
return (double) Math.round(value * scale) / scale;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class AssetDeliveryHelperTest {

private static String TEST_ASSET_RESOURCE_PATH = "/content/dam/test";
private static String TEST_IMAGE_COMPONENT_PATH = "/content/test/image/resource";
private static final String PNG_IMAGE_BINARY_NAME = "Adobe_Systems_logo_and_wordmark.png";
private static String TEST_SEO_NAME = "test-seo";
private static String JPEG_EXTENSION = "jpg";
private static String WIDTH_PLACEHOLDER = "{width}";
Expand Down Expand Up @@ -125,7 +126,27 @@ public void testSrcWithCropParameter() throws Exception {
Resource imageComponentResource = context.create().resource(TEST_IMAGE_COMPONENT_PATH, imageResourceProperties);
String src = AssetDeliveryHelper.getSrc(assetDelivery, imageComponentResource, TEST_SEO_NAME, JPEG_EXTENSION, 200, JPEG_QUALITY);
String expectedSrcUrl = MockAssetDelivery.BASE_URL + TEST_ASSET_RESOURCE_PATH + "." + TEST_SEO_NAME + "." + JPEG_EXTENSION +
"?" + "width=" + 200 + "&" + "quality=" + JPEG_QUALITY + "&" + "c=" + 10 + "," + 20 + "," + 90 + "," + 180 + "&" + "preferwebp=true";
"?" + "width=" + 200 + "&" + "quality=" + JPEG_QUALITY + "&" + "c=" + 10.0 + "," + 20.0 + "," + 90.0 + "," + 180.0 + "&" +
"preferwebp=true";
assertEquals(expectedSrcUrl, src);
}

@Test
public void testSrcWithRelativeCropParameter() throws Exception {
String TEST_ASSET_RESOURCE_PATH_CROP = "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.jpg";
String TEST_SEO_NAME_CROP = "Adobe_Systems_logo_and_wordmark";
AssetDelivery assetDelivery = new MockAssetDelivery();
context.load().json("/image/test-content-dam.json", "/content/dam/core/images");
context.load().binaryFile("/image/" + "cq5dam.web.1280.1280_" + PNG_IMAGE_BINARY_NAME,
TEST_ASSET_RESOURCE_PATH_CROP + "/jcr:content/renditions/cq5dam.web.1280.1280.png");
Map<String, Object> imageResourceProperties = new HashMap<>();
imageResourceProperties.put(DownloadResource.PN_REFERENCE, TEST_ASSET_RESOURCE_PATH_CROP);
imageResourceProperties.put(ImageResource.PN_IMAGE_CROP, "10,20,100,200");
Resource imageComponentResource = context.create().resource(TEST_IMAGE_COMPONENT_PATH, imageResourceProperties);
String src = AssetDeliveryHelper.getSrc(assetDelivery, imageComponentResource, "Adobe_Systems_logo_and_wordmark", JPEG_EXTENSION, 200, JPEG_QUALITY);
String expectedSrcUrl = MockAssetDelivery.BASE_URL + TEST_ASSET_RESOURCE_PATH_CROP + "." + TEST_SEO_NAME_CROP + "." + JPEG_EXTENSION +
"?" + "width=" + 200 + "&" + "quality=" + JPEG_QUALITY + "&" + "c=" + 0.8 + "p," + 1.6 + "p," + 7.0 + "p," + 14.1 + "p&" +
"preferwebp=true";
assertEquals(expectedSrcUrl, src);
}

Expand Down Expand Up @@ -200,7 +221,7 @@ public void testSrcWithCropAndRotateAndFlipParameter() throws Exception {
String src = AssetDeliveryHelper.getSrc(assetDelivery, imageComponentResource, TEST_SEO_NAME, JPEG_EXTENSION, 200, JPEG_QUALITY);
String expectedSrcUrl = MockAssetDelivery.BASE_URL + TEST_ASSET_RESOURCE_PATH + "." + TEST_SEO_NAME + "." + JPEG_EXTENSION +
"?" + "width=" + 200 + "&" + "quality=" + JPEG_QUALITY +
"&" + "c=" + 10 + "," + 20 + "," + 90 + "," + 180 +
"&" + "c=" + 10.0 + "," + 20.0 + "," + 90.0 + "," + 180.0 +
"&" + "r=" + 90 +
"&" + "flip=HORIZONTAL_AND_VERTICAL" +
"&" + "preferwebp=true";
Expand Down