Make WordPress Core

Opened 6 months ago

Last modified 6 months ago

#63773 assigned defect (bug)

Palette-based PNG cause failure attempting AVIF/WebP conversion under GD

Reported by: adamsilverstein's profile adamsilverstein Owned by: adamsilverstein's profile adamsilverstein
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.8
Component: Media Keywords: has-patch has-unit-tests dev-reviewed needs-testing
Focuses: Cc:

Description (last modified by adamsilverstein)

When uploading a palette based PNG image and attempting to use core's image_editor_output_format to output WebP or AVIF images, conversion fails with a PHP error when using GD. GD cannot convert palette-based PNG to WebP/AVIF formats so to fix the issue, a palette image is first converted to truecolor (imagepalettetotruecolor). Testing shows the PHP error is resolved and the resulting images are still significantly smaller than their PNG counterparts.

This issue was originally reported in the Performance Lab Modern Images plugin - https://github.com/WordPress/performance/issues/1561. The issue is fixed by this PR in the plugin: https://github.com/WordPress/performance/pull/2024. The purpose of this ticket is to bring that fix directly into core, into WP_Image_Editor_GD.

We can start by adding a unit test to demonstrate the issue.

Change History (6)

#1 @adamsilverstein
6 months ago

  • Description modified (diff)

This ticket was mentioned in PR #9368 on WordPress/wordpress-develop by @adamsilverstein.


6 months ago
#2

  • Keywords has-patch has-unit-tests added

#3 @adamsilverstein
6 months ago

I added a simple test to convert an indexed PNG to a WebP. As expected, it fails.

Test - https://github.com/WordPress/wordpress-develop/pull/9368/files#diff-551c6fb8796176aa11b26a07c677b2b56c97f02d496fc3028cde5c083730d52d

"Fatal error: Paletter image not supported by webp in /var/www/src/wp-includes/class-wp-image-editor.php on line 577" (https://github.com/WordPress/wordpress-develop/actions/runs/16655700347/job/47140087933?pr=9368)

Pushing the fix next...

#4 @adamsilverstein
6 months ago

  • Keywords dev-reviewed needs-testing added

Pushed the fix in https://github.com/WordPress/wordpress-develop/pull/9368/commits/73b5cb2a1a37e1d275668011fdd06a903d0f75c5, tests should now pass.

This is ready for review and testing.

Test instructions

  1. Add a filter to WordPress as an mu plugin:
// Convert PNG's uploads to WebP.
add_filter( 'image_editor_output_format', function( $format ) {
	return array(
		'image/png' => 'image/webp'
	);
} );
  1. Upload the sample dice image from the PR (or any palette based PNG image) to the media library.
  2. Check the generated files and PHP logs.

Expected

All files are generated as WebPs.
No errors in logs.

Actual (before fix)

Fill sized image is generated with zero file size and is not a valid image.
PHP error or warning in log.

#5 @adamsilverstein
6 months ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

#6 @b1ink0
6 months ago

I confirmed the fix by uploading a palette-based PNG and everything converted to WebP properly without any PHP errors. To double-check, I commented out the imagepalettetotruecolor logic and got the expected "Palette image not supported by webp" error, so the fix is working.

Also tested this patch with the Modern Image Formats 2.5.1 https://wordpress.org/plugins/webp-uploads/ plugin active as an additional test case and had no issues there either.

Note: See TracTickets for help on using tickets.