Make WordPress Core


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

    r50284 r50810  
    12271227
    12281228        /**
     1229         * @ticket 35725
     1230         * @dataProvider data_wp_getimagesize
     1231         */
     1232        public function test_wp_getimagesize( $file, $expected ) {
     1233                if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) {
     1234                        $this->markTestSkipped( 'The exif PHP extension is not loaded.' );
     1235                }
     1236
     1237                $result = wp_getimagesize( $file );
     1238
     1239                // The getimagesize() function varies in its response, so
     1240                // let's restrict comparison to expected keys only.
     1241                if ( is_array( $expected ) ) {
     1242                        foreach ( $expected as $k => $v ) {
     1243                                $this->assertEquals( true, isset( $result[ $k ] ) );
     1244                                $this->assertEquals( $expected[ $k ], $result[ $k ] );
     1245                        }
     1246                } else {
     1247                        $this->assertEquals( $expected, $result );
     1248                }
     1249        }
     1250
     1251        /**
    12291252         * @ticket 39550
    12301253         * @dataProvider _wp_check_filetype_and_ext_data
     
    13131336                                DIR_TESTDATA . '/images/test-image-mime-jpg.png',
    13141337                                'image/jpeg',
     1338                        ),
     1339                        // Animated WebP.
     1340                        array(
     1341                                DIR_TESTDATA . '/images/webp-animated.webp',
     1342                                'image/webp',
     1343                        ),
     1344                        // Lossless WebP.
     1345                        array(
     1346                                DIR_TESTDATA . '/images/webp-lossless.webp',
     1347                                'image/webp',
     1348                        ),
     1349                        // Lossy WebP.
     1350                        array(
     1351                                DIR_TESTDATA . '/images/webp-lossy.webp',
     1352                                'image/webp',
     1353                        ),
     1354                        // Transparent WebP.
     1355                        array(
     1356                                DIR_TESTDATA . '/images/webp-transparent.webp',
     1357                                'image/webp',
     1358                        ),
     1359                        // Not an image.
     1360                        array(
     1361                                DIR_TESTDATA . '/uploads/dashicons.woff',
     1362                                false,
     1363                        ),
     1364                );
     1365
     1366                return $data;
     1367        }
     1368
     1369        /**
     1370         * Data profider for test_wp_getimagesize();
     1371         */
     1372        public function data_wp_getimagesize() {
     1373                $data = array(
     1374                        // Standard JPEG.
     1375                        array(
     1376                                DIR_TESTDATA . '/images/test-image.jpg',
     1377                                array(
     1378                                        50,
     1379                                        50,
     1380                                        IMAGETYPE_JPEG,
     1381                                        'width="50" height="50"',
     1382                                        'mime' => 'image/jpeg',
     1383                                ),
     1384                        ),
     1385                        // Standard GIF.
     1386                        array(
     1387                                DIR_TESTDATA . '/images/test-image.gif',
     1388                                array(
     1389                                        50,
     1390                                        50,
     1391                                        IMAGETYPE_GIF,
     1392                                        'width="50" height="50"',
     1393                                        'mime' => 'image/gif',
     1394                                ),
     1395                        ),
     1396                        // Standard PNG.
     1397                        array(
     1398                                DIR_TESTDATA . '/images/test-image.png',
     1399                                array(
     1400                                        50,
     1401                                        50,
     1402                                        IMAGETYPE_PNG,
     1403                                        'width="50" height="50"',
     1404                                        'mime' => 'image/png',
     1405                                ),
     1406                        ),
     1407                        // Image with wrong extension.
     1408                        array(
     1409                                DIR_TESTDATA . '/images/test-image-mime-jpg.png',
     1410                                array(
     1411                                        50,
     1412                                        50,
     1413                                        IMAGETYPE_JPEG,
     1414                                        'width="50" height="50"',
     1415                                        'mime' => 'image/jpeg',
     1416                                ),
     1417                        ),
     1418                        // Animated WebP.
     1419                        array(
     1420                                DIR_TESTDATA . '/images/webp-animated.webp',
     1421                                array(
     1422                                        100,
     1423                                        100,
     1424                                        IMAGETYPE_WEBP,
     1425                                        'width="100" height="100"',
     1426                                        'mime' => 'image/webp',
     1427                                ),
     1428                        ),
     1429                        // Lossless WebP.
     1430                        array(
     1431                                DIR_TESTDATA . '/images/webp-lossless.webp',
     1432                                array(
     1433                                        1200,
     1434                                        675,
     1435                                        IMAGETYPE_WEBP,
     1436                                        'width="1200" height="675"',
     1437                                        'mime' => 'image/webp',
     1438                                ),
     1439                        ),
     1440                        // Lossy WebP.
     1441                        array(
     1442                                DIR_TESTDATA . '/images/webp-lossy.webp',
     1443                                array(
     1444                                        1200,
     1445                                        675,
     1446                                        IMAGETYPE_WEBP,
     1447                                        'width="1200" height="675"',
     1448                                        'mime' => 'image/webp',
     1449                                ),
     1450                        ),
     1451                        // Transparent WebP.
     1452                        array(
     1453                                DIR_TESTDATA . '/images/webp-transparent.webp',
     1454                                array(
     1455                                        1200,
     1456                                        675,
     1457                                        IMAGETYPE_WEBP,
     1458                                        'width="1200" height="675"',
     1459                                        'mime' => 'image/webp',
     1460                                ),
    13151461                        ),
    13161462                        // Not an image.
Note: See TracChangeset for help on using the changeset viewer.