-
Notifications
You must be signed in to change notification settings - Fork 765
Issue Fix for #2958 Transparent backgrounds on PNG images lost in dynamic media Urls #2978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
362a5a5
701df18
653d1b1
e467bc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| import com.adobe.cq.wcm.core.components.models.ImageArea; | ||
| import com.day.cq.dam.api.Asset; | ||
| import com.day.cq.dam.api.DamConstants; | ||
| import com.day.cq.dam.commons.handler.StandardImageHandler; | ||
| import com.day.cq.dam.scene7.api.constants.Scene7AssetType; | ||
| import com.day.cq.dam.scene7.api.constants.Scene7Constants; | ||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
|
@@ -159,11 +160,12 @@ protected void initModel() { | |
| // if content policy delegate path is provided pass it to the image Uri | ||
| String policyDelegatePath = request.getParameter(CONTENT_POLICY_DELEGATE_PATH); | ||
| String dmImageUrl = null; | ||
| Asset asset = null; | ||
| if (StringUtils.isNotEmpty(fileReference)) { | ||
| // the image is coming from DAM | ||
| final Resource assetResource = request.getResourceResolver().getResource(fileReference); | ||
| if (assetResource != null) { | ||
| Asset asset = assetResource.adaptTo(Asset.class); | ||
| asset = assetResource.adaptTo(Asset.class); | ||
| if (asset != null) { | ||
| if (!uuidDisabled) { | ||
| uuid = asset.getID(); | ||
|
|
@@ -289,6 +291,18 @@ protected void initModel() { | |
| srcUriTemplate += imagePresetCommand; | ||
| src += imagePresetCommand; | ||
| } | ||
|
|
||
| // Auto-preserve PNG transparency if enabled and asset has transparency | ||
| if (shouldApplyPngAlphaModifier(asset)) { | ||
| if (StringUtils.isNotBlank(imageModifiers)) { | ||
| // Append to existing modifiers | ||
| imageModifiers += "&" + Image.PNG_ALPHA_FORMAT_MODIFIER; | ||
| } else { | ||
| // Set as the only modifier | ||
| imageModifiers = Image.PNG_ALPHA_FORMAT_MODIFIER; | ||
| } | ||
| } | ||
|
|
||
| if (StringUtils.isNotBlank(imageModifiers)){ | ||
| String imageModifiersCommand = (srcUriTemplate.contains("?") ? '&':'?') + imageModifiers; | ||
| srcUriTemplate += imageModifiersCommand; | ||
|
|
@@ -402,5 +416,47 @@ protected ImageArea newImageArea(String shape, String coordinates, String relati | |
| return new ImageAreaImpl(shape, coordinates, relativeCoordinates, link, alt); | ||
| } | ||
|
|
||
| /** | ||
| * Convenience method to determine if the PNG alpha format modifier should be applied. | ||
| * This evaluates both the configuration setting and asset transparency. | ||
| * | ||
| * @param asset The DAM asset to check | ||
| * @return true if the PNG alpha modifier should be applied, false otherwise | ||
| */ | ||
| protected boolean shouldApplyPngAlphaModifier(Asset asset) { | ||
| boolean autoPreservePngTransparency = currentStyle.get(Image.PN_DESIGN_AUTO_PRESERVE_PNG_TRANSPARENCY, false); | ||
| return autoPreservePngTransparency && hasPngTransparency(asset); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a PNG asset has transparency based on its bits per pixel metadata. | ||
| * | ||
| * @param asset The DAM asset to check | ||
| * @return true if the PNG has transparency (32 bits), false otherwise | ||
| */ | ||
| protected boolean hasPngTransparency(Asset asset) { | ||
| if (asset == null) { | ||
| return false; | ||
| } | ||
|
|
||
| String mimeType = asset.getMimeType(); | ||
| if (!StandardImageHandler.PNG1_MIMETYPE.equals(mimeType)) { | ||
| return false; | ||
| } | ||
|
|
||
| String bitsPerPixel = asset.getMetadataValue(Image.DAM_BITS_PER_PIXEL); | ||
| if (StringUtils.isEmpty(bitsPerPixel)) { | ||
| LOGGER.debug("No bits per pixel metadata found for PNG asset"); | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| int bits = Integer.parseInt(bitsPerPixel); | ||
| return bits == 32; // 32 bits indicates PNG with alpha channel | ||
| } catch (NumberFormatException e) { | ||
| LOGGER.debug("Could not parse bits per pixel value: {}", bitsPerPixel); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for not logging this as an error?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vladbailescu Improved! I've enhanced the logging approach:
This provides visibility for troubleshooting while avoiding unnecessary error-level entries for expected conditions. |
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for moving the variable definition here? Looks like it's not really used outside of the
assetResource != nullevaluation scope.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vladbailescu Good catch! The variable scope was actually appropriate in this case since the
assetvariable is used in multiple places within the broader method scope. However, I've optimized the PNG transparency logic by:assetvariable at the appropriate scope since it's used for multiple purposes (UUID, metadata extraction, transparency detection)The current scope provides the best balance between readability and proper variable lifecycle management.