Media: Generate WebP only for certain registered image sizes.

The existing filter `image_editor_output_format` receives an additional parameter `$size_name` which is populated whenever it controls the output format for a specific registered image size to create. Otherwise, it remains empty. In order to achieve this, a low level change has been added in bringing a new `$size_name` class property to the `WP_Image_Editor` base class, which is introduced in a backward compatible way that will not cause conflicts with custom implementations.

This parameter is then used in new logic inside the `wp_default_image_output_mapping()` callback function for the filter, controlling whether `image/jpeg` should map to `image/webp` output or not. By default, this is enabled for all WordPress core image sizes by default, and this list can be modified using a new `wp_image_sizes_with_additional_mime_type_support` filter, e.g. to remove core sizes or add custom sizes.

The customization per image size may be further enhanced by providing a more declarative API via a new parameter on the `add_image_size()` function.

Props eugenemanuilov, flixos90, adamsilverstein, joegrainger.

Fixes #56526.
See #55443, #56288.


git-svn-id: https://develop.svn.wordpress.org/trunk@54097 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2022-09-07 21:43:28 +00:00
parent d51e65b78f
commit d119ff711d
8 changed files with 177 additions and 25 deletions

View File

@@ -3626,7 +3626,7 @@ EOF;
* @ticket 55443
*/
public function test_wp_default_image_output_mapping() {
$mapping = wp_default_image_output_mapping( array() );
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
}
@@ -3637,7 +3637,7 @@ EOF;
*/
public function test_wp_default_image_output_mapping_existing() {
$mapping = array( 'mime/png' => 'mime/webp' );
$mapping = wp_default_image_output_mapping( $mapping );
$mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
$this->assertSame(
array(
'mime/png' => 'mime/webp',
@@ -3672,6 +3672,78 @@ EOF;
$this->assertSame( 'canola-100x75.jpg', $saved['file'] );
}
}
/**
* @ticket 56526
* @dataProvider data_wp_default_image_output_mapping_size_filter
*/
public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
if ( $filter_callback ) {
add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
}
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
if ( $expects_webp ) {
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
} else {
$this->assertSame( array(), $mapping );
}
}
public function data_wp_default_image_output_mapping_size_filter() {
return array(
'default size thumbnail' => array(
'thumbnail',
null,
true,
),
'default size medium' => array(
'medium',
null,
true,
),
'default size medium_large' => array(
'medium_large',
null,
true,
),
'default size large' => array(
'large',
null,
true,
),
'default size unset' => array(
'medium',
function( $enabled_sizes ) {
unset( $enabled_sizes['medium'] );
return $enabled_sizes;
},
false,
),
'default size set to false' => array(
'medium',
function( $enabled_sizes ) {
$enabled_sizes['medium'] = false;
return $enabled_sizes;
},
false,
),
'custom size' => array(
'custom',
null,
false,
),
'custom size opted in' => array(
'custom',
function( $enabled_sizes ) {
$enabled_sizes['custom'] = true;
return $enabled_sizes;
},
true,
),
);
}
}
/**