Ticket #52867: 52867.diff
File 52867.diff, 14.3 KB (added by , 4 years ago) |
---|
-
src/wp-includes/class-wp-image-editor-imagick.php
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php index e4f1ea1b16..348714148a 100644
class WP_Image_Editor_Imagick extends WP_Image_Editor { 677 677 * @return array|WP_Error 678 678 */ 679 679 protected function _save( $image, $filename = null, $mime_type = null ) { 680 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); 680 // Imagick supports animated images. 681 $imagick_mappings = array( 682 'image/jpeg' => array( 683 'mime_type' => 'image/webp', 684 'extension' => 'webp', 685 ), 686 'image/png' => array( 687 'mime_type' => 'image/webp', 688 'extension' => 'webp', 689 ), 690 'image/gif' => array( 691 'mime_type' => 'image/webp', 692 'extension' => 'webp', 693 ), 694 ); 695 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type, $imagick_mappings ); 681 696 682 697 if ( ! $filename ) { 683 698 $filename = $this->generate_filename( null, null, $extension ); -
src/wp-includes/class-wp-image-editor.php
diff --git src/wp-includes/class-wp-image-editor.php src/wp-includes/class-wp-image-editor.php index 7dcdc91d5a..103f09684a 100644
abstract class WP_Image_Editor { 290 290 * 291 291 * @param string $filename 292 292 * @param string $mime_type 293 * @param array $image_editor_mime_mapping { 294 * An array of mime type mappings. Maps a source mime type to a new 295 * destination mime type and file extension. Only remaps to supported 296 * mime types. 297 * 298 * @type array $mime_type The source mime type { 299 * @type string $mime_type The new mime type. 300 * @type string $extension The new mime file extension. 301 * } 293 302 * @return array { filename|null, extension, mime-type } 294 303 */ 295 protected function get_output_format( $filename = null, $mime_type = null ) { 304 protected function get_output_format( $filename = null, $mime_type = null, $image_editor_mime_mapping = array( 305 'image/jpeg' => array( 306 'mime_type' => 'image/webp', 307 'extension' => 'webp', 308 ), 309 ) ) { 296 310 $new_ext = null; 297 311 298 312 // By default, assume specified type takes priority. … … abstract class WP_Image_Editor { 316 330 $new_ext = $file_ext; 317 331 } 318 332 333 /** 334 * Filters the default mime mapping. 335 * 336 * @see src/wp-includes/class-wp-image-editor.php -> get_output_format() 337 * 338 * @since 5.8.0 339 * 340 * @param array $image_editor_mime_mapping { 341 * An array of mime type mappings. Maps a source mime type to a new 342 * destination mime type and file extension. Only remaps to supported 343 * mime types. 344 * 345 * @type array $mime_type The source mime type { 346 * @type string $mime_type The new mime type. 347 * @type string $extension The new mime file extension. 348 * } 349 * } 350 */ 351 $image_editor_mime_mapping = apply_filters( 'image_editor_mime_mapping', $image_editor_mime_mapping, $filename, $mime_type ); 352 353 if ( 354 $image_editor_mime_mapping && 355 isset( $image_editor_mime_mapping[ $mime_type ] ) && 356 isset( $image_editor_mime_mapping[ $mime_type ]['mime_type'] ) && 357 $image_editor_mime_mapping[ $mime_type ] && 358 this->supports_mime_type( $image_editor_mime_mapping[ $mime_type ]['mime_type'] ) 359 ) { 360 $new_ext = $image_editor_mime_mapping[ $mime_type ]['extension']; 361 $mime_type = $image_editor_mime_mapping[ $mime_type ]['mime_type']; 362 } 363 319 364 // Double-check that the mime-type selected is supported by the editor. 320 365 // If not, choose a default instead. 321 366 if ( ! $this->supports_mime_type( $mime_type ) ) { -
tests/phpunit/tests/functions.php
diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php index f485d3b4ec..75a2ecf5e0 100644
class Tests_Functions extends WP_UnitTestCase { 1225 1225 $this->assertSame( $expected, wp_get_image_mime( $file ) ); 1226 1226 } 1227 1227 1228 /** 1229 * @ticket 35725 1230 * @dataProvider _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 1228 1251 /** 1229 1252 * @ticket 39550 1230 1253 * @dataProvider _wp_check_filetype_and_ext_data … … class Tests_Functions extends WP_UnitTestCase { 1313 1336 DIR_TESTDATA . '/images/test-image-mime-jpg.png', 1314 1337 'image/jpeg', 1315 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 _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 ), 1461 ), 1316 1462 // Not an image. 1317 1463 array( 1318 1464 DIR_TESTDATA . '/uploads/dashicons.woff', -
tests/phpunit/tests/image/editorGd.php
diff --git tests/phpunit/tests/image/editorGd.php tests/phpunit/tests/image/editorGd.php index d2523f4c6f..d2493d9650 100644
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 17 17 require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; 18 18 require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; 19 19 20 add_filter( 'image_editor_mime_mapping', '__return_false' ); 21 20 22 // This needs to come after the mock image editor class is loaded. 21 23 parent::setUp(); 22 24 } … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 29 31 } 30 32 31 33 $this->remove_added_uploads(); 34 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 32 35 33 36 parent::tearDown(); 34 37 } … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 110 113 ); 111 114 } 112 115 116 /** 117 * Test multi_resize with single image resize and no crop, with auto conversion 118 * to the webp format. 119 */ 120 public function test_single_multi_resize_to_webp() { 121 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 122 123 $file = DIR_TESTDATA . '/images/waffles.jpg'; 124 125 $gd_image_editor = new WP_Image_Editor_GD( $file ); 126 $gd_image_editor->load(); 127 128 $sizes_array = array( 129 array( 130 'width' => 50, 131 'height' => 50, 132 ), 133 ); 134 135 $resized = $gd_image_editor->multi_resize( $sizes_array ); 136 137 // First, check to see if returned array is as expected. 138 $expected_array = array( 139 array( 140 'file' => 'waffles-50x33.webp', 141 'width' => 50, 142 'height' => 33, 143 'mime-type' => 'image/webp', 144 ), 145 ); 146 147 $this->assertSame( $expected_array, $resized ); 148 149 // Now, verify real dimensions are as expected. 150 $image_path = DIR_TESTDATA . '/images/' . $resized[0]['file']; 151 $this->assertImageDimensions( 152 $image_path, 153 $expected_array[0]['width'], 154 $expected_array[0]['height'] 155 ); 156 add_filter( 'image_editor_mime_mapping', '__return_false' ); 157 } 158 159 113 160 /** 114 161 * Ensure multi_resize doesn't create an image when 115 162 * both height and weight are missing, null, or 0. -
tests/phpunit/tests/image/functions.php
diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php index 1694c1e7f1..315e6aae90 100644
class Tests_Image_Functions extends WP_UnitTestCase { 25 25 foreach ( glob( $folder ) as $file ) { 26 26 unlink( $file ); 27 27 } 28 add_filter( 'image_editor_mime_mapping', '__return_false' ); 29 } 30 31 public function tearDown() { 32 parent::tearDown(); 33 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 28 34 } 29 35 30 36 /** … … class Tests_Image_Functions extends WP_UnitTestCase { 59 65 'test-image.psd', 60 66 'test-image-zip.tiff', 61 67 'test-image.jpg', 68 'webp-animated.webp', 69 'webp-lossless.webp', 70 'webp-lossy.webp', 71 'webp-transparent.webp', 62 72 ); 63 73 64 74 // IMAGETYPE_ICO is only defined in PHP 5.3+. … … class Tests_Image_Functions extends WP_UnitTestCase { 90 100 'test-image.gif', 91 101 'test-image.png', 92 102 'test-image.jpg', 103 'webp-animated.webp', 104 'webp-lossless.webp', 105 'webp-lossy.webp', 106 'webp-transparent.webp', 93 107 ); 94 108 95 109 // IMAGETYPE_ICO is only defined in PHP 5.3+. … … class Tests_Image_Functions extends WP_UnitTestCase { 152 166 * @requires extension fileinfo 153 167 */ 154 168 public function test_wp_save_image_file() { 169 add_filter( 'image_editor_mime_mapping', '__return_false' ); 155 170 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); 156 171 157 172 foreach ( $classes as $key => $class ) { … … class Tests_Image_Functions extends WP_UnitTestCase { 172 187 'image/jpeg', 173 188 'image/gif', 174 189 'image/png', 190 'image/webp', 175 191 ); 176 192 177 193 // Test each image editor engine. … … class Tests_Image_Functions extends WP_UnitTestCase { 270 286 'jpe' => 'image/jpeg', 271 287 'gif' => 'image/gif', 272 288 'png' => 'image/png', 273 'unk' => 'image/jpeg', // Default, unknown. 289 'webp' => 'image/webp', 290 'unk' => 'image/jpeg', // Default, unknown. 274 291 ); 275 292 276 293 // Test each image editor engine. -
tests/phpunit/tests/image/intermediateSize.php
diff --git tests/phpunit/tests/image/intermediateSize.php tests/phpunit/tests/image/intermediateSize.php index b92c833a9d..08b3a015de 100644
5 5 * @group upload 6 6 */ 7 7 class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 8 /** 9 * Setup test fixture 10 */ 11 public function setUp() { 12 parent::setUp(); 13 add_filter( 'image_editor_mime_mapping', '__return_false' ); 14 } 15 8 16 function tearDown() { 9 17 $this->remove_added_uploads(); 10 18 … … class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 12 20 remove_image_size( 'false-height' ); 13 21 remove_image_size( 'false-width' ); 14 22 remove_image_size( 'off-by-one' ); 23 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 15 24 parent::tearDown(); 16 25 } 17 26 -
tests/phpunit/tests/image/resize.php
diff --git tests/phpunit/tests/image/resize.php tests/phpunit/tests/image/resize.php index 3e3255e6a5..cc66256769 100644
abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase 14 14 parent::setUp(); 15 15 16 16 add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) ); 17 add_filter( 'image_editor_mime_mapping', '__return_false' ); 18 } 19 20 public function tearDown() { 21 parent::tearDown(); 22 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 17 23 } 18 24 19 25 public function wp_image_editors() { -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index bc9b593d15..df755691ba 100644
class Tests_Media extends WP_UnitTestCase { 11 11 protected static $post_ids; 12 12 13 13 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 14 add_filter( 'image_editor_mime_mapping', '__return_false' ); 15 14 16 self::$_sizes = wp_get_additional_image_sizes(); 15 17 $GLOBALS['_wp_additional_image_sizes'] = array(); 16 18 … … class Tests_Media extends WP_UnitTestCase { 49 51 50 52 public static function wpTearDownAfterClass() { 51 53 $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; 54 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 52 55 } 53 56 54 57 public static function tearDownAfterClass() { -
tests/phpunit/tests/post/attachments.php
diff --git tests/phpunit/tests/post/attachments.php tests/phpunit/tests/post/attachments.php index 0a03d77ed6..ff3ba64541 100644
6 6 * @group upload 7 7 */ 8 8 class Tests_Post_Attachments extends WP_UnitTestCase { 9 /** 10 * Setup test fixture 11 */ 12 public function setUp() { 13 parent::setUp(); 14 add_filter( 'image_editor_mime_mapping', '__return_false' ); 15 } 9 16 10 17 function tearDown() { 11 18 // Remove all uploads. 12 19 $this->remove_added_uploads(); 20 remove_filter( 'image_editor_mime_mapping', '__return_false' ); 13 21 parent::tearDown(); 14 22 } 15 23