Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#66262 new defect (bug)

GD image editor saves resized indexed PNGs as truecolor (regression since 3.5)

Reported by: abhishekfdd Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 3.5
Severity: normal Keywords: has-test-info has-patch has-unit-tests
Cc: Focuses:

Description

Summary

This is the GD follow-up to #36477 (see comment:62 and comment:63), where the Imagick side was fixed in [59589].

Since 3.5, the GD editor saves every resized sub-size of an indexed (color type 3) PNG as truecolor RGBA. Sub-sizes are often several times larger than the original.

In 3.4, image_resize() did this:

<?php
if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
        imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );

It checked the original image and converted the new one. [22094] moved this into WP_Image_Editor_GD::_save(), which checks the image being saved. _resize() always returns a truecolor image from wp_imagecreatetruecolor(), so for sub-sizes the check is always false and the conversion never runs.

Steps to reproduce

  1. add_filter( 'wp_image_editors', fn() => array( 'WP_Image_Editor_GD' ) );
  2. Upload tests/phpunit/data/images/png-tests/cloudflare-status.png.
  3. Check the sub-sizes' PNG color type: all are 6 (RGBA). Expected: 3 (indexed).

Results

Core's png-tests fixtures, trunk editor classes, PHP 8.3, libgd 2.3.3. Percentages are sub-size bytes relative to the original upload, averaged across generated sizes, with the worst single size in brackets.

Fixture Transparency Trunk Patched
cloudflare-status.png none 405% (707%) 110% (189%)
Palette_icon-or8.png none 195% (359%) 78% (140%)
test8.png binary 261% (413%) unchanged
deskcat8.png partial 226% (424%) unchanged

Patched output is visually indistinguishable from trunk (PSNR ~39 dB vs. the truecolor output).

Scope: opaque images only

imagetruecolortopalette() discards alpha on libgd 2.3.3, even for a minimal two-color image with imagealphablending( false ) and imagesavealpha( true ). Transparent regions become opaque. Preserving transparency would need a per-pixel loop in PHP, which isn't practical on large images. So the patch converts only indexed sources with no transparency, and leaves transparent ones as they are today.

Proposed patch

  • In load(), record whether the source is palette-based and whether it has any transparency (imagecolortransparent() index or non-zero alpha in the palette).
  • In _save() for image/png, when the source was an opaque palette image, convert a copy of the image to a 256-color palette and save that.
  • Convert a copy, not $image itself: converting in place makes a later WebP/AVIF save from the same editor fail (0-byte file), since GD can't write those from palette images (see performance#2018).
  • Unit tests: opaque indexed fixture stays indexed after resize; transparent indexed fixture keeps its alpha; PNG then WebP save from the same editor both succeed.

Related: #36477, #30402, #63448, #53408

Attachments (2)

66262.diff​ (6.9 KB ) - added by abhishekfdd 6 weeks ago.
66262.2.diff​ (6.8 KB ) - added by abhishekfdd 6 weeks ago.
66262.2.diff: is_opaque_palette_image() now checks $this->image instead of taking a parameter, which avoids a new PHPStan class.notFound error. Matches the PR.

Download all attachments as: .zip

Change History (4)

#1 @abhishekfdd
6 weeks ago

Attached 66262.diff.

  • Detects opaque indexed PNGs in load() and saves resized/edited copies as indexed PNGs in _save().
  • Converts a copy, so later WebP/AVIF saves from the same editor still get a truecolor image.
  • Leaves indexed PNGs with transparency as truecolor, since imagetruecolortopalette() drops alpha.
  • Removes the conversion added in [22094], which has had no effect since then: it only runs on palette images, where imagetruecolortopalette() does nothing.

Tests: the first fails on trunk and passes with the patch; the other two guard against the transparency and WebP failure modes.

This ticket was mentioned in ​PR #14064 on ​WordPress/wordpress-develop by ​@abhishekfdd.


