Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 97 additions & 10 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Copy link
Copy Markdown
Member

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.0 as a plain format check.

A side effect: settings.php:113 still calls webp_uploads_mime_type_supported('image/avif') for the "neither format supported" gate, while the dropdown in webp_uploads_generate_avif_webp_setting_callback() now uses webp_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 in webp_uploads_get_upload_image_mime_transforms(), so the overload is a deliberate choice rather than an emergent one.

Copy link
Copy Markdown
Member

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's webp_uploads_mime_type_supported('image/avif'). The two are functionally equivalent:

  • Line 113: wp_image_editor_supports('image/avif') && webp_uploads_avif_transparency_supported()
  • Callback: webp_uploads_avif_supported() && webp_uploads_avif_transparency_supported()

(wp_image_editor_supports() is essentially _wp_image_editor_choose() !== false under 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_supported is 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.0 function now folds in transparency), so it's worth confirming that overload is intentional — but it does not produce divergent behavior.

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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.t note to webp_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.

}
Comment on lines 361 to 363

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.
*
Expand Down Expand Up @@ -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.
*

@adamsilverstein adamsilverstein Jun 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

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

Done in 703795d. Expanded the webp_uploads_imagick_avif_transparency_supported() docblock to explain that ImageMagick before 6.9.12-68 reports AVIF support but drops the alpha channel when encoding — so transparent images silently lose transparency — and that 6.9.12-68 is the version that fixed it (with a link to #2237). Also added an inline comment at the version_compare() check noting the same. Credited you as co-author.

* 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 );
Comment thread
westonruter marked this conversation as resolved.
Resolved
}
26 changes: 17 additions & 9 deletions plugins/webp-uploads/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand All @@ -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' ); ?>
Expand All @@ -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>
Expand Down
16 changes: 16 additions & 0 deletions plugins/webp-uploads/tests/data/class-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,20 @@ public function mock_frontend_body_hooks(): void {
remove_all_actions( 'template_redirect' );
do_action( 'template_redirect' );
}

/**
* Mocks AVIF transparency support to force a specific scenario.
*
* @param bool $supported Whether to mock AVIF transparency as supported.
*/
public function mock_avif_transparency_support( bool $supported ): void {
remove_all_filters( 'webp_uploads_imagick_avif_transparency_supported' );
add_filter(
'webp_uploads_imagick_avif_transparency_supported',
static function () use ( $supported ) {
return $supported;
},
1
);
}
}
69 changes: 69 additions & 0 deletions plugins/webp-uploads/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,33 @@ public function test_it_should_return_default_transforms_when_filter_returns_non
$this->assertSame( $default_transforms, $transforms );
}

