Skip to content

Commit

Permalink
Merge pull request #2 from telegraph/AEM-7769-Fix-Image-Cropping-Issue
Browse files Browse the repository at this point in the history
Aem 7769 fix image cropping issue
  • Loading branch information
james-appleby authored Mar 23, 2018
2 parents 9737865 + 0f60a4b commit 669d1a3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.adobe.acs.commons.images;

/**
* Constants which are used in Crop functionality
*/
public final class CropConstants {

/**
* Parameter name for X coordinate, from which crop should be started
*/
public static final String CROP_START_X_PARAM = "cropStartX";
/**
* Parameter name for Y coordinate, from which crop should be started
*/
public static final String CROP_START_Y_PARAM = "cropStartY";
/**
* Parameter name for Crop width
*/
public static final String CROP_WIDTH_PARAM = "cropWidth";
/**
* Parameter name for Crop height
*/
public static final String CROP_HEIGHT_PARAM = "cropHeight";
/**
* Separator which is used in URL to split parameter name and parameter value
*/
public static final String PARAM_SEPARATOR = ":";

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.adobe.acs.commons.images.impl;

import com.adobe.acs.commons.dam.RenditionPatternPicker;
import com.adobe.acs.commons.images.CropConstants;
import com.adobe.acs.commons.images.ImageTransformer;
import com.adobe.acs.commons.images.NamedImageTransformer;
import com.adobe.acs.commons.util.PathInfoUtil;
Expand Down Expand Up @@ -147,6 +148,8 @@ public class NamedTransformImageServlet extends SlingSafeMethodsServlet implemen

private static final String TYPE_PROGRESSIVE = "progressive";

private static final String PROP_ADD_URL_PARAMETERS = "addUrlParams";

private Pattern lastSuffixPattern = Pattern.compile(DEFAULT_FILENAME_PATTERN);

private Map<String, NamedImageTransformer> namedImageTransformers =
Expand Down Expand Up @@ -227,7 +230,7 @@ protected final void doGet(final SlingHttpServletRequest request, final SlingHtt
}

// Transform the image
layer = this.transform(layer, imageTransformersWithParams);
layer = this.transform(layer, imageTransformersWithParams, request);

// Get the quality
final double quality = this.getQuality(mimeType,
Expand All @@ -253,9 +256,11 @@ protected final void doGet(final SlingHttpServletRequest request, final SlingHtt
*
* @param layer the Image layer
* @param imageTransformersWithParams the transforms and their params
* @param request the SlingHttpServletRequest
* @return the transformed Image layer
*/
protected final Layer transform(Layer layer, final ValueMap imageTransformersWithParams) {
protected final Layer transform(Layer layer, final ValueMap imageTransformersWithParams,
SlingHttpServletRequest request) {

for (final String type : imageTransformersWithParams.keySet()) {
if (StringUtils.equals(TYPE_QUALITY, type)) {
Expand All @@ -269,9 +274,16 @@ protected final Layer transform(Layer layer, final ValueMap imageTransformersWit
continue;
}

final ValueMap transformParams = imageTransformersWithParams.get(type, EMPTY_PARAMS);
ValueMap transformParams = imageTransformersWithParams.get(type, EMPTY_PARAMS);

if (transformParams != null) {
if (Boolean.valueOf(transformParams.get(PROP_ADD_URL_PARAMETERS, false))) {
LinkedHashMap<String, Object> cropParamsFromUrl = getCropParamsFromUrl(request);
if(!cropParamsFromUrl.isEmpty()) {
transformParams = new ValueMapDecorator(new LinkedHashMap<String, Object>(transformParams));
transformParams.putAll(cropParamsFromUrl);
}
}
layer = imageTransformer.transform(layer, transformParams);
}
}
Expand Down Expand Up @@ -472,6 +484,21 @@ private String getMimeType(final SlingHttpServletRequest request, final Image im
}
}

private LinkedHashMap<String, Object> getCropParamsFromUrl(SlingHttpServletRequest request) {
LinkedHashMap<String, Object> urlParams = new LinkedHashMap<String, Object>();

String transformName = PathInfoUtil.getFirstSuffixSegment(request);
String extension = PathInfoUtil.getLastSuffixSegment(request);

String paramsString = StringUtils.substringBetween(request.getRequestURI(), transformName + "/", extension);
String[] params = StringUtils.split(paramsString, "/");
for (String param : params) {
urlParams.put(StringUtils.substringBefore(param, ":"),
StringUtils.substringAfter(param, CropConstants.PARAM_SEPARATOR));
}
return urlParams;
}

/**
* Gets the Image layer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.awt.*;
import java.util.*;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -78,6 +80,12 @@ public class NamedTransformImageServletTest {

private MockSlingHttpServletRequest mockRequest;

@Mock
private Layer layer;

@Mock
private Rectangle rectangle;

@Mock
private Resource mockImageResource;

Expand Down Expand Up @@ -184,7 +192,7 @@ public void test_multipleTransforms() throws Exception {

final ValueMap imageTransformersWithParams = servlet.getImageTransformersWithParams(selectedNamedImageTransformers);

servlet.transform(mock(Layer.class), imageTransformersWithParams);
servlet.transform(mock(Layer.class), imageTransformersWithParams, mockRequest);

org.mockito.Mockito.verify(resizeImageTransformer, times(1)).transform(any(Layer.class), any(ValueMap.class));
org.mockito.Mockito.verify(greyscaleImageTransformer, times(1)).transform(any(Layer.class), any(ValueMap.class));
Expand Down

0 comments on commit 669d1a3

Please sign in to comment.