6 weeks ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch removed

Summary

Since [22094] in 3.5, the GD image editor saves resized indexed (palette) PNGs as truecolor RGBA PNGs, which are considerably larger than the original. The palette check in _save() runs on the image after it has been resized, and resizing in GD always produces a truecolor image, so the check never matches.

This patch:

  • Detects opaque indexed PNGs in load(), before any editing, and stores the result in a new $is_opaque_indexed_png property.
  • In _save(), converts a copy of the truecolor image to a palette and saves that copy as the PNG. The editor's own image stays truecolor, so later WebP or AVIF saves from the same editor still work (GD can't write those formats from a palette image).
  • Leaves indexed PNGs that have transparency as truecolor, because imagetruecolortopalette() discards the alpha channel (details below).

Testing

New tests in tests/phpunit/tests/image/editorGd.php:

  • test_resize_opaque_indexed_png_saves_indexed_png: a resized opaque indexed PNG is saved with PNG color type 3 (indexed). This test fails on trunk (Failed asserting that 6 is identical to 3, i.e. truecolor RGBA).
  • test_resize_indexed_png_with_transparency_preserves_alpha: a resized indexed PNG with transparency keeps its transparent pixels.
  • test_resize_opaque_indexed_png_then_save_webp: saving a PNG and then a WebP from the same editor gives a valid WebP file. This test is skipped if GD has no WebP support.

Commands:

npm run test:php -- --group wp-image-editor-gd
npm run test:php -- --group image
npm run test:php -- --filter test_resize_opaque_indexed_png_saves_indexed_png

Before/after

Fixture Original Size Trunk (type, bytes) Patched (type, bytes)
cloudflare-status.png 24,940 300x300 (167x300) 6, 25,917 3, 7,822
cloudflare-status.png 24,940 1024x1024 (569x1024) 6, 176,260 3, 47,148
Palette_icon-or8.png 24,841 300x300 (300x300) 6, 12,699 3, 5,812
Palette_icon-or8.png 24,841 1024x1024 (1024x1024) 6, 55,090 3, 21,539
test8.png 45,231 300x300 (300x174) 6, 35,442 6, 35,442
test8.png 45,231 1024x1024 (1024x595) 6, 186,604 6, 186,604
deskcat8.png 105,705 300x300 (300x290) 6, 56,410 6, 56,410
deskcat8.png 105,705 1024x1024 (1024x990) 6, 264,297 6, 264,297

Fixtures from tests/phpunit/data/images/png-tests/, GD bundled (2.1.0 compatible), PHP 8.3.33.

test8.png and deskcat8.png are indexed but have transparency, so they are intentionally left as truecolor (see below).

Why indexed PNGs with transparency are left as truecolor

imagetruecolortopalette() discards the alpha channel. A minimal check, converting a 100x100 truecolor image that is half fully transparent red and half opaque blue:

{"red":4,"green":2,"blue":252,"alpha":0}
{"red":252,"green":2,"blue":4,"alpha":0}

The transparent half comes back fully opaque. This happens on both PHP's bundled GD ("2.1.0 compatible", PHP 8.3 in wp-env) and system libgd 2.3.3 (Ubuntu 24.04). Converting these images would replace transparent areas with solid color, so the patch only converts indexed PNGs with no transparency (no transparent color index and no palette entries with alpha). Indexed PNGs with transparency keep the current behavior.

The same check shows GD's quantizer can shift colors by a few levels (255,0,0 becomes 252,2,4). Measured against trunk's truecolor output for the opaque fixtures above, PSNR is between 38.6 and 40.7 dB.

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

Trac ticket:

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Opus 5.5
Used for: Used claude code to run tests

@abhishekfdd
6 weeks ago

66262.2.diff: is_opaque_palette_image() now checks $this->image instead of taking a parameter, which avoids a new PHPStan class.notFound error. Matches the PR.

Note: See TracTickets for help on using tickets.