-
Notifications
You must be signed in to change notification settings - Fork 146
Prevent transparency loss in AVIF by falling back to WebP on older ImageMagick #2245
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 1 commit
ae7f22d
a1aff66
be0817b
3fe6954
2247b61
1a58d1c
fdf65ec
50f2be1
f8af1e9
991d7c9
fece741
9020b24
435f5ff
4da1758
717e768
c033c44
b28885e
6004d18
d218d0f
41f94e1
28f2583
b421017
433ea1c
b00d2d0
28d6020
4cf8302
a28415f
99a2704
92f6528
23f528b
06f67f7
4e1ac64
fec8109
50e74ad
d8acc81
0e0ee09
23e12d5
1045006
789906c
8140089
2c50ed7
d01f2d6
315d6c3
3190c5b
f0beb09
346a61e
3f55c70
59d39dc
be178ec
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 |
|---|---|---|
|
|
@@ -966,3 +966,53 @@ function webp_uploads_convert_palette_png_to_truecolor( $file ): array { | |
| } | ||
| add_filter( 'wp_handle_upload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ); | ||
| add_filter( 'wp_handle_sideload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ); | ||
|
|
||
| /** | ||
| * Checks if an image has transparency when uploading AVIF images with Imagick. | ||
| * | ||
| * @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_check_image_transparency( $file ): array { | ||
| if ( 'avif' !== webp_uploads_get_image_output_format() || webp_uploads_imagick_avif_transparency_supported() ) { | ||
| return $file; | ||
| } | ||
|
|
||
| // Because plugins do bad things. | ||
| if ( ! is_array( $file ) ) { | ||
| $file = array(); | ||
| } | ||
| if ( ! isset( $file['tmp_name'], $file['name'] ) ) { | ||
| return $file; | ||
| } | ||
| if ( isset( $file['type'] ) && is_string( $file['type'] ) ) { | ||
| if ( ! str_starts_with( strtolower( $file['type'] ), 'image/' ) ) { | ||
| return $file; | ||
| } | ||
| } elseif ( ! str_starts_with( strtolower( (string) wp_check_filetype_and_ext( $file['tmp_name'], $file['name'] )['type'] ), 'image/' ) ) { | ||
| return $file; | ||
| } | ||
|
|
||
| $editor = wp_get_image_editor( $file['tmp_name'] ); | ||
|
|
||
| if ( is_wp_error( $editor ) || ! $editor instanceof WP_Image_Editor_Imagick ) { | ||
| return $file; | ||
| } | ||
|
|
||
| $reflection = new ReflectionClass( $editor ); | ||
| $image_property = $reflection->getProperty( 'image' ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $image_property->setAccessible( true ); | ||
| } | ||
| $imagick = $image_property->getValue( $editor ); | ||
|
||
|
|
||
| if ( $imagick instanceof Imagick ) { | ||
| wp_cache_set( 'webp_uploads_image_has_transparency', (bool) $imagick->getImageAlphaChannel(), 'webp-uploads' ); | ||
|
||
| } | ||
|
|
||
| return $file; | ||
| } | ||
| add_filter( 'wp_handle_upload_prefilter', 'webp_uploads_check_image_transparency' ); | ||
| add_filter( 'wp_handle_sideload_prefilter', 'webp_uploads_check_image_transparency' ); | ||
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.
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.
Updated regex to
/\d+(?:\.\d+)+(?:-\d+)?/for handling version string like this: