Skip to content

Check images for true alpha transparency #8933

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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

siliconforks
Copy link

Because ImageMagick cannot save images with true alpha transparency in indexed PNG format, we need to check whether an image has true alpha transparency before telling ImageMagick to use the png8 (indexed) format.

Trac ticket: https://core.trac.wordpress.org/ticket/63448


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Because ImageMagick cannot save images with true alpha transparency in
indexed PNG format, we need to check whether an image has true alpha
transparency before telling ImageMagick to use the png8 (indexed)
format.

See https://core.trac.wordpress.org/ticket/63448
Copy link

github-actions bot commented Jun 7, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props siliconforks, sirlouen.

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

Copy link

github-actions bot commented Jun 7, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

* We need to avoid forcing indexed format for images with true alpha transparency,
* because ImageMagick does not support saving an image with true alpha transparency as an indexed PNG.
*/
if ( Imagick::COLORSPACE_GRAY === $this->image->getImageColorspace() && ! $is_indexed_png_with_true_alpha_transparency ) {
Copy link

Choose a reason for hiding this comment

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

I would remove the colorspace_gray check and move this to L531 as the first check.

Like this:

/*
* We need to avoid forcing indexed format for images with true alpha transparency,
* because ImageMagick does not support saving an image with true alpha transparency as an indexed PNG.
*/
if ( ! $is_indexed_png_with_true_alpha_transparency ) {
    $this->image->setOption( 'png:format', 'png8' );
} else {
    $this->image->quantizeImage( 256, $this->image->getColorspace(), 0, false, false );
}

Copy link
Author

Choose a reason for hiding this comment

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

Well, if we did that, I think the code would probably work OK - but then there would be no indication of what the purpose of that branch actually is.

As far as I know, the reason that branch is there is specifically to work around an edge case with grayscale images. Otherwise I think there's no reason to have that branch at all? (I believe just calling quantizeImage would work for non-grayscale images.) So I would like to retain some indication of this, either in the code itself (so that setOption would be called only for grayscale images), or at least mention in a comment the reason for having that branch.

Copy link

Choose a reason for hiding this comment

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

Greyscale condition was only a supposition that setOption was the way to deal with them.

That was originally expected like that. But completely out of chance I found that the real right way to index any (both colour and greyscale) 1-bit alpha depth images was using the setOption

Quantization is just a way to play around with images with a true alpha transparency. I would simply comment this.

I'm not 100% confident yet, but given that the results in size are identical in 99% of the cases, I would say that setOption is some sort of quantization. As I said in my original post, the problem for me was that I could not see when png8 was viable because I could not identify the true alpha transparency. But now that there is a solution, quantization should be done as the last resort.

PS: I would add tests with all the alpha images we played with, checking if they preserve the alpha bits (theoretically with the current code they should fail because png8 is trimming them. With the mod I'm suggesting here, I've run the current 9 assertions and they are obviously working correctly as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants