WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/04/2021 02:43:36 PM (7 months ago)
Author:
adamsilverstein
Message:

Images: enable WebP support.

Add support for uploading, editing and saving WebP images when supported by the server.

Add 'image/webp' to supported mime types. Correctly identify WebP images and sizes even when PHP doesn't support WebP. Resize uploaded WebP files (when supported) and use for front end markup.

Props markoheijne, blobfolio, Clorith, joemcgill, atjn, desrosj, spacedmonkey, marylauc, mikeschroder, hellofromtonya, flixos90.
Fixes #35725.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r49927 r50810  
    198198
    199199        try {
    200             if ( 'image/jpeg' === $this->mime_type ) {
    201                 $this->image->setImageCompressionQuality( $quality );
    202                 $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
    203             } else {
    204                 $this->image->setImageCompressionQuality( $quality );
     200            switch ( $this->mime_type ) {
     201                case 'image/jpeg':
     202                    $this->image->setImageCompressionQuality( $quality );
     203                    $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
     204                    break;
     205                case 'image/webp':
     206                    if ( _wp_webp_is_lossy( $this->file ) ) {
     207                        $this->image->setImageCompressionQuality( $quality );
     208                    } else {
     209                        // Use WebP lossless settings.
     210                        $this->image->setImageCompressionQuality( 100 );
     211                        $this->image->setOption( 'webp:lossless', 'true' );
     212                    }
     213                    break;
     214                default:
     215                    $this->image->setImageCompressionQuality( $quality );
    205216            }
    206217        } catch ( Exception $e ) {
    207218            return new WP_Error( 'image_quality_error', $e->getMessage() );
    208219        }
    209 
    210220        return true;
    211221    }
     222
    212223
    213224    /**
Note: See TracChangeset for help on using the changeset viewer.