Skip to content

Commit b7cd02d

Browse files
Merge pull request #1724 from WordPress/fix/1557-convert-webp-to-avif
Convert WebP to AVIF on upload
2 parents 6fbbaae + 9e47d74 commit b7cd02d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

plugins/webp-uploads/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
2828

2929
$default_transforms = array(
3030
'image/jpeg' => array( 'image/' . $output_format ),
31-
'image/webp' => array( 'image/webp' ),
31+
'image/webp' => array( 'image/' . $output_format ),
3232
'image/avif' => array( 'image/avif' ),
3333
'image/png' => array( 'image/' . $output_format ),
3434
);

plugins/webp-uploads/tests/test-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function test_it_should_return_default_transforms_when_filter_returns_non
365365
$this->set_image_output_type( 'avif' );
366366
$default_transforms = array(
367367
'image/jpeg' => array( 'image/avif' ),
368-
'image/webp' => array( 'image/webp' ),
368+
'image/webp' => array( 'image/avif' ),
369369
'image/avif' => array( 'image/avif' ),
370370
'image/png' => array( 'image/avif' ),
371371
);

plugins/webp-uploads/tests/test-load.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,4 +1087,36 @@ public function test_it_should_generate_fallback_images_for_all_sizes_when_gener
10871087

10881088
wp_delete_attachment( $attachment_id );
10891089
}
1090+
1091+
/**
1092+
* Convert WebP to AVIF on uploads.
1093+
*/
1094+
public function test_that_it_should_convert_webp_to_avif_on_upload(): void {
1095+
// Ensure the AVIF MIME type is supported; skip the test if not.
1096+
if ( ! webp_uploads_mime_type_supported( 'image/avif' ) ) {
1097+
$this->markTestSkipped( 'Mime type image/avif is not supported.' );
1098+
}
1099+
1100+
$this->set_image_output_type( 'avif' );
1101+
1102+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/balloons.webp' );
1103+
1104+
// There should be a AVIF source, but no WebP source for the full image.
1105+
$this->assertImageNotHasSource( $attachment_id, 'image/webp' );
1106+
$this->assertImageHasSource( $attachment_id, 'image/avif' );
1107+
1108+
$metadata = wp_get_attachment_metadata( $attachment_id );
1109+
1110+
// The full image should be a AVIF.
1111+
$this->assertArrayHasKey( 'file', $metadata );
1112+
$this->assertStringEndsWith( $metadata['sources']['image/avif']['file'], $metadata['file'] );
1113+
$this->assertStringEndsWith( $metadata['sources']['image/avif']['file'], get_attached_file( $attachment_id ) );
1114+
1115+
// There should be a AVIF source, but no WebP source for all sizes.
1116+
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
1117+
$this->assertImageNotHasSizeSource( $attachment_id, $size_name, 'image/webp' );
1118+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/avif' );
1119+
}
1120+
wp_delete_attachment( $attachment_id );
1121+
}
10901122
}

0 commit comments

Comments
 (0)