Make WordPress Core


Ignore:
Timestamp:
09/07/2022 09:43:28 PM (4 years ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r54094 r54097  
    39133913 *
    39143914 * @param array $output_mapping Map of mime type to output format.
    3915  * @retun array The adjusted default output mapping.
    3916  */
    3917 function wp_default_image_output_mapping( $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.
     3919 */
     3920function 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;
Note: See TracChangeset for help on using the changeset viewer.