diff --git a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilder.java b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilder.java index 185e38d7..dd5ca5fb 100644 --- a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilder.java +++ b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilder.java @@ -121,9 +121,9 @@ public NextGenDynamicMediaImageUrlBuilder(@NotNull NextGenDynamicMediaContext co SmartCrop namedSmartCrop = getMatchingNamedSmartCrop(metadata, cropSmartRatio); if (namedSmartCrop != null) { urlParamMap.put(PARAM_SMARTCROP, namedSmartCrop.getName()); - boolean widthAndOrHeightDefined = applyWidthOrPlaceholder(params, urlParamMap) || applyHeightOrPlaceholder(params, urlParamMap); - if (!widthAndOrHeightDefined) { - // if no width given apply default width/height to not rely on dimensions defined in AEM image profile + boolean widthOrHeightDefined = applyWidthOrPlaceholder(params, urlParamMap) || applyHeightOrPlaceholder(params, urlParamMap); + if (!widthOrHeightDefined) { + // if no width or height given apply default width/height to not rely on dimensions defined in AEM image profile String imageWidthHeightDefault = Long.toString(context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault()); if (namedSmartCrop.getCropDimension().getWidth() >= namedSmartCrop.getCropDimension().getHeight()) { urlParamMap.put(PARAM_WIDTH, imageWidthHeightDefault); @@ -144,8 +144,22 @@ else if (orginalDimension != null && cropSmartRatio != null && isAutoCroppingReq } else { // No cropping required or insufficient metadata available to detect cropping - applyWidthOrPlaceholder(params, urlParamMap); - applyHeightOrPlaceholder(params, urlParamMap); + boolean widthDefined = applyWidthOrPlaceholder(params, urlParamMap); + boolean heightDefined = applyHeightOrPlaceholder(params, urlParamMap); + if (!(widthDefined || heightDefined) && cropSmartRatio != null) { + // if no width or height given apply default width/height respecting the requested aspect ratio + double ratio = Ratio.get(cropSmartRatio); + long width = context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault(); + long height = context.getNextGenDynamicMediaConfig().getImageWidthHeightDefault(); + if (ratio > 1) { + height = Math.round(width / ratio); + } + else if (ratio < 1) { + width = Math.round(height * ratio); + } + urlParamMap.put(PARAM_WIDTH, Long.toString(width)); + urlParamMap.put(PARAM_HEIGHT, Long.toString(height)); + } } if (rotation != null && rotation != 0) { diff --git a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaLocalAssetTest.java b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaLocalAssetTest.java index a92d3209..5d7269a5 100644 --- a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaLocalAssetTest.java +++ b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaLocalAssetTest.java @@ -75,7 +75,7 @@ void testAsset_JPEG_Original_ContentDisposition() { void testAsset_JPEG_Rescale() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_Rescale(asset, 80, 40, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80", ContentType.JPEG); } @@ -84,7 +84,7 @@ void testAsset_JPEG_Rescale() { void testAsset_JPEG_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85", ContentType.JPEG); } @@ -93,7 +93,7 @@ void testAsset_JPEG_AutoCrop() { void testAsset_JPEG_AutoCrop_ImageQuality() { Asset asset = createSampleAsset("/filetype/sample.jpg", ContentType.JPEG); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=60", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=60", ContentType.JPEG, 0.6d); } @@ -103,7 +103,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, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85", ContentType.JPEG); } @@ -121,7 +121,7 @@ void testAsset_GIF_Original() { void testAsset_GIF_Rescale() { Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF); buildAssertMedia_Rescale(asset, 80, 40, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?height=40&preferwebp=true&quality=85&width=80", ContentType.JPEG); } @@ -130,7 +130,7 @@ void testAsset_GIF_Rescale() { void testAsset_GIF_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.gif", ContentType.GIF); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.gif?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85", ContentType.JPEG); } @@ -148,7 +148,7 @@ void testAsset_PNG_Original() { void testAsset_PNG_Rescale() { Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG); buildAssertMedia_Rescale(asset, 80, 40, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?height=40&preferwebp=true&quality=85&width=80", ContentType.PNG); } @@ -157,7 +157,7 @@ void testAsset_PNG_Rescale() { void testAsset_PNG_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.png", ContentType.PNG); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.png?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85", ContentType.PNG); } @@ -184,7 +184,7 @@ void testAsset_TIFF_Original_ContentDisposition() { void testAsset_TIFF_Rescale() { Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF); buildAssertMedia_Rescale(asset, 80, 40, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80", ContentType.JPEG); } @@ -193,7 +193,7 @@ void testAsset_TIFF_Rescale() { void testAsset_TIFF_AutoCrop() { Asset asset = createSampleAsset("/filetype/sample.tif", ContentType.TIFF); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://localrepo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/sample.jpg?crop=25%2C0%2C50%2C50&preferwebp=true&quality=85", ContentType.JPEG); } diff --git a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaTest.java b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaTest.java index c0f5e9ef..d66a6e08 100644 --- a/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaTest.java +++ b/src/test/java/io/wcm/handler/media/impl/MediaHandlerImplImageFileTypesEnd2EndNextGenDynamicMediaTest.java @@ -78,7 +78,7 @@ void testAsset_JPEG_Original_ContentDisposition() { void testAsset_JPEG_Rescale() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg"); buildAssertMedia_Rescale(asset, 80, 40, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80", ContentType.JPEG); } @@ -87,7 +87,7 @@ void testAsset_JPEG_Rescale() { void testAsset_JPEG_AutoCrop() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg"); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=85&width=2048", ContentType.JPEG); } @@ -96,7 +96,7 @@ void testAsset_JPEG_AutoCrop() { void testAsset_JPEG_AutoCrop_ImageQuality() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.jpg"); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=60", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=60&width=2048", ContentType.JPEG, 0.6d); } @@ -121,7 +121,7 @@ void testAsset_GIF_Original() { void testAsset_GIF_Rescale() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.gif"); buildAssertMedia_Rescale(asset, 80, 40, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?height=40&preferwebp=true&quality=85&width=80", ContentType.GIF); } @@ -130,7 +130,7 @@ void testAsset_GIF_Rescale() { void testAsset_GIF_AutoCrop() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.gif"); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.gif?height=2048&preferwebp=true&quality=85&width=2048", ContentType.GIF); } @@ -148,7 +148,7 @@ void testAsset_PNG_Original() { void testAsset_PNG_Rescale() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.png"); buildAssertMedia_Rescale(asset, 80, 40, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?height=40&preferwebp=true&quality=85&width=80", ContentType.PNG); } @@ -157,7 +157,7 @@ void testAsset_PNG_Rescale() { void testAsset_PNG_AutoCrop() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.png"); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.png?height=2048&preferwebp=true&quality=85&width=2048", ContentType.PNG); } @@ -184,7 +184,7 @@ void testAsset_TIFF_Original_ContentDisposition() { void testAsset_TIFF_Rescale() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.tif"); buildAssertMedia_Rescale(asset, 80, 40, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=80%3A40%2Csmart&preferwebp=true&quality=85&width=80", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=40&preferwebp=true&quality=85&width=80", ContentType.JPEG); } @@ -193,7 +193,7 @@ void testAsset_TIFF_Rescale() { void testAsset_TIFF_AutoCrop() { Asset asset = createNextGenDynamicMediaReferenceAsAsset("sample.tif"); buildAssertMedia_AutoCrop(asset, 50, 50, - "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?crop=1%3A1%2Csmart&preferwebp=true&quality=85", + "https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/sample.jpg?height=2048&preferwebp=true&quality=85&width=2048", ContentType.JPEG); } diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilderTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilderTest.java index 9c3ee428..dd60b5ba 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilderTest.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaImageUrlBuilderTest.java @@ -94,6 +94,45 @@ void testAllParams() { underTest.build(params)); } + @Test + void testOnlyRatio_16_9() { + NextGenDynamicMediaImageUrlBuilder underTest = getBuilder(); + NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams() + .cropSmartRatio(new Dimension(16, 9)) + .rotation(90) + .quality(60); + + assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg" + + "?height=1152&preferwebp=true&quality=60&rotate=90&width=2048", + underTest.build(params)); + } + + @Test + void testOnlyRatio_1_2() { + NextGenDynamicMediaImageUrlBuilder underTest = getBuilder(); + NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams() + .cropSmartRatio(new Dimension(1, 2)) + .rotation(90) + .quality(60); + + assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg" + + "?height=2048&preferwebp=true&quality=60&rotate=90&width=1024", + underTest.build(params)); + } + + @Test + void testOnlyRatio_1_1() { + NextGenDynamicMediaImageUrlBuilder underTest = getBuilder(); + NextGenDynamicMediaImageDeliveryParams params = new NextGenDynamicMediaImageDeliveryParams() + .cropSmartRatio(new Dimension(1, 1)) + .rotation(90) + .quality(60); + + assertEquals("https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image.jpg" + + "?height=2048&preferwebp=true&quality=60&rotate=90&width=2048", + underTest.build(params)); + } + @Test void testAllParams_NamedSmartCrop() throws Exception { NextGenDynamicMediaMetadata metadata = NextGenDynamicMediaMetadata.fromJson(METADATA_JSON_IMAGE);