Make WordPress Core

Changeset 57524

Timestamp:
02/02/2024 05:46:50 PM (6 months ago)
Author:
adamsilverstein
Message:

Media: enable AVIF support.

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

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

Props adamsilverstein, lukefiretoss, ayeshrajans, navjotjsingh, Tyrannous, jb510, gregbenz, nickpagz, JavierCasares, mukesh27, yguyon, swissspidy.
Fixes #51228.

Location:
trunk
Files:
7 added
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpcs.xml.dist

    r57264 r57524  
    6060    <exclude-pattern>/src/wp-includes/class-simplepie\.php</exclude-pattern>
    6161    <exclude-pattern>/src/wp-includes/class-snoopy\.php</exclude-pattern>
     62
    6263    <exclude-pattern>/src/wp-includes/deprecated\.php</exclude-pattern>
    6364    <exclude-pattern>/src/wp-includes/ms-deprecated\.php</exclude-pattern>
  • trunk/src/js/_enqueues/vendor/plupload/handlers.js

    r57231 r57524  
    609609                    up.removeFile( file );
    610610                    return;
     611
     612
     613
     614
     615
    611616                }
    612617
  • trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js

    r54085 r57524  
    364364                    up.removeFile( file );
    365365                    return;
     366
     367
     368
     369
     370
    366371                }
    367372
  • trunk/src/js/_enqueues/vendor/thickbox/thickbox.js

    r53451 r57524  
    7777       }
    7878
    79        var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.webp$/;
     79       var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.webp$/;
    8080       var urlType = baseURL.toLowerCase().match(urlString);
    8181
     
    8585            urlType == '.gif' ||
    8686            urlType == '.bmp' ||
    87             urlType == '.webp'
     87            urlType == '.webp' ||
     88            urlType == '.avif'
    8889        ){//code to show images
    8990
  • trunk/src/js/media/controllers/library.js

    r52073 r57524  
    197197        // If uploading, we know the filename but not the mime type.
    198198        if ( attachment.get('uploading') ) {
    199             return /\.(jpe?g|png|gif|webp)$/i.test( attachment.get('filename') );
     199            return /\.(jpe?g|png|gif|webp)$/i.test( attachment.get('filename') );
    200200        }
    201201
  • trunk/src/wp-admin/includes/image-edit.php

    r56653 r57524  
    391391                }
    392392                return false;
     393
     394
     395
     396
     397
     398
    393399            default:
    394400                return false;
     
    493499                if ( function_exists( 'imagewebp' ) ) {
    494500                    return imagewebp( $image, $filename );
     501
     502
     503
     504
     505
    495506                }
    496507                return false;
  • trunk/src/wp-admin/includes/image.php

    r57267 r57524  
    10071007 */
    10081008function file_is_displayable_image( $path ) {
    1009     $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP );
     1009    $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP );
    10101010
    10111011    $info = wp_getimagesize( $path );
  • trunk/src/wp-admin/includes/media.php

    r57181 r57524  
    21992199    }
    22002200
     2201
     2202
     2203
     2204
     2205
    22012206    /**
    22022207     * Filters the default Plupload settings.
  • trunk/src/wp-admin/includes/schema.php

    r57389 r57524  
    12511251        'gif',
    12521252        'webp',
     1253
    12531254        // Video.
    12541255        'mov',
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r56418 r57524  
    7272            case 'image/webp':
    7373                return ( $image_types & IMG_WEBP ) != 0;
     74
     75
    7476        }
    7577
     
    112114        }
    113115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
    114126        if ( ! is_gd_image( $this->image ) ) {
    115127            return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
     
    512524        } elseif ( 'image/webp' == $mime_type ) {
    513525            if ( ! function_exists( 'imagewebp' ) || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) ) {
     526
     527
     528
     529
    514530                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    515531            }
     
    562578                    header( 'Content-Type: image/webp' );
    563579                    return imagewebp( $this->image, null, $this->get_quality() );
     580
     581
     582
     583
    564584                }
    565                 // Fall back to the default if webp isn't supported.
     585            case 'image/avif':
     586                if ( function_exists( 'imageavif' ) ) {
     587                    header( 'Content-Type: image/avif' );
     588                    return imageavif( $this->image, null, $this->get_quality() );
     589                }
     590                // Fall back to JPEG.
    566591            default:
    567592                header( 'Content-Type: image/jpeg' );
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r56547 r57524  
    220220                    }
    221221                    break;
     222
    222223                default:
    223224                    $this->image->setImageCompressionQuality( $quality );
     
    255256        if ( ! $height ) {
    256257            $height = $size['height'];
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
    257268        }
    258269
  • trunk/src/wp-includes/class-wp-image-editor.php

    r56536 r57524  
    319319                break;
    320320            case 'image/jpeg':
     321
    321322            default:
    322323                $quality = $this->default_quality;
  • trunk/src/wp-includes/class-wp-theme.php

    r56978 r57524  
    12641264        }
    12651265
    1266         foreach ( array( 'png', 'gif', 'jpg', 'jpeg', 'webp' ) as $ext ) {
     1266        foreach ( array( 'png', 'gif', 'jpg', 'jpeg', 'webp' ) as $ext ) {
    12671267            if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
    12681268                $this->cache_add( 'screenshot', 'screenshot.' . $ext );
  • trunk/src/wp-includes/compat.php

    r57337 r57524  
    530530    define( 'IMG_WEBP', IMAGETYPE_WEBP );
    531531}
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
  • trunk/src/wp-includes/customize/class-wp-customize-media-control.php

    r56193 r57524  
    9494                 */
    9595                $ext  = substr( $this->setting->default, -3 );
    96                 $type = in_array( $ext, array( 'jpg', 'png', 'gif', 'bmp', 'webp' ), true ) ? 'image' : 'document';
     96                $type = in_array( $ext, array( 'jpg', 'png', 'gif', 'bmp', 'webp' ), true ) ? 'image' : 'document';
    9797
    9898                $default_attachment = array(
  • trunk/src/wp-includes/deprecated.php

    r57511 r57524  
    33373337            case 'image/webp':
    33383338                return (imagetypes() & IMG_WEBP) != 0;
    3339         }
     3339            case 'image/avif':
     3340                return (imagetypes() & IMG_AVIF) != 0;
     3341            }
    33403342    } else {
    33413343        switch( $mime_type ) {
     
    33483350            case 'image/webp':
    33493351                return function_exists('imagecreatefromwebp');
     3352
     3353
    33503354        }
    33513355    }
  • trunk/src/wp-includes/formatting.php

    r57302 r57524  
    34653465    $matches    = array();
    34663466    $ext        = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
    3467     $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
     3467    $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
    34683468
    34693469    // Don't convert smilies that aren't images - they're probably emoji.
  • trunk/src/wp-includes/functions.php

    r57509 r57524  
    31183118                    'image/tiff' => 'tif',
    31193119                    'image/webp' => 'webp',
     3120
    31203121                )
    31213122            );
     
    32963297 * @since 4.7.1
    32973298 * @since 5.8.0 Added support for WebP images.
     3299
    32983300 *
    32993301 * @param string $file Full path to the file.
     
    33503352            $mime = 'image/webp';
    33513353        }
     3354
     3355
     3356
     3357
     3358
     3359
     3360
     3361
     3362
     3363
     3364
     3365
     3366
     3367
     3368
     3369
     3370
     3371
     3372
    33523373    } catch ( Exception $e ) {
    33533374        $mime = false;
     
    33893410            'tiff|tif'                     => 'image/tiff',
    33903411            'webp'                         => 'image/webp',
     3412
    33913413            'ico'                          => 'image/x-icon',
    33923414            'heic'                         => 'image/heic',
     
    35103532        'ext2type',
    35113533        array(
    3512             'image'       => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic', 'webp' ),
     3534            'image'       => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic', 'webp' ),
    35133535            'audio'       => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
    35143536            'video'       => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
  • trunk/src/wp-includes/media.php

    r57294 r57524  
    41014101    require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
    41024102    require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
     4103
    41034104    /**
    41044105     * Filters the list of image editing library classes.
     
    42034204    if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
    42044205        $defaults['webp_upload_error'] = true;
     4206
     4207
     4208
     4209
     4210
    42054211    }
    42064212
     
    54815487 * @since 5.7.0
    54825488 * @since 5.8.0 Added support for WebP images.
     5489
    54835490 *
    54845491 * @param string $filename   The file path.
     
    55135520    }
    55145521
    5515     if ( false !== $info ) {
     5522    if (
     5523        ! empty( $info ) &&
     5524        // Some PHP versions return 0x0 sizes from `getimagesize` for unrecognized image formats, including AVIFs.
     5525        ! ( empty( $info[0] ) && empty( $info[1] ) )
     5526    ) {
    55165527        return $info;
    55175528    }
     
    55425553    }
    55435554
     5555
     5556
     5557
     5558
     5559
     5560
     5561
     5562
     5563
     5564
     5565
     5566
     5567
     5568
     5569
     5570
     5571
     5572
     5573
     5574
     5575
     5576
     5577
    55445578    // The image could not be parsed.
    55455579    return false;
     5580
     5581
     5582
     5583
     5584
     5585
     5586
     5587
     5588
     5589
     5590
     5591
     5592
     5593
     5594
     5595
     5596
     5597
     5598
     5599
     5600
     5601
     5602
     5603
     5604
     5605
     5606
     5607
     5608
     5609
     5610
     5611
     5612
     5613
     5614
     5615
     5616
     5617
     5618
     5619
     5620
     5621
    55465622}
    55475623
  • trunk/src/wp-includes/post.php

    r57273 r57524  
    67016701    switch ( $type ) {
    67026702        case 'image':
    6703             $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
     6703            $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
    67046704            return in_array( $ext, $image_exts, true );
    67056705
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r57380 r57524  
    457457        }
    458458
    459         $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' );
     459        $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' );
    460460        $mime_type       = get_post_mime_type( $attachment_id );
    461461        if ( ! in_array( $mime_type, $supported_types, true ) ) {
  • trunk/tests/phpunit/tests/functions.php

    r57284 r57524  
    13711371                false,
    13721372            ),
     1373
     1374
     1375
     1376
     1377
     1378
     1379
     1380
     1381
     1382
     1383
     1384
     1385
     1386
     1387
     1388
     1389
     1390
     1391
     1392
    13731393        );
    13741394
     
    14961516                DIR_TESTDATA . '/uploads/dashicons.woff',
    14971517                false,
     1518
     1519
     1520
     1521
     1522
     1523
     1524
     1525
     1526
     1527
     1528
     1529
     1530
     1531
     1532
     1533
     1534
     1535
     1536
     1537
     1538
     1539
     1540
     1541
     1542
     1543
     1544
     1545
     1546
     1547
     1548
     1549
     1550
     1551
     1552
     1553
     1554
     1555
     1556
     1557
     1558
     1559
     1560
     1561
    14981562            ),
    14991563        );
  • trunk/tests/phpunit/tests/image/editor.php

    r56547 r57524  
    293293     */
    294294    public function test_wp_get_webp_info( $file, $expected ) {
    295         $editor = wp_get_image_editor( $file );
    296 
    297         if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
    298             $this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) );
    299         }
    300 
    301295        $file_data = wp_get_webp_info( $file );
    302296        $this->assertSame( $expected, $file_data );
     
    364358        );
    365359    }
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
    366461}
  • trunk/tests/phpunit/tests/image/functions.php

    r56559 r57524  
    112112            'webp-lossy.webp',
    113113            'webp-transparent.webp',
     114
     115
     116
     117
    114118        );
    115119
     
    185189            $files[] = 'webp-lossy.webp';
    186190            $files[] = 'webp-transparent.webp';
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
    187202        }
    188203
  • trunk/tests/phpunit/tests/image/resize.php

    r54226 r57524  
    8989    }
    9090
     91
     92
     93
     94
     95
     96
     97
     98
     99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
    91117    public function test_resize_larger() {
    92118        // image_resize() should refuse to make an image larger.
Note: See TracChangeset for help on using the changeset viewer.