Make WordPress Core


Ignore:
Timestamp:
09/07/2022 09:43:28 PM (2 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/class-wp-image-editor-imagick.php

    r53547 r54097  
    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.
     
    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
     
    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,
     
    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'] ) ) {
     
    502508        if ( ! isset( $size_data['crop'] ) ) {
    503509            $size_data['crop'] = false;
     510        }
     511
     512        if ( isset( $size_data['name'] ) ) {
     513            $this->update_size_name( $size_data['name'] );
    504514        }
    505515
     
    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 ) ) {
Note: See TracChangeset for help on using the changeset viewer.