From ee6b0ff5d2f9b09558456494de6ede573caf3327 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Fri, 23 Aug 2024 11:58:34 +0200 Subject: [PATCH] experimental: use size=,height instead of width= if both with and height are known --- .../dam/impl/VirtualRenditionMetadata.java | 2 +- .../VirtualTransformedRenditionMetadata.java | 2 +- .../dam/impl/weboptimized/ParameterMap.java | 7 ++++++- .../WebOptimizedImageDeliveryParams.java | 17 ++++++++++++++++ ...sEnd2EndWebOptimizedImageDeliveryTest.java | 20 +++++++++---------- ...OptimizedImageDeliveryServiceImplTest.java | 17 ++++++++++++++++ 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualRenditionMetadata.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualRenditionMetadata.java index ef991a13..6d39010c 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualRenditionMetadata.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualRenditionMetadata.java @@ -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 diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualTransformedRenditionMetadata.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualTransformedRenditionMetadata.java index 28d45ab5..d07157b2 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualTransformedRenditionMetadata.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/VirtualTransformedRenditionMetadata.java @@ -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 diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/ParameterMap.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/ParameterMap.java index 993bbd52..ac87a5a1 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/ParameterMap.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/ParameterMap.java @@ -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"; @@ -72,6 +73,7 @@ static Map 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(); @@ -82,7 +84,10 @@ static Map 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) { diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryParams.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryParams.java index 955b09b0..8f87df65 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryParams.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryParams.java @@ -30,6 +30,7 @@ public class WebOptimizedImageDeliveryParams { private Long width; + private Long height; private CropDimension cropDimension; private Integer rotation; private Integer quality; @@ -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 */ diff --git a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java index 33345eb8..e8bbb428 100644 --- a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java +++ b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/src/test/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryServiceImplTest.java b/src/test/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryServiceImplTest.java index 91fa4d78..75752ec9 100644 --- a/src/test/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryServiceImplTest.java +++ b/src/test/java/io/wcm/handler/mediasource/dam/impl/weboptimized/WebOptimizedImageDeliveryServiceImplTest.java @@ -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);