Skip to content

Commit

Permalink
add qlt parameter (configurable)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Aug 23, 2024
1 parent 26c8b32 commit 86f5b6a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<action type="update" dev="sseifert" issue="64">
Dynamic Media with OpenAPI: Do not enable support for remote assets by default. Since general availability the related configuration services not longer protected by a feature flag, so the feature has to be enabled explicitly via OSGi configuration.
</action>
<action type="update" dev="sseifert"><![CDATA[
Dynamic Media: Control image quality for lossy output image for each media request by appending "qlt" URL parameter, defaulting to the default image quality configured in the media handler configuration class.<br/>
<b>Breaking change:</b> You can disable this behavior by setting "Set Image Quality" to false in the "wcm.io Media Handler Dynamic Media Support" OSGi configuration. If disabled, the default image quality setting configured in Dynamic Media is used for all images.
]]></action>
<action type="update" dev="sseifert">
Eliminate dependency to Commons Lang 2.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ public boolean isDynamicMediaValidateSmartCropRenditionSizes() {
return dynamicMediaSupportService.isValidateSmartCropRenditionSizes();
}

/**
* @return Whether to control image quality for lossy output formats for each media request via 'qlt' URL parameter
* (instead of relying on default setting within Dynamic Media).
*/
public boolean isDynamicMediaSetImageQuality() {
return dynamicMediaSupportService.isSetImageQuality();
}

/**
* @return Dynamic media reply image size limit
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.wcm.handler.media.CropDimension;
import io.wcm.handler.media.Dimension;
import io.wcm.handler.media.format.Ratio;
import io.wcm.handler.media.impl.ImageQualityPercentage;
import io.wcm.handler.mediasource.dam.impl.DamContext;
import io.wcm.wcm.commons.contenttype.ContentType;

Expand Down Expand Up @@ -126,15 +127,8 @@ private DynamicMediaPath() {
logResult(damContext, "<too small for " + width + "x" + height + ">");
return null;
}
result.append("%3A").append(smartCropDef.getName()).append("?")
.append("wid=").append(dimension.getWidth()).append("&")
.append("hei=").append(dimension.getHeight()).append("&")
// cropping/width/height is pre-calculated to fit with original ratio, make sure there are no 1px background lines visible
.append("fit=stretch");
if (isPNG(damContext)) {
// if original image is PNG image, make sure alpha channel is preserved
result.append("&fmt=png-alpha");
}
result.append("%3A").append(smartCropDef.getName()).append("?");
appendWidthHeigtFormatQuality(result, dimension, damContext);
logResult(damContext, result);
return result.toString();
}
Expand All @@ -147,6 +141,12 @@ private DynamicMediaPath() {
if (rotation != null) {
result.append("rotate=").append(rotation).append("&");
}
appendWidthHeigtFormatQuality(result, dimension, damContext);
logResult(damContext, result);
return result.toString();
}

private static void appendWidthHeigtFormatQuality(@NotNull StringBuilder result, @NotNull Dimension dimension, @NotNull DamContext damContext) {
result.append("wid=").append(dimension.getWidth()).append("&")
.append("hei=").append(dimension.getHeight()).append("&")
// cropping/width/height is pre-calculated to fit with original ratio, make sure there are no 1px background lines visible
Expand All @@ -155,8 +155,10 @@ private DynamicMediaPath() {
// if original image is PNG image, make sure alpha channel is preserved
result.append("&fmt=png-alpha");
}
logResult(damContext, result);
return result.toString();
else if (damContext.isDynamicMediaSetImageQuality()) {
// it not PNG lossy format is used, apply image quality setting
result.append("&qlt=").append(ImageQualityPercentage.getAsInteger(damContext.getMediaArgs(), damContext.getMediaHandlerConfig()));
}
}

private static void logResult(@NotNull DamContext damContext, @NotNull CharSequence result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public interface DynamicMediaSupportService {
@NotNull
Dimension getImageSizeLimit();

/**
* @return Whether to control image quality for lossy output formats for each media request via 'qlt' URL parameter
* (instead of relying on default setting within Dynamic Media).
*/
boolean isSetImageQuality();

/**
* Get image profile.
* @param profilePath Full profile path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public class DynamicMediaSupportServiceImpl implements DynamicMediaSupportServic
description = "The configured height value for 'Reply Image Size Limit'.")
long imageSizeLimitHeight() default 2000;

@AttributeDefinition(
name = "Set Image Quality",
description = "Control image quality for lossy output formats for each media request via 'qlt' URL parameter (instead of relying on default setting within Dynamic Media).")
boolean setImageQuality() default false;

}

@Reference
Expand All @@ -118,6 +123,7 @@ public class DynamicMediaSupportServiceImpl implements DynamicMediaSupportServic
private boolean disableAemFallback;
private boolean validateSmartCropRenditionSizes;
private Dimension imageSizeLimit;
private boolean setImageQuality;

private static final String SERVICEUSER_SUBSERVICE = "dynamic-media-support";
private static final Pattern DAM_PATH_PATTERN = Pattern.compile("^/content/dam(/.*)?$");
Expand All @@ -132,6 +138,7 @@ private void activate(Config config) {
this.disableAemFallback = config.disableAemFallback();
this.validateSmartCropRenditionSizes = config.validateSmartCropRenditionSizes();
this.imageSizeLimit = new Dimension(config.imageSizeLimitWidth(), config.imageSizeLimitHeight());
this.setImageQuality = config.setImageQuality();

if (this.enabled) {
log.info("DynamicMediaSupport: enabled={}, capabilityEnabled={}, capabilityDetection={}, "
Expand Down Expand Up @@ -174,6 +181,11 @@ public boolean isValidateSmartCropRenditionSizes() {
return this.imageSizeLimit;
}

@Override
public boolean isSetImageQuality() {
return setImageQuality;
}

@Override
public @Nullable ImageProfile getImageProfile(@NotNull String profilePath) {
try (ResourceResolver resourceResolver = resourceResolverFactory
Expand Down

0 comments on commit 86f5b6a

Please sign in to comment.