Skip to content

Original Full-Size AVIF/WEBP Conversion Fails on Palette-Based PNGs When GD is Used #2018

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
b1ink0 opened this issue May 26, 2025 · 2 comments · May be fixed by #2024
Open

Original Full-Size AVIF/WEBP Conversion Fails on Palette-Based PNGs When GD is Used #2018

b1ink0 opened this issue May 26, 2025 · 2 comments · May be fixed by #2024
Assignees
Labels
[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken

Comments

@b1ink0
Copy link
Contributor

b1ink0 commented May 26, 2025

Bug Description

When uploading a PNG that was compressed using https://imageresizer.com/image-compressor, I encountered following PHP warnings during AVIF and WebP conversion.

AVIF conversion warning:

Warning: imageavif(): avif error - avif doesn't support palette images in /var/www/html/wp-includes/class-wp-image-editor.php on line 585

WebP conversion warning:

 Warning: imagewebp(): Palette image not supported by webp in /var/www/html/wp-includes/class-wp-image-editor.php on line 585

Based on inspection of the PHP GD source:

Both imageavif() and imagewebp() functions fail when passed a palette-based image, because GD requires truecolor images for these conversions.

It appears these compression tools convert standard PNGs into palette-based (indexed color) PNGs. Due to this when uploading a palette-based (indexed color) PNG to WordPress media library, the original full-size AVIF/WebP file fails to convert and ends up as a 0-byte file.

However, resized sub-sizes are unaffected. Upon reviewing the GD editor code in WordPress core, I noticed that the _resize() method in WP_Image_Editor_GD always converts the source image to truecolor before any further processing hence, sub-sizes convert without issue.

References:

Steps to reproduce

  1. Force GD as the default image editor:
add_filter( 'wp_image_editors', function () {
  return [ 'WP_Image_Editor_GD' ];
} );
  1. Ensure the Modern Image Formats plugin is installed and activated.
  2. Upload a palette-based PNG image (created or optimized by an online compressor like imageresizer.com).

Sample palette PNG image:
Image
Sample transparent palette PNG image:
Image

Screenshots

AVIF conversion warning:

Image

WebP conversion warning:
Image

Additional Context

  • GD Version: bundled (2.1.0 compatible)
@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 26, 2025
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2025 May 26, 2025
@b1ink0
Copy link
Contributor Author

b1ink0 commented May 26, 2025

A potential solution to avoid conversion failure for original uploads is to hook into wp_handle_upload_prefilter and convert palette PNGs to truecolor before they are passed to GD’s conversion functions.

Example code snippet that fixes this issue:
add_filter( 'wp_handle_upload_prefilter', 'convert_palette_png_to_truecolor' );

function convert_palette_png_to_truecolor( $file ) {
	if ( ! isset( $file['tmp_name'], $file['name'] ) ) {
		return $file;
	}

	$ext = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );

	if ( 'png' !== $ext ) {
		return $file;
	}

	$editor = wp_get_image_editor( $file['tmp_name'] );

	if ( is_wp_error( $editor ) || ! $editor instanceof WP_Image_Editor_GD ) {
		return $file; // Skip conversion if not using GD.
	}

	$image = imagecreatefrompng( $file['tmp_name'] );
	if ( ! $image || imageistruecolor( $image ) ) {
		return $file;
	}

	// Preserve transparency.
	imagealphablending( $image, false );
	imagesavealpha( $image, true );

	// Convert the palette to truecolor.
	imagepalettetotruecolor( $image );

	// Overwrite the upload with the new truecolor PNG.
	imagepng( $image, $file['tmp_name'] );
	imagedestroy( $image );

	return $file;
}

cc: @adamsilverstein

@b1ink0 b1ink0 self-assigned this May 26, 2025
@mukeshpanchal27
Copy link
Member

Actually, looks like a possible duplicate of #1561

@b1ink0 b1ink0 changed the title AVIF/WEBP Conversion Fails on Palette-Based PNGs When GD is Used Original Full-Size AVIF/WEBP Conversion Fails on Palette-Based PNGs When GD is Used May 27, 2025
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
Status: Not Started/Backlog 📆
2 participants