Make WordPress Core


Ignore:
Timestamp:
05/04/2021 02:43:36 PM (3 years 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/functions.php

    r50505 r50810  
    28872887                    'image/bmp'  => 'bmp',
    28882888                    'image/tiff' => 'tif',
     2889                    'image/webp' => 'webp',
    28892890                )
    28902891            );
     
    30643065            $mime = false;
    30653066        }
     3067
     3068        if ( false !== $mime ) {
     3069            return $mime;
     3070        }
     3071
     3072        $handle = fopen( $file, 'rb' );
     3073        if ( false === $handle ) {
     3074            return false;
     3075        }
     3076
     3077        $magic = fread( $handle, 12 );
     3078        if ( false === $magic ) {
     3079            return false;
     3080        }
     3081
     3082        // Add WebP fallback detection when image library doesn't support WebP.
     3083        // Note: detection values come from LibWebP, see
     3084        // https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30
     3085        $magic = bin2hex( $magic );
     3086        if (
     3087            // RIFF.
     3088            ( 0 === strpos( $magic, '52494646' ) ) &&
     3089            // WEBP.
     3090            ( 16 === strpos( $magic, '57454250' ) )
     3091        ) {
     3092            $mime = 'image/webp';
     3093        }
     3094
     3095        fclose( $handle );
    30663096    } catch ( Exception $e ) {
    30673097        $mime = false;
     
    31023132            'bmp'                          => 'image/bmp',
    31033133            'tiff|tif'                     => 'image/tiff',
     3134            'webp'                         => 'image/webp',
    31043135            'ico'                          => 'image/x-icon',
    31053136            'heic'                         => 'image/heic',
     
    32233254        'ext2type',
    32243255        array(
    3225             'image'       => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic' ),
     3256            'image'       => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic', 'webp' ),
    32263257            'audio'       => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
    32273258            'video'       => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
Note: See TracChangeset for help on using the changeset viewer.