Skip to content

Conversation

@b1ink0
Copy link
Contributor

@b1ink0 b1ink0 commented May 27, 2025

Summary

Fixes #2018, #1561, #1864

Relevant technical choices

This change hooks into both wp_handle_upload_prefilter and wp_handle_sideload_prefilter to detect palette-based (indexed-color) PNGs and convert them in-place to truecolor while preserving transparency before any AVIF/WebP encoding takes place. It preserves all existing upload metadata and only affects PNGs handled by the GD image editor.

@b1ink0 b1ink0 added this to the webp-uploads n.e.x.t milestone May 27, 2025
@b1ink0 b1ink0 added [Type] Bug An existing feature is broken [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels May 27, 2025
@codecov
Copy link

codecov bot commented May 27, 2025

Codecov Report

❌ Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.61%. Comparing base (0b9d8f0) to head (15fd0ea).
⚠️ Report is 27 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/webp-uploads/hooks.php 85.18% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2024      +/-   ##
==========================================
+ Coverage   68.54%   68.61%   +0.06%     
==========================================
  Files          91       91              
  Lines        7930     7956      +26     
==========================================
+ Hits         5436     5459      +23     
- Misses       2494     2497       +3     
Flag Coverage Δ
multisite 68.61% <85.18%> (+0.06%) ⬆️
single 35.68% <0.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@b1ink0 b1ink0 marked this pull request as ready for review May 30, 2025 12:38
@github-actions
Copy link

github-actions bot commented May 30, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @IlyaZha, @mytory, @chimok.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: IlyaZha, mytory, chimok.

Co-authored-by: b1ink0 <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: benniledl <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b1ink0 Thank you, this looks great to me so far, just one comment.

It would be great to get @adamsilverstein's review on this as well.

@adamsilverstein
Copy link
Member

Hi @b1ink0 and thanks for the PR.

detect palette-based (indexed-color) PNGs and convert them in-place to truecolor while preserving transparency before any AVIF/WebP encoding takes place

Good catch! I understand the technical requirement for GD here and agree we need to address them to resolve this issue.

My only concern is that by converting a palette image (up to 8 bit) to truecolor (eg. 24 bit), we might inadvertently bloat the image size. We tried to address this for Imagick in https://core.trac.wordpress.org/ticket/36477. I'd love to see some before/after size numbers for indexed PNG uploads converted to WebP and AVIF to confirm that isn't the case. I suspect we are ok given the lossy compression of WebP/AVIF vs the lossless PNG compression, but it would be worth checking and perhaps adding a unit test for this aspect.

@adamsilverstein
Copy link
Member

I was able to verify the issue with the Bedrock container and some code to force using GD. I also verified the PR fixes the issue. This should be good to go.

We should probably port this fix to WordPress core since this will also affect anyone using the core output filter directly with the same configuration.

@adamsilverstein
Copy link
Member

@b1ink0 - this looks very close, thanks! I left a few additional comments, let me know what you think.

@b1ink0 b1ink0 requested a review from adamsilverstein July 24, 2025 07:12
Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice work!

@adamsilverstein
Copy link
Member

I opened a ticket to move this fix upstream to core since I expect it is a bug there: https://core.trac.wordpress.org/ticket/63773#ticket

@adamsilverstein
Copy link
Member

adamsilverstein commented Jul 31, 2025

@b1ink0 if you have a moment, can you please review my fix and test for core: WordPress/wordpress-develop#9368. The tests would benefit from being expanded to be more through, I wanted to capture the simplest case first to validate the test fails before and is fixed after the change.

@b1ink0
Copy link
Contributor Author

b1ink0 commented Jul 31, 2025

@adamsilverstein Yes, the patch looks good and is working all tests pass with your patch. When the logic for imagepalettetotruecolor is commented out, the test fails with the imagewebp(): Palette image not supported by webp message.

I think also adding a test to ensure the size of the converted WebP from truecolor is smaller than the palette PNG would be good, as you suggested in this PR.

Also tested Modern Image Formats version 2.5.1 installed through the WordPress plugin directory with the core patched there is no error when uploading a palette-based PNG.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple tiny suggestions.

@westonruter
Copy link
Member

Here's a build of the plugin in this branch's current state: webp-uploads.zip

@westonruter
Copy link
Member

Fix confirmed: #1576 (comment)

@westonruter westonruter merged commit 3d88f7b into WordPress:trunk Aug 19, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken

Projects

None yet

4 participants