-
Notifications
You must be signed in to change notification settings - Fork 128
Fixes palette-based PNG uploads failing original full-size AVIF/WebP conversion under GD #2024
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
4d54810
82cf154
cf97730
8ad675c
85482bc
ef9a9f9
7443e68
731d3b0
7f757c7
e646941
56e8dac
9ff40f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -803,3 +803,77 @@ | |
return $allowed_sizes; | ||
} | ||
add_filter( 'webp_uploads_image_sizes_with_additional_mime_type_support', 'webp_uploads_enable_additional_mime_type_support_for_all_sizes' ); | ||
|
||
/** | ||
* Converts palette PNG images to truecolor PNG images. | ||
* | ||
* GD cannot convert palette-based PNG to WebP/AVIF formats, causing conversion failures. | ||
* This function detects and converts palette PNG to truecolor during upload. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array<string, mixed>|mixed $file The uploaded file data. | ||
* @return array<string, mixed> The modified file data. | ||
*/ | ||
function webp_uploads_convert_palette_png_to_truecolor( $file ): array { | ||
if ( ! is_array( $file ) ) { | ||
$file = array(); | ||
} | ||
if ( ! isset( $file['tmp_name'], $file['name'] ) ) { | ||
return $file; | ||
} | ||
if ( isset( $file['type'] ) && is_string( $file['type'] ) ) { | ||
if ( 'image/png' !== strtolower( $file['type'] ) ) { | ||
return $file; | ||
} | ||
} elseif ( 'image/png' !== wp_check_filetype( $file['name'] )['type'] ) { | ||
return $file; | ||
} | ||
|
||
$editor = wp_get_image_editor( $file['tmp_name'] ); | ||
|
||
if ( is_wp_error( $editor ) || ! $editor instanceof WP_Image_Editor_GD ) { | ||
return $file; | ||
} | ||
|
||
// Check if required GD functions exist. | ||
if ( | ||
! function_exists( 'imagecreatefrompng' ) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to defensive programming; however all of these functions have been around since PHP4/5 as as far as I know should be available to all WordPress installs with GD. So maybe we can remove this check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would address the code coverage issue as well, if it is impossible to create a scenario where any of these functions don't exist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, but actually will these functions be available if GD is not installed? If PHP is not compiled with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
! function_exists( 'imageistruecolor' ) || | ||
! function_exists( 'imagealphablending' ) || | ||
! function_exists( 'imagesavealpha' ) || | ||
! function_exists( 'imagepalettetotruecolor' ) || | ||
! function_exists( 'imagepng' ) || | ||
! function_exists( 'imagedestroy' ) | ||
) { | ||
return $file; | ||
} | ||
|
||
$image = imagecreatefrompng( $file['tmp_name'] ); | ||
|
||
// Check if the image was created successfully. | ||
if ( false === $image ) { | ||
return $file; | ||
} | ||
|
||
// Check if the image is already truecolor. | ||
if ( imageistruecolor( $image ) ) { | ||
imagedestroy( $image ); | ||
return $file; | ||
} | ||
|
||
// Preserve transparency. | ||
imagealphablending( $image, false ); | ||
imagesavealpha( $image, true ); | ||
|
||
// Convert the palette to truecolor. | ||
if ( imagepalettetotruecolor( $image ) ) { | ||
// Overwrite the upload with the new truecolor PNG. | ||
imagepng( $image, $file['tmp_name'] ); | ||
} | ||
imagedestroy( $image ); | ||
|
||
return $file; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we set $file['name'] to the new file name before returning $file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're asking whether the conversion has changed the name of the file, it has not - we simply overwrote the same file with the converted version. However, if you're suggesting that users should see a different filename when an image is converted to truecolor, we could add a suffix like |
||
} | ||
add_filter( 'wp_handle_upload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're already using this in the test, but wouldn't it make sense to also add the same function as a hook callback to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it makes sense. At first, I thought There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 8ad675c |
||
add_filter( 'wp_handle_sideload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this fix belongs in core itself sice we inherently support this type of conversion with a simple filter. If we don't fix automatically, at the very least, we should inform the user why the upload failed and how to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I located the Trac ticket: #59339 "Conversion to webp causes fatal error when original image is a palette image (as opposed to truecolor)"
Should we post an update there and start the discussion on where to go next?