aem-core-wcm-components icon indicating copy to clipboard operation
aem-core-wcm-components copied to clipboard

Issue Fix for #2958 Transparent backgrounds on PNG images lost in dynamic media Urls

Open khanmaroof opened this issue 4 months ago • 2 comments

Q                       A
Fixed Issues? Fixes #2958
Patch: Bug Fix?
Minor: New Feature? Yes
Major: Breaking Change?
Tests Added + Pass? Yes
Documentation Provided Yes (markdown)
Any Dependency Changes?
License Apache License, Version 2.0

Conditional PNG Transparency Detection

Rather than applying fmt=png-alpha to all PNGs (which would cause significant performance issues), we can automatically detect which PNGs actually have transparency using existing DAM metadata.

Key Insight: dam:Bitsperpixel metadata reliably indicates PNG transparency:

  • 32 bits = PNG with alpha channel (transparent) → Add fmt=png-alpha
  • 24 bits = RGB PNG (opaque) → No format modifier needed
  • 8 bits = Indexed PNG (opaque) → No format modifier needed

Implementation: Modify the Dynamic Media URL building logic in ImageImpl.java to automatically prepend fmt=png-alpha only when dam:Bitsperpixel = 32 for PNG assets.

Benefits:

  • ✅ Preserves transparency automatically for transparent PNGs
  • ✅ Maintains optimal performance for non-transparent PNGs
  • ✅ Zero breaking changes - existing manual configurations still work
  • ✅ Can be policy-controlled via autoPreservePngTransparency setting

This approach solves the transparency issue while completely avoiding the performance penalty since fmt=png-alpha is only applied when transparency actually exists.

khanmaroof avatar Aug 30 '25 23:08 khanmaroof

Codecov Report

:x: Patch coverage is 9.09091% with 40 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../core/components/internal/models/v3/ImageImpl.java 4.34% 21 Missing and 1 partial :warning:
.../core/components/internal/models/v2/ImageImpl.java 14.28% 17 Missing and 1 partial :warning:

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Sep 01 '25 14:09 codecov[bot]