/**
* Falls back to WebP output when AVIF is selected but AVIF transparency is not supported.
*
* @covers ::webp_uploads_get_upload_image_mime_transforms
*/
public function test_it_should_fall_back_to_webp_when_avif_transparency_not_supported(): void {
$editor = _wp_image_editor_choose( array( 'mime_type' => 'image/avif' ) );
if ( ! is_string( $editor ) || ! webp_uploads_imagick_avif_supported( $editor ) ) {
$this->markTestSkipped( 'Test requires WP_Image_Editor_Imagick.' );
}

$this->set_image_output_type( 'avif' );
$this->mock_avif_transparency_support( false );

$transforms = webp_uploads_get_upload_image_mime_transforms();

$this->assertSame(
array(
'image/jpeg' => array( 'image/webp' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/webp' ),
),
$transforms
);
}

/**
* Returns transforms array with fallback to original mime with invalid transforms array.
*/
Expand Down Expand Up @@ -715,4 +742,46 @@ public function data_provider_to_test_webp_uploads_sanitize_image_format(): arra
public function test_webp_uploads_sanitize_image_format( $input, string $expected ): void {
$this->assertSame( $expected, webp_uploads_sanitize_image_format( $input ) );
}

/**
* Data provider for ImageMagick version strings.
*
* @return array<string, array{string, bool}> Test data with version strings and expected support.
*/
public function data_provider_imagick_versions(): array {
return array(
'ImageMagick 6.8.9 Q16 x86_64' => array( 'ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28 https://imagemagick.org/index.php', false ),
'ImageMagick 6.9.11 Q16 x86_64' => array( 'ImageMagick 6.9.11-60 Q16 x86_64 2021-01-01 https://imagemagick.org', false ),
'ImageMagick 6.9.12 below minimum revision' => array( 'ImageMagick 6.9.12-27 Q16 x86_64 2021-10-24 https://imagemagick.org', false ),
'ImageMagick 6.9.12 just below minimum revision' => array( 'ImageMagick 6.9.12-67 Q16 x86_64 2025-06-01 https://imagemagick.org', false ),
'ImageMagick 6.9.12 exact minimum revision' => array( 'ImageMagick 6.9.12-68 Q16 x86_64 2025-06-04 https://imagemagick.org', true ),
'ImageMagick 6.9.13 above minimum revision' => array( 'ImageMagick 6.9.13-17 Q16 x86_64', true ),
'ImageMagick 7.0.24 above minimum major version' => array( 'ImageMagick 7.0.24 Q16 x86_64', true ),
'ImageMagick 7.0.25 above minimum major version' => array( 'ImageMagick 7.0.25 Q16 x86_64', true ),
'ImageMagick 7.1.0 Q16-HDRI x86_64' => array( 'ImageMagick 7.1.0-57 Q16-HDRI x86_64 d68553b17:20221230 https://imagemagick.org', true ),
'ImageMagick 7.1.1 Q16 aarch64' => array( 'ImageMagick 7.1.1-15 Q16 aarch64 98eceff6a:20230729 https://imagemagick.org', true ),
'ImageMagick 7.1.2 Q16-HDRI x86_64' => array( 'ImageMagick 7.1.2-7 Q16-HDRI x86_64 23405 https://imagemagick.org', true ),
'Empty string should return false' => array( '', false ),
'Invalid string without version should be false' => array( 'Invalid version string', false ),
'String with only text should be false' => array( 'ImageMagick', false ),
'Malformed version string should be false' => array( 'ImageMagick x.y.z', false ),
);
}

/**
* Tests webp_uploads_imagick_avif_transparency_supported checks version correctly.
*
* @dataProvider data_provider_imagick_versions
* @covers ::webp_uploads_imagick_avif_transparency_supported
*
* @param string $version ImageMagick version string.
* @param bool $expected_support Expected transparency support result.
*/
public function test_webp_uploads_imagick_avif_transparency_supported_checks_version( string $version, bool $expected_support ): void {
remove_all_filters( 'webp_uploads_imagick_avif_transparency_supported' );

$result = webp_uploads_imagick_avif_transparency_supported( $version );

$this->assertSame( $expected_support, $result );
}
}
31 changes: 30 additions & 1 deletion plugins/webp-uploads/tests/test-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package webp-uploads
*/

class Test_WebP_Uploads_Settings extends WP_UnitTestCase {
use WebP_Uploads\Tests\TestCase;

class Test_WebP_Uploads_Settings extends TestCase {

/**
* @covers ::webp_uploads_add_media_settings_fields
Expand Down Expand Up @@ -43,4 +45,31 @@ public function test_webp_uploads_add_settings_action_link(): void {
webp_uploads_add_settings_action_link( $default_action_links )
);
}

/**
* @covers ::webp_uploads_generate_avif_webp_setting_callback
*/
public function test_webp_uploads_generate_avif_webp_setting_callback_disables_avif_and_shows_warning(): void {
$editor = _wp_image_editor_choose( array( 'mime_type' => 'image/avif' ) );
if ( ! is_string( $editor ) || ! webp_uploads_imagick_avif_supported( $editor ) ) {
$this->markTestSkipped( 'Test requires WP_Image_Editor_Imagick.' );
}

$this->set_image_output_type( 'avif' );
$this->mock_avif_transparency_support( false );
$output = get_echo( 'webp_uploads_generate_avif_webp_setting_callback' );
$processor = new WP_HTML_Tag_Processor( $output );

$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'OPTION' ) ) );
$this->assertSame( 'webp', $processor->get_attribute( 'value' ) );
$this->assertNotNull( $processor->get_attribute( 'selected' ) );

$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'OPTION' ) ) );
$this->assertSame( 'avif', $processor->get_attribute( 'value' ) );
$this->assertNotNull( $processor->get_attribute( 'disabled' ) );
$this->assertNull( $processor->get_attribute( 'selected' ) );

$this->assertStringContainsString( 'AVIF is supported, but not fully: transparency support is lacking.', $output );
$this->assertStringContainsString( 'Current ImageMagick version does not support transparent AVIF images', $output );
}
}
Loading