Skip to content

Commit d119ff7

Browse files
committed
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
1 parent d51e65b commit d119ff7

8 files changed

Lines changed: 177 additions & 25 deletions

File tree

src/wp-admin/includes/image.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
451451

452452
if ( method_exists( $editor, 'make_subsize' ) ) {
453453
foreach ( $new_sizes as $new_size_name => $new_size_data ) {
454+
// Include size name in the data.
455+
$new_size_data['name'] = $new_size_name;
456+
454457
$new_size_meta = $editor->make_subsize( $new_size_data );
455458

456459
if ( is_wp_error( $new_size_meta ) ) {

src/wp-includes/class-wp-image-editor-gd.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ protected function _resize( $max_w, $max_h, $crop = false ) {
227227
* @since 3.5.0
228228
*
229229
* @param array $sizes {
230-
* An array of image size data arrays.
230+
* Associative array of image size names and their data.
231231
*
232232
* Either a height or width must be provided.
233233
* If one of the two is set to null, the resize will
@@ -247,6 +247,9 @@ public function multi_resize( $sizes ) {
247247
$metadata = array();
248248

249249
foreach ( $sizes as $size => $size_data ) {
250+
// Include size name in the data.
251+
$size_data['name'] = $size;
252+
250253
$meta = $this->make_subsize( $size_data );
251254

252255
if ( ! is_wp_error( $meta ) ) {
@@ -261,13 +264,15 @@ public function multi_resize( $sizes ) {
261264
* Create an image sub-size and return the image meta data value for it.
262265
*
263266
* @since 5.3.0
267+
* @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
264268
*
265269
* @param array $size_data {
266270
* Array of size data.
267271
*
268-
* @type int $width The maximum width in pixels.
269-
* @type int $height The maximum height in pixels.
270-
* @type bool $crop Whether to crop the image to exact dimensions.
272+
* @type int $width The maximum width in pixels.
273+
* @type int $height The maximum height in pixels.
274+
* @type bool $crop Whether to crop the image to exact dimensions.
275+
* @type string $name Image size name.
271276
* }
272277
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
273278
* WP_Error object on error.
@@ -277,7 +282,8 @@ public function make_subsize( $size_data ) {
277282
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
278283
}
279284

280-
$orig_size = $this->size;
285+
$orig_size = $this->size;
286+
$orig_size_name = $this->size_name;
281287

282288
if ( ! isset( $size_data['width'] ) ) {
283289
$size_data['width'] = null;
@@ -291,6 +297,10 @@ public function make_subsize( $size_data ) {
291297
$size_data['crop'] = false;
292298
}
293299

300+
if ( isset( $size_data['name'] ) ) {
301+
$this->update_size_name( $size_data['name'] );
302+
}
303+
294304
$resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
295305

296306
if ( is_wp_error( $resized ) ) {
@@ -301,6 +311,7 @@ public function make_subsize( $size_data ) {
301311
}
302312

303313
$this->size = $orig_size;
314+
$this->size_name = $orig_size_name;
304315

305316
if ( ! is_wp_error( $saved ) ) {
306317
unset( $saved['path'] );

src/wp-includes/class-wp-image-editor-imagick.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIAN
438438
* @since 3.5.0
439439
*
440440
* @param array $sizes {
441-
* An array of image size data arrays.
441+
* Associative array of image size names and their data.
442442
*
443443
* Either a height or width must be provided.
444444
* If one of the two is set to null, the resize will
@@ -458,6 +458,9 @@ public function multi_resize( $sizes ) {
458458
$metadata = array();
459459

460460
foreach ( $sizes as $size => $size_data ) {
461+
// Include size name in the data.
462+
$size_data['name'] = $size;
463+
461464
$meta = $this->make_subsize( $size_data );
462465

463466
if ( ! is_wp_error( $meta ) ) {
@@ -472,13 +475,15 @@ public function multi_resize( $sizes ) {
472475
* Create an image sub-size and return the image meta data value for it.
473476
*
474477
* @since 5.3.0
478+
* @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
475479
*
476480
* @param array $size_data {
477481
* Array of size data.
478482
*
479-
* @type int $width The maximum width in pixels.
480-
* @type int $height The maximum height in pixels.
481-
* @type bool $crop Whether to crop the image to exact dimensions.
483+
* @type int $width The maximum width in pixels.
484+
* @type int $height The maximum height in pixels.
485+
* @type bool $crop Whether to crop the image to exact dimensions.
486+
* @type string $name Image size name.
482487
* }
483488
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
484489
* WP_Error object on error.
@@ -488,8 +493,9 @@ public function make_subsize( $size_data ) {
488493
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
489494
}
490495

491-
$orig_size = $this->size;
492-
$orig_image = $this->image->getImage();
496+
$orig_size = $this->size;
497+
$orig_size_name = $this->size_name;
498+
$orig_image = $this->image->getImage();
493499

494500
if ( ! isset( $size_data['width'] ) ) {
495501
$size_data['width'] = null;
@@ -503,6 +509,10 @@ public function make_subsize( $size_data ) {
503509
$size_data['crop'] = false;
504510
}
505511

512+
if ( isset( $size_data['name'] ) ) {
513+
$this->update_size_name( $size_data['name'] );
514+
}
515+
506516
$resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
507517

508518
if ( is_wp_error( $resized ) ) {
@@ -515,8 +525,9 @@ public function make_subsize( $size_data ) {
515525
$this->image = null;
516526
}
517527

518-
$this->size = $orig_size;
519-
$this->image = $orig_image;
528+
$this->size = $orig_size;
529+
$this->size_name = $orig_size_name;
530+
$this->image = $orig_image;
520531

521532
if ( ! is_wp_error( $saved ) ) {
522533
unset( $saved['path'] );

src/wp-includes/class-wp-image-editor.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
abstract class WP_Image_Editor {
1515
protected $file = null;
1616
protected $size = null;
17+
protected $size_name = '';
1718
protected $mime_type = null;
1819
protected $output_mime_type = null;
1920
protected $default_mime_type = 'image/jpeg';
@@ -117,7 +118,7 @@ abstract public function resize( $max_w, $max_h, $crop = false );
117118
* @abstract
118119
*
119120
* @param array $sizes {
120-
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
121+
* Associative array of image size names and their data. Default sizes are 'small', 'medium', 'large'.
121122
*
122123
* @type array ...$0 {
123124
* @type int $width Image width.
@@ -185,7 +186,7 @@ abstract public function stream( $mime_type = null );
185186
*
186187
* @since 3.5.0
187188
*
188-
* @return int[] {
189+
* @return array {
189190
* Dimensions of the image.
190191
*
191192
* @type int $width The image width.
@@ -201,9 +202,9 @@ public function get_size() {
201202
*
202203
* @since 3.5.0
203204
*
204-
* @param int $width
205-
* @param int $height
206-
* @return true
205+
* @param int $width The image width.
206+
* @param int $height The image height.
207+
* @return true True on success, false on failure.
207208
*/
208209
protected function update_size( $width = null, $height = null ) {
209210
$this->size = array(
@@ -213,6 +214,28 @@ protected function update_size( $width = null, $height = null ) {
213214
return true;
214215
}
215216

217+
/**
218+
* Gets the current image size name.
219+
*
220+
* @since 6.1.0
221+
*
222+
* @return string Image size name, or empty string if none set.
223+
*/
224+
public function get_size_name() {
225+
return $this->size_name;
226+
}
227+
228+
/**
229+
* Sets the current image size name.
230+
*
231+
* @since 6.1.0
232+
*
233+
* @param string $size_name The image size name.
234+
*/
235+
protected function update_size_name( $size_name ) {
236+
$this->size_name = (string) $size_name;
237+
}
238+
216239
/**
217240
* Gets the Image Compression quality on a 1-100% scale.
218241
*
@@ -364,6 +387,7 @@ protected function get_output_format( $filename = null, $mime_type = null ) {
364387
* @see WP_Image_Editor::get_output_format()
365388
*
366389
* @since 5.8.0
390+
* @since 6.1.0 The $size_name parameter was added.
367391
*
368392
* @param string[] $output_format {
369393
* An array of mime type mappings. Maps a source mime type to a new
@@ -373,8 +397,9 @@ protected function get_output_format( $filename = null, $mime_type = null ) {
373397
* }
374398
* @param string $filename Path to the image.
375399
* @param string $mime_type The source image mime type.
400+
* @param string $size_name The image size name to create, or empty string if not set.
376401
*/
377-
$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
402+
$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type, $this->size_name );
378403

379404
if ( isset( $output_format[ $mime_type ] )
380405
&& $this->supports_mime_type( $output_format[ $mime_type ] )

src/wp-includes/default-filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@
632632
add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
633633
add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
634634

635-
add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
635+
add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping', 10, 4 );
636636

637637
// Embeds.
638638
add_action( 'rest_api_init', 'wp_oembed_register_route' );

src/wp-includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null )
26902690
*/
26912691
if ( $is_image ) {
26922692
/** This filter is documented in wp-includes/class-wp-image-editor.php */
2693-
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type );
2693+
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type, '' );
26942694
$alt_types = array();
26952695

26962696
if ( ! empty( $output_formats[ $mime_type ] ) ) {

src/wp-includes/media.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,9 +3912,39 @@ function _wp_image_editor_choose( $args = array() ) {
39123912
* @since 6.1.0
39133913
*
39143914
* @param array $output_mapping Map of mime type to output format.
3915-
* @retun array The adjusted default output mapping.
3915+
* @param string $filename Path to the image.
3916+
* @param string $mime_type The source image mime type.
3917+
* @param string $size_name Optional. The image size name to create, or empty string if not set. Default empty string.
3918+
* @return array The adjusted default output mapping.
39163919
*/
3917-
function wp_default_image_output_mapping( $output_mapping ) {
3920+
function wp_default_image_output_mapping( $output_mapping, $filename, $mime_type, $size_name = '' ) {
3921+
// If size name is specified, check whether the size supports additional MIME types like WebP.
3922+
if ( $size_name ) {
3923+
// Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.
3924+
$enabled_sizes = array(
3925+
'thumbnail' => true,
3926+
'medium' => true,
3927+
'medium_large' => true,
3928+
'large' => true,
3929+
'post-thumbnail' => true,
3930+
);
3931+
3932+
/**
3933+
* Filters the sizes that support secondary mime type output. Developers can use this
3934+
* to control the generation of additional mime type sub-sized images.
3935+
*
3936+
* @since 6.1.0
3937+
*
3938+
* @param array $enabled_sizes Map of size names and whether they support secondary mime type output.
3939+
*/
3940+
$enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes );
3941+
3942+
// Bail early if the size does not support additional MIME types.
3943+
if ( empty( $enabled_sizes[ $size_name ] ) ) {
3944+
return $output_mapping;
3945+
}
3946+
}
3947+
39183948
$output_mapping['image/jpeg'] = 'image/webp';
39193949
return $output_mapping;
39203950
}

tests/phpunit/tests/media.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ private function reset_omit_loading_attr_filter() {
36263626
* @ticket 55443
36273627
*/
36283628
public function test_wp_default_image_output_mapping() {
3629-
$mapping = wp_default_image_output_mapping( array() );
3629+
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
36303630
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
36313631
}
36323632

@@ -3637,7 +3637,7 @@ public function test_wp_default_image_output_mapping() {
36373637
*/
36383638
public function test_wp_default_image_output_mapping_existing() {
36393639
$mapping = array( 'mime/png' => 'mime/webp' );
3640-
$mapping = wp_default_image_output_mapping( $mapping );
3640+
$mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
36413641
$this->assertSame(
36423642
array(
36433643
'mime/png' => 'mime/webp',
@@ -3672,6 +3672,78 @@ public function test_wp_image_editor_default_output_maps_to_webp() {
36723672
$this->assertSame( 'canola-100x75.jpg', $saved['file'] );
36733673
}
36743674
}
3675+
3676+
/**
3677+
* @ticket 56526
3678+
* @dataProvider data_wp_default_image_output_mapping_size_filter
3679+
*/
3680+
public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
3681+
remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
3682+
if ( $filter_callback ) {
3683+
add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
3684+
}
3685+
3686+
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
3687+
if ( $expects_webp ) {
3688+
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
3689+
} else {
3690+
$this->assertSame( array(), $mapping );
3691+
}
3692+
}
3693+
3694+
public function data_wp_default_image_output_mapping_size_filter() {
3695+
return array(
3696+
'default size thumbnail' => array(
3697+
'thumbnail',
3698+
null,
3699+
true,
3700+
),
3701+
'default size medium' => array(
3702+
'medium',
3703+
null,
3704+
true,
3705+
),
3706+
'default size medium_large' => array(
3707+
'medium_large',
3708+
null,
3709+
true,
3710+
),
3711+
'default size large' => array(
3712+
'large',
3713+
null,
3714+
true,
3715+
),
3716+
'default size unset' => array(
3717+
'medium',
3718+
function( $enabled_sizes ) {
3719+
unset( $enabled_sizes['medium'] );
3720+
return $enabled_sizes;
3721+
},
3722+
false,
3723+
),
3724+
'default size set to false' => array(
3725+
'medium',
3726+
function( $enabled_sizes ) {
3727+
$enabled_sizes['medium'] = false;
3728+
return $enabled_sizes;
3729+
},
3730+
false,
3731+
),
3732+
'custom size' => array(
3733+
'custom',
3734+
null,
3735+
false,
3736+
),
3737+
'custom size opted in' => array(
3738+
'custom',
3739+
function( $enabled_sizes ) {
3740+
$enabled_sizes['custom'] = true;
3741+
return $enabled_sizes;
3742+
},
3743+
true,
3744+
),
3745+
);
3746+
}
36753747
}
36763748

36773749
/**

0 commit comments

Comments
 (0)