Repository navigation
Disable AVIF when ImageMagick does not support AVIF transparency #2540
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
Changes from all commits
253727c
d76f49b
f8eb6aa
92ce27f
b47fb8d
9095d39
c2011a9
e08e821
c69e471
cf889f5
db45281
25b8e10
be049ee
703795d
272a61f
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 |
|---|---|---|
|
|
@@ -347,6 +347,7 @@ function webp_uploads_should_discard_additional_image_file( array $original, arr | |
| * Includes special handling for false positives on AVIF support. | ||
| * | ||
| * @since 2.0.0 | ||
| * @since n.e.x.t AVIF is now only reported as supported when the server can also encode transparent AVIF images. | ||
| * | ||
| * @param string $mime_type The mime type to check. | ||
| * @return bool Whether the server supports a given mime type. | ||
|
|
@@ -358,21 +359,64 @@ function webp_uploads_mime_type_supported( string $mime_type ): bool { | |
|
|
||
| // In certain server environments Image editors can report a false positive for AVIF support. | ||
| if ( 'image/avif' === $mime_type ) { | ||
| $editor = _wp_image_editor_choose( array( 'mime_type' => 'image/avif' ) ); | ||
| if ( false === $editor ) { | ||
| return false; | ||
| } | ||
| if ( is_a( $editor, WP_Image_Editor_GD::class, true ) ) { | ||
| return function_exists( 'imageavif' ); | ||
| } | ||
| if ( is_a( $editor, WP_Image_Editor_Imagick::class, true ) && class_exists( 'Imagick' ) ) { | ||
| return 0 !== count( Imagick::queryFormats( 'AVIF' ) ); | ||
| } | ||
| return webp_uploads_avif_transparency_supported(); | ||
| } | ||
|
Comment on lines
361
to
363
Contributor
Author
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 c69e471. |
||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the server supports AVIF image format. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return bool True if the server supports AVIF, false otherwise. | ||
| */ | ||
| function webp_uploads_avif_supported(): bool { | ||
| $editor = _wp_image_editor_choose( array( 'mime_type' => 'image/avif' ) ); | ||
| if ( false === $editor ) { | ||
| return false; | ||
| } | ||
| if ( is_a( $editor, WP_Image_Editor_GD::class, true ) ) { | ||
| return function_exists( 'imageavif' ); | ||
| } | ||
| return webp_uploads_imagick_avif_supported( $editor ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the server supports transparent AVIF images. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return bool True if the server supports transparent AVIF images, false otherwise. | ||
| */ | ||
| function webp_uploads_avif_transparency_supported(): bool { | ||
| if ( ! webp_uploads_avif_supported() ) { | ||
| return false; | ||
| } | ||
| // AVIF is supported at this point. GD's imageavif() preserves alpha, so for GD (and any non-Imagick editor) AVIF support implies transparency support. The transparency regression is specific to older ImageMagick. | ||
| $editor = _wp_image_editor_choose( array( 'mime_type' => 'image/avif' ) ); | ||
| if ( is_string( $editor ) && is_a( $editor, WP_Image_Editor_Imagick::class, true ) ) { | ||
| return webp_uploads_imagick_avif_transparency_supported(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if Imagick supports AVIF format on the server. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $editor The image editor class name. | ||
| * @return bool True if Imagick supports AVIF, false otherwise. | ||
| */ | ||
| function webp_uploads_imagick_avif_supported( string $editor ): bool { | ||
| if ( is_a( $editor, WP_Image_Editor_Imagick::class, true ) && class_exists( 'Imagick' ) ) { | ||
| return 0 !== count( Imagick::queryFormats( 'AVIF' ) ); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get the image output format setting from the option. Default is avif. | ||
| * | ||
|
|
@@ -519,3 +563,46 @@ function webp_uploads_get_attachment_file_mime_type( int $attachment_id, string | |
| $mime_type = $filetype['type'] ?? get_post_mime_type( $attachment_id ); | ||
| return is_string( $mime_type ) ? $mime_type : ''; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if Imagick has AVIF transparency support. | ||
| * | ||
|
Member
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. add an inline comment explaining what this function does, why this check is added, namely that Imagick before 6.9.12-68 supports AVIF but not transparencies in AVIF
Member
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. 🤖 Claude (Opus 4.8), via @westonruter Done in 703795d. Expanded the |
||
| * ImageMagick versions prior to 6.9.12-68 report AVIF support (so AVIF images can be | ||
| * generated), but they do not preserve the alpha channel when encoding AVIF. As a result, | ||
| * transparent images converted to AVIF on those versions silently lose their transparency. | ||
| * Support for AVIF transparency was added in 6.9.12-68, so this gates on that minimum version. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @link https://github.com/WordPress/performance/issues/2237 | ||
| * | ||
| * @param string|null $version Optional Imagick version string. If not provided, the version will be retrieved from the Imagick class. | ||
| * @return bool True if Imagick has AVIF transparency support, false otherwise. | ||
| */ | ||
| function webp_uploads_imagick_avif_transparency_supported( ?string $version = null ): bool { | ||
| $imagick_version = $version; | ||
|
|
||
| if ( null === $imagick_version && extension_loaded( 'imagick' ) && class_exists( 'Imagick' ) ) { | ||
| $version_info = Imagick::getVersion(); | ||
| $imagick_version = is_array( $version_info ) && isset( $version_info['versionString'] ) && is_string( $version_info['versionString'] ) | ||
| ? $version_info['versionString'] | ||
| : ''; | ||
| } | ||
|
|
||
| if ( null === $imagick_version || '' === $imagick_version || ! (bool) preg_match( '/\d+(?:\.\d+)+(?:-\d+)?/', $imagick_version, $matches ) ) { | ||
| $supported = false; | ||
| } else { | ||
| // Transparency in AVIF is only handled correctly as of ImageMagick 6.9.12-68. | ||
| $imagick_version = $matches[0]; | ||
| $supported = version_compare( $imagick_version, '6.9.12-68', '>=' ); | ||
| } | ||
|
|
||
| /** | ||
| * Filters whether Imagick has AVIF transparency support. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param bool $supported Whether AVIF transparency is supported. | ||
| */ | ||
| return (bool) apply_filters( 'webp_uploads_imagick_avif_transparency_supported', $supported ); | ||
|
westonruter marked this conversation as resolved.
Resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,14 +153,16 @@ static function (): void { | |
| */ | ||
| function webp_uploads_generate_avif_webp_setting_callback(): void { | ||
|
|
||
| $selected = webp_uploads_get_image_output_format(); | ||
| $avif_supported = webp_uploads_mime_type_supported( 'image/avif' ); | ||
| $webp_supported = webp_uploads_mime_type_supported( 'image/webp' ); | ||
| $selected = webp_uploads_get_image_output_format(); | ||
| $avif_supported = webp_uploads_avif_supported(); | ||
| $avif_transparency_supported = webp_uploads_avif_transparency_supported(); | ||
| $avif_fully_supported = $avif_supported && $avif_transparency_supported; | ||
| $webp_supported = webp_uploads_mime_type_supported( 'image/webp' ); | ||
|
|
||
|
Comment on lines
+156
to
161
Contributor
Author
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 c69e471. |
||
| // If neither format is support, the entire field is not shown. | ||
| if ( ! $avif_supported && ! $webp_supported ) { | ||
| if ( ! $avif_fully_supported && ! $webp_supported ) { | ||
| ?> | ||
| <br /> | ||
| <br> | ||
| <div class="notice notice-warning inline"> | ||
| <p><b><?php esc_html_e( 'Modern image support is not available.', 'webp-uploads' ); ?></b></p> | ||
| <p><?php esc_html_e( 'WebP or AVIF support can only be enabled by your hosting provider, so contact them for more information.', 'webp-uploads' ); ?></p> | ||
|
|
@@ -170,15 +172,15 @@ function webp_uploads_generate_avif_webp_setting_callback(): void { | |
| } | ||
|
|
||
| // If only one of the two formats is supported, the dropdown defaults to that type and the other type is disabled. | ||
| if ( ! $avif_supported && 'avif' === $selected ) { | ||
| if ( ! $avif_fully_supported && 'avif' === $selected ) { | ||
| $selected = 'webp'; | ||
| } elseif ( ! $webp_supported && 'webp' === $selected ) { | ||
| $selected = 'avif'; | ||
| } | ||
| ?> | ||
| <select name="perflab_modern_image_format" id="perflab_modern_image_format" aria-describedby="perflab_modern_image_format_description"> | ||
| <option value="webp"<?php selected( 'webp', $selected ); ?><?php disabled( ! $webp_supported ); ?>><?php esc_html_e( 'WebP', 'webp-uploads' ); ?></option> | ||
| <option value="avif"<?php selected( 'avif', $selected ); ?><?php disabled( ! $avif_supported ); ?>><?php esc_html_e( 'AVIF', 'webp-uploads' ); ?></option> | ||
| <option value="avif"<?php selected( 'avif', $selected ); ?><?php disabled( ! $avif_fully_supported ); ?>><?php esc_html_e( 'AVIF', 'webp-uploads' ); ?></option> | ||
| </select> | ||
| <label for="perflab_modern_image_format"> | ||
| <?php esc_html_e( 'Generate images in this format', 'webp-uploads' ); ?> | ||
|
|
@@ -188,14 +190,20 @@ function webp_uploads_generate_avif_webp_setting_callback(): void { | |
| <?php esc_html_e( 'Generated images may be discarded if the file in the modern format is larger than the originally uploaded image.', 'webp-uploads' ); ?> | ||
| </p> | ||
| <?php if ( ! $avif_supported ) : ?> | ||
| <br /> | ||
| <br> | ||
| <div class="notice notice-warning inline"> | ||
| <p><b><?php esc_html_e( 'AVIF support is not available.', 'webp-uploads' ); ?></b></p> | ||
| <p><?php esc_html_e( 'AVIF support can only be enabled by your hosting provider, so contact them for more information.', 'webp-uploads' ); ?></p> | ||
| </div> | ||
| <?php elseif ( ! $avif_transparency_supported ) : ?> | ||
| <br> | ||
| <div class="notice notice-warning inline"> | ||
| <p><b><?php esc_html_e( 'AVIF is supported, but not fully: transparency support is lacking.', 'webp-uploads' ); ?></b></p> | ||
| <p><?php esc_html_e( 'Current ImageMagick version does not support transparent AVIF images, so contact your hosting provider for more information.', 'webp-uploads' ); ?></p> | ||
| </div> | ||
| <?php endif; ?> | ||
| <?php if ( ! $webp_supported ) : ?> | ||
| <br /> | ||
| <br> | ||
| <div class="notice notice-warning inline"> | ||
| <p><b><?php esc_html_e( 'WebP support is not available.', 'webp-uploads' ); ?></b></p> | ||
| <p><?php esc_html_e( 'WebP support can only be enabled by your hosting provider, so contact them for more information.', 'webp-uploads' ); ?></p> | ||
|
|
||
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.
🤖 Claude (Opus 4.8), via @westonruter
This silently changes the meaning of
webp_uploads_mime_type_supported('image/avif')from "AVIF format supported" to "AVIF and transparency supported", on a function documented@since 2.0.0as a plain format check.A side effect:
settings.php:113still callswebp_uploads_mime_type_supported('image/avif')for the "neither format supported" gate, while the dropdown inwebp_uploads_generate_avif_webp_setting_callback()now useswebp_uploads_avif_supported()(raw support). So two divergent notions of "AVIF supported" now live in the same file. In the (rare) case where AVIF works, transparency doesn't, and WebP is unsupported, line 113 hides the fallback settings field while the dropdown still renders AVIF + a transparency warning.Consider keeping
webp_uploads_mime_type_supported()purely about format support and handling the transparency gate explicitly inwebp_uploads_get_upload_image_mime_transforms(), so the overload is a deliberate choice rather than an emergent one.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.
🤖 Claude (Opus 4.8), via @westonruter
Correction to the above: I overstated this. The dropdown's disabled state and the "neither format supported" gate both key off
$avif_fully_supported($avif_supported && $avif_transparency_supported), which is transparency-aware — the same notion as line 113'swebp_uploads_mime_type_supported('image/avif'). The two are functionally equivalent:wp_image_editor_supports('image/avif') && webp_uploads_avif_transparency_supported()webp_uploads_avif_supported() && webp_uploads_avif_transparency_supported()(
wp_image_editor_supports()is essentially_wp_image_editor_choose() !== falseunder the hood, so the preconditions align too.)So the "rare inconsistency" I described — AVIF works, transparency doesn't, WebP unsupported → line 113 hides the field while the dropdown still renders AVIF — can't actually happen. In that case both evaluate to not-available and hide consistently. The raw
$avif_supportedis used only to choose which warning to show ("not available" vs "transparency lacking"), not to gate the dropdown or field visibility. I'm retracting the divergence claim.The only point that still stands is the narrower one:
webp_uploads_mime_type_supported('image/avif')had its meaning changed (a@since 2.0.0function now folds in transparency), so it's worth confirming that overload is intentional — but it does not produce divergent behavior.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 think its fine to change the meaning/behavior here, but it would be nice to add a single line to the readme release notes that specifies the new behavior
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.
🤖 Claude (Opus 4.8), via @westonruter
Documented in 272a61f. Rather than the readme (the function-level change felt better captured at the source), I added a
@since n.e.x.tnote towebp_uploads_mime_type_supported(): "AVIF is now only reported as supported when the server can also encode transparent AVIF images." Happy to also add a readme release-notes line if you'd prefer it there as well. Credited you as co-author.