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 2 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 @@ -103,7 +110,29 @@ public static String getSrc(@NotNull AssetDelivery assetDelivery, @NotNull Reso
if (assetResource == null) {
return null;
}

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 +165,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,9 +189,10 @@ 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);
if (StringUtils.isNotEmpty(csv)) {
try {
Expand All @@ -171,22 +201,39 @@ private static String getCropRect(@NotNull ValueMap properties) {
// 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) {
x1 = round(( x1 / imageHeight * 100));
x2 = round( x2 / imageHeight * 100);
y1 = round( y1 / imageWidth * 100);
y2 = round(y2 / imageWidth * 100);
}
double width = x2-x1;
double height = y2-y1;
if (imageHeight > 0 && imageWidth > 0) {
return x1 + PERCENTAGE + COMMA + y1 + PERCENTAGE + COMMA + width + PERCENTAGE + COMMA + height + PERCENTAGE;
} else {
return x1 + COMMA + y1 + COMMA + width + COMMA + height;
}
} catch (RuntimeException e) {
LOGGER.warn(String.format("Invalid cropping rectangle %s.", csv), e);
}
}
return null;
}

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

/**
* Retrieves the rotation angle for the image, if one is present. Typically this should be a value between 0 and 360.
*
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 @@ -129,6 +130,25 @@ public void testSrcWithCropParameter() throws Exception {
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.0 + "p&" +
"preferwebp=true";
assertEquals(expectedSrcUrl, src);
}

@Test
public void testSrcWithRotationParameter() throws Exception {
AssetDelivery assetDelivery = new MockAssetDelivery();
Expand Down