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.php

    r54086 r54097  
    1515    protected $file              = null;
    1616    protected $size              = null;
     17    protected $size_name         = '';
    1718    protected $mime_type         = null;
    1819    protected $output_mime_type  = null;
     
    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 {
     
    186187     * @since 3.5.0
    187188     *
    188      * @return int[] {
     189     * @return array {
    189190     *     Dimensions of the image.
    190191     *
     
    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 ) {
     
    212213        );
    213214        return true;
     215    }
     216
     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;
    214237    }
    215238
     
    365388         *
    366389         * @since 5.8.0
     390         * @since 6.1.0 The $size_name parameter was added.
    367391         *
    368392         * @param string[] $output_format {
     
    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 ] )
Note: See TracChangeset for help on using the changeset viewer.