[wp-trac] [WordPress Trac] #63448: Image quality significantly degrades for resized PNGs with transparency in WordPress 6.8.1
WordPress Trac
noreply at wordpress.org
Fri May 23 20:27:33 UTC 2025
#63448: Image quality significantly degrades for resized PNGs with transparency in
WordPress 6.8.1
-------------------------------------------------+-------------------------
Reporter: elvismdev | Owner:
| adamsilverstein
Type: defect (bug) | Status: reopened
Priority: normal | Milestone: 6.8.2
Component: Media | Version: 6.8
Severity: critical | Resolution:
Keywords: has-patch has-test-info dev- | Focuses:
feedback has-unit-tests commit fixed-major |
-------------------------------------------------+-------------------------
Comment (by nosilver4u):
I think the disconnect here is ''when'' one checks the image colorspace. I
just tested on 22.04 with IM 6.9.11-60 as well and get
Imagick::COLORSPACE_GRAY after quantizing the image.
So the issue isn't that test8.png is grayscale, it is that IM thinks it
ought to ''make'' it grayscale after quantizing and reducing the number of
colors back to 9. The file size difference seems dramatic to me, though
that's a matter of perspective I suppose.
Here is the test script I wrote way back to illustrate this issue:
{{{#!php
<?php
$filename = 'test8.png';
$newname = 'test8-1140x662-grayscale.png';
$width = 1140;
$height = 662;
get_png_color_depth( $filename );
$image = new Imagick( $filename );
$image->resizeImage( $width, $height, Imagick::FILTER_TRIANGLE, 1 );
$image->setOption( 'png:compression-filter', '5' );
$image->setOption( 'png:compression-level', '9' );
$image->setOption( 'png:compression-strategy', '1' );
$image->setOption( 'png:include-chunk', 'tRNS' );
$current_colors = $image->getImageColors();
$max_colors = min( $current_colors + 8, 255 );
$image->quantizeImage( $max_colors, $image->getColorspace(), 0, false,
false );
if ( Imagick::COLORSPACE_GRAY === $image->getImageColorspace() ) {
echo "image changed to gray colorspace\n";
// $image->setOption( 'png:format', 'png8' );
}
$write_image_result = $image->writeImage( $newname );
if ( $write_image_result ) {
get_png_color_depth( $newname );
}
function get_png_color_depth( $filename ) {
if ( ! is_file( $filename ) ) {
return;
}
$size = filesize( $filename );
echo "size of $filename is $size\n";
$file_handle = fopen( $filename, 'rb' );
if ( ! $file_handle ) {
return;
}
$png_header = fread( $file_handle, 4 );
if ( chr( 0x89 ) . 'PNG' !== $png_header ) {
return;
}
// Move forward 8 bytes.
fread( $file_handle, 8 );
$png_ihdr = fread( $file_handle, 4 );
// Make sure we have an IHDR.
if ( 'IHDR' !== $png_ihdr ) {
return;
}
// Skip past the dimensions.
$dimensions = fread( $file_handle, 8 );
// Bit depth: 1 byte
// Bit depth is a single-byte integer giving the number of bits
per sample or
// per palette index (not per pixel).
//
// Valid values are 1, 2, 4, 8, and 16, although not all values
are allowed for all color types.
$pixel_depth = ord( (string) fread( $file_handle, 1 ) );
echo "pixel depth of $filename is $pixel_depth\n";
// Color type is a single-byte integer that describes the
interpretation of the image data.
// Color type codes represent sums of the following values:
// 1 (palette used), 2 (color used), and 4 (alpha channel used).
// The valid color types are:
// 0 => Grayscale
// 2 => Truecolor
// 3 => Indexed
// 4 => Greyscale with alpha
// 6 => Truecolour with alpha
$color_type = ord( (string) fread( $file_handle, 1 ) );
echo "color type of $filename is $color_type\n";
fclose( $file_handle );
}
}}}
Without the PNG8 fix, the resized image is 138kb. Otherwise, with the
proper fix to keep it indexed, test8-1140x662-grayscale.png is 32kb.
That's an image with 9 colors that increases by more than 4x.
Now, if this was another of my 'contrived' examples, I might be inclined
to agree that it is unnecessary. But this is one of a number of actual
images from a real site that was experiencing #36477. To revert the fix
for test8.png would be a regression, and seems silly to me when we already
have a fix.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63448#comment:66>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list