From dff4abdb18f39f099e3a1e458f1637875d2f1f03 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Wed, 17 Jan 2024 14:55:56 +0100 Subject: [PATCH] web optimized image delivery --- .../mediasource/dam/impl/DamContext.java | 11 ++++++++ ...sEnd2EndWebOptimizedImageDeliveryTest.java | 28 +++++++++---------- .../dam/DamUriTemplateRenditionTest.java | 4 +-- .../mediasource/dam/DamUriTemplateTest.java | 4 +-- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/DamContext.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/DamContext.java index 94216f06..6c54f1d0 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/DamContext.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/DamContext.java @@ -41,6 +41,7 @@ import io.wcm.handler.mediasource.dam.impl.dynamicmedia.NamedDimension; import io.wcm.handler.mediasource.dam.impl.ngdm.WebOptimizedImageDeliveryParams; import io.wcm.handler.mediasource.dam.impl.ngdm.WebOptimizedImageDeliveryService; +import io.wcm.wcm.commons.contenttype.ContentType; /** * Context objects require in DAM support implementation. @@ -204,6 +205,16 @@ public boolean isWebOptimizedImageDeliveryEnabled() { * @return Delivery URL or null if not supported or not enabled */ public @Nullable String getWebOptimizedImageDeliveryUrl(@NotNull WebOptimizedImageDeliveryParams params) { + + // set image quality. + Double quality = this.mediaArgs.getImageQualityPercentage(); + if (quality == null) { + // use JPEG content type by default, because preferwebp is set by default and is a lossy compression, + // so we always need a quality value + quality = this.mediaHandlerConfig.getDefaultImageQuality(ContentType.JPEG); + } + params.quality((int)Math.round(quality * 100d)); + return webOptimizedImageDeliveryService.getDeliveryUrl(asset, params); } 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 db60c284..62fe3d38 100644 --- a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java +++ b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndWebOptimizedImageDeliveryTest.java @@ -49,7 +49,7 @@ void setUp() { void testAsset_JPEG_Original() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia(asset, 100, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=98", ContentType.JPEG); } @@ -67,7 +67,7 @@ void testAsset_JPEG_Original_WebOptimizedImageDeliveryDisabled() { void testAsset_JPEG_Rescale() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_Rescale(asset, 80, 40, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&width=80", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=98&width=80", ContentType.JPEG); } @@ -76,7 +76,7 @@ void testAsset_JPEG_Rescale() { void testAsset_JPEG_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_AutoCrop(asset, 50, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&quality=98&width=50", ContentType.JPEG); } @@ -85,7 +85,7 @@ void testAsset_JPEG_AutoCrop() { void testAsset_JPEG_AutoCrop_ImageQuality() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_AutoCrop(asset, 50, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&quality=60&width=50", ContentType.JPEG, 0.6d); } @@ -95,7 +95,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, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&quality=98&width=50", ContentType.JPEG); } @@ -104,7 +104,7 @@ void testAsset_JPEG_CropWithExplicitRendition() { void testAsset_GIF_Original() { Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF); buildAssertMedia(asset, 100, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.gif?preferwebp=true", + "/asset/delivery/" + getAssetId(asset) + "/sample.gif?preferwebp=true&quality=98", ContentType.GIF); } @@ -113,7 +113,7 @@ void testAsset_GIF_Original() { void testAsset_GIF_Rescale() { Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF); buildAssertMedia_Rescale(asset, 80, 40, - "/asset/delivery/" + getAssetId(asset) + "/sample.gif?preferwebp=true&width=80", + "/asset/delivery/" + getAssetId(asset) + "/sample.gif?preferwebp=true&quality=98&width=80", ContentType.GIF); } @@ -122,7 +122,7 @@ void testAsset_GIF_Rescale() { void testAsset_GIF_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF); buildAssertMedia_AutoCrop(asset, 50, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.gif?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.gif?c=25%2C0%2C50%2C50&preferwebp=true&quality=98&width=50", ContentType.GIF); } @@ -131,7 +131,7 @@ void testAsset_GIF_AutoCrop() { void testAsset_PNG_Original() { Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG); buildAssertMedia(asset, 100, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.png?preferwebp=true", + "/asset/delivery/" + getAssetId(asset) + "/sample.png?preferwebp=true&quality=98", ContentType.PNG); } @@ -140,7 +140,7 @@ void testAsset_PNG_Original() { void testAsset_PNG_Rescale() { Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG); buildAssertMedia_Rescale(asset, 80, 40, - "/asset/delivery/" + getAssetId(asset) + "/sample.png?preferwebp=true&width=80", + "/asset/delivery/" + getAssetId(asset) + "/sample.png?preferwebp=true&quality=98&width=80", ContentType.PNG); } @@ -149,7 +149,7 @@ void testAsset_PNG_Rescale() { void testAsset_PNG_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG); buildAssertMedia_AutoCrop(asset, 50, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.png?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.png?c=25%2C0%2C50%2C50&preferwebp=true&quality=98&width=50", ContentType.PNG); } @@ -158,7 +158,7 @@ void testAsset_PNG_AutoCrop() { void testAsset_TIFF_Original() { Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF); buildAssertMedia(asset, 100, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=98", ContentType.JPEG); } @@ -167,7 +167,7 @@ void testAsset_TIFF_Original() { void testAsset_TIFF_Rescale() { Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF); buildAssertMedia_Rescale(asset, 80, 40, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&width=80", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?preferwebp=true&quality=98&width=80", ContentType.JPEG); } @@ -176,7 +176,7 @@ void testAsset_TIFF_Rescale() { void testAsset_TIFF_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF); buildAssertMedia_AutoCrop(asset, 50, 50, - "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&width=50", + "/asset/delivery/" + getAssetId(asset) + "/sample.jpg?c=25%2C0%2C50%2C50&preferwebp=true&quality=98&width=50", ContentType.JPEG); } diff --git a/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateRenditionTest.java b/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateRenditionTest.java index 9bd85da9..ba3acb68 100644 --- a/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateRenditionTest.java +++ b/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateRenditionTest.java @@ -139,7 +139,7 @@ void testOriginal_WebOptimizedImageDelivery() { .build(); assertUriTemplate(media.getRendition(), SCALE_WIDTH, 192, 120, - "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&width={width}"); + "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&quality=98&width={width}"); assertUriTemplate(media.getRendition(), SCALE_HEIGHT, 192, 120, "/content/dam/folder1/sample.jpg/_jcr_content/renditions/original.image_file.0.{height}.file/sample.jpg"); } @@ -184,7 +184,7 @@ void test4_3_WebOptimizedImageDelivery() { .build(); assertUriTemplate(media.getRendition(), SCALE_WIDTH, 160, 120, - "/asset/delivery/" + assetId + "/sample.jpg?c=16%2C0%2C160%2C120&preferwebp=true&width={width}"); + "/asset/delivery/" + assetId + "/sample.jpg?c=16%2C0%2C160%2C120&preferwebp=true&quality=98&width={width}"); assertUriTemplate(media.getRendition(), SCALE_HEIGHT, 160, 120, "/content/dam/folder1/sample.jpg/_jcr_content/renditions/original.image_file.0.{height}.16,0,176,120.file/sample.jpg"); } diff --git a/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateTest.java b/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateTest.java index a2fdeb7c..92d49dc7 100644 --- a/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateTest.java +++ b/src/test/java/io/wcm/handler/mediasource/dam/DamUriTemplateTest.java @@ -111,9 +111,9 @@ void testGetUriTemplate_WebOptimizedImageDelivery() { Media media = mediaHandler.get(asset.getPath()).build(); assertUriTemplate(media, CROP_CENTER, 100, 50, - "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&width={width}"); + "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&quality=98&width={width}"); assertUriTemplate(media, SCALE_WIDTH, 100, 50, - "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&width={width}"); + "/asset/delivery/" + assetId + "/sample.jpg?preferwebp=true&quality=98&width={width}"); assertUriTemplate(media, SCALE_HEIGHT, 100, 50, "/content/dam/sample.jpg/_jcr_content/renditions/original.image_file.0.{height}.file/sample.jpg"); }