Skip to content

Commit

Permalink
experimental: use size=<width>,height instead of width=<width> if bot…
Browse files Browse the repository at this point in the history
…h with and height are known
  • Loading branch information
stefanseifert committed Aug 23, 2024
1 parent 5229e4a commit ee6b0ff
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ else if (isVectorImage()) {
@Override
public @Nullable String getWebOptimizedImageDeliveryPath(DamContext damContext) {
return damContext.getWebOptimizedImageDeliveryUrl(new WebOptimizedImageDeliveryParams()
.width(getWidth()));
.width(getWidth()).height(getHeight()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Integer getRotation() {
@Override
public @Nullable String getWebOptimizedImageDeliveryPath(DamContext damContext) {
return damContext.getWebOptimizedImageDeliveryUrl(new WebOptimizedImageDeliveryParams()
.width(getWidth()).cropDimension(this.cropDimension).rotation(this.rotation));
.width(getWidth()).height(getHeight()).cropDimension(this.cropDimension).rotation(this.rotation));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ final class ParameterMap {

static final String PARAM_PREFER_WEBP = "preferwebp";
static final String PARAM_WIDTH = "width";
static final String PARAM_SIZE = "sz";
static final String PARAM_CROP = "c";
static final String PARAM_ROTATE = "r";
static final String PARAM_QUALITY = "quality";
Expand All @@ -72,6 +73,7 @@ static Map<String, Object> build(@NotNull Asset asset, @NotNull WebOptimizedImag
}

Long width = params.getWidth();
Long height = params.getHeight();
CropDimension cropDimension = params.getCropDimension();
Integer rotation = params.getRotation();
Integer quality = params.getQuality();
Expand All @@ -82,7 +84,10 @@ static Map<String, Object> build(@NotNull Asset asset, @NotNull WebOptimizedImag
map.put(PARAM_SEO_NAME, sanitizeSeoName(seoName));
map.put(PARAM_FORMAT, format);
map.put(PARAM_PREFER_WEBP, "true");
if (width != null) {
if (width != null && height != null) {
map.put(PARAM_SIZE, width.toString() + "," + height.toString());
}
else if (width != null) {
map.put(PARAM_WIDTH, width.toString());
}
if (cropDimension != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class WebOptimizedImageDeliveryParams {

private Long width;
private Long height;
private CropDimension cropDimension;
private Integer rotation;
private Integer quality;
Expand All @@ -50,6 +51,22 @@ public class WebOptimizedImageDeliveryParams {
return this;
}

/**
* @return Height
*/
public @Nullable Long getHeight() {
return this.height;
}

/**
* @param value Height
* @return this
*/
public @NotNull WebOptimizedImageDeliveryParams height(@Nullable Long value) {
this.height = value;
return this;
}

/**
* @return Crop dimension
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testAsset_JPEG_Original_WebOptimizedImageDeliveryDisabled() {
void testAsset_JPEG_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_Rescale(asset, 80, 40,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=85&width=80",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=85&sz=80%2C40",
ContentType.JPEG);
}

Expand All @@ -78,7 +78,7 @@ void testAsset_JPEG_Rescale() {
void testAsset_JPEG_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&sz=50%2C50",
ContentType.JPEG);
}

Expand All @@ -87,7 +87,7 @@ void testAsset_JPEG_AutoCrop() {
void testAsset_JPEG_AutoCrop_ImageQuality() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=60&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=60&sz=50%2C50",
ContentType.JPEG, 0.6d);
}

Expand All @@ -97,7 +97,7 @@ void testAsset_JPEG_CropWithExplicitRendition() {
Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG);
context.create().assetRendition(asset, "square.jpg", 50, 50, ContentType.JPEG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&sz=50%2C50",
ContentType.JPEG);
}

Expand All @@ -115,7 +115,7 @@ void testAsset_GIF_Original() {
void testAsset_GIF_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF);
buildAssertMedia_Rescale(asset, 80, 40,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.gif?preferwebp=true&quality=85&width=80",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.gif?preferwebp=true&quality=85&sz=80%2C40",
ContentType.GIF);
}

Expand All @@ -124,7 +124,7 @@ void testAsset_GIF_Rescale() {
void testAsset_GIF_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.gif?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.gif?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&sz=50%2C50",
ContentType.GIF);
}

Expand All @@ -142,7 +142,7 @@ void testAsset_PNG_Original() {
void testAsset_PNG_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG);
buildAssertMedia_Rescale(asset, 80, 40,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.png?preferwebp=true&quality=85&width=80",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.png?preferwebp=true&quality=85&sz=80%2C40",
ContentType.PNG);
}

Expand All @@ -151,7 +151,7 @@ void testAsset_PNG_Rescale() {
void testAsset_PNG_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.png?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.png?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&sz=50%2C50",
ContentType.PNG);
}

Expand All @@ -169,7 +169,7 @@ void testAsset_TIFF_Original() {
void testAsset_TIFF_Rescale() {
Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF);
buildAssertMedia_Rescale(asset, 80, 40,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=85&width=80",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=85&sz=80%2C40",
ContentType.JPEG);
}

Expand All @@ -178,7 +178,7 @@ void testAsset_TIFF_Rescale() {
void testAsset_TIFF_AutoCrop() {
Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF);
buildAssertMedia_AutoCrop(asset, 50, 50,
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&width=50",
"/adobe/dynamicmedia/deliver/" + getAssetId(asset) + "/sample.jpg?c=25.0p%2C0.0p%2C50.0p%2C100.0p&preferwebp=true&quality=85&sz=50%2C50",
ContentType.JPEG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ void testGetDeliveryUrl_relativeCropping() {
.cropDimension(new CropDimension(1028, 0, 806, 604))));
}

@Test
void testGetDeliveryUrl_relativeCropping_widthAndHeight() {
context.registerInjectActivateService(MockAssetDelivery.class);
WebOptimizedImageDeliveryService underTest = context.registerInjectActivateService(WebOptimizedImageDeliveryServiceImpl.class);
Asset asset = context.create().asset("/content/dam/Test_1.jpg", 1920, 604, ContentType.JPEG);
String assetId = MockAssetDelivery.getAssetId(asset);

assertEquals(WebOptimizedImageDeliveryCropOption.RELATIVE_PARAMETERS, underTest.getCropOption());

String cropping = URLEncoder.encode(RelativeCroppingString.create(0.535, 0, 0.42, 1), StandardCharsets.UTF_8);
assertEquals("/adobe/dynamicmedia/deliver/" + assetId + "/test-1.jpg?c=" + cropping + "&preferwebp=true&sz=806%2C604",
underTest.getDeliveryUrl(asset, new WebOptimizedImageDeliveryParams()
.width(806L)
.height(604L)
.cropDimension(new CropDimension(1028, 0, 806, 604))));
}

@Test
void testGetDeliveryUrl_absoluteCropping() {
context.registerInjectActivateService(MockAssetDelivery.class);
Expand Down

0 comments on commit ee6b0ff

Please sign in to comment.