diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
index 139d405..e85c20f 100644
a
|
b
|
function wp_read_image_metadata( $file ) { |
259 | 259 | if ( ! file_exists( $file ) ) |
260 | 260 | return false; |
261 | 261 | |
262 | | list( , , $sourceImageType ) = getimagesize( $file ); |
| 262 | $sourceImageType = wp_get_image_type( $file ); |
263 | 263 | |
264 | 264 | /* |
265 | 265 | * EXIF contains a bunch of data we'll probably never need formatted in ways |
… |
… |
function wp_read_image_metadata( $file ) { |
430 | 430 | } |
431 | 431 | |
432 | 432 | /** |
| 433 | * Get the type of an image. |
| 434 | * |
| 435 | * This is the constant value for the image type, pass to |
| 436 | * image_type_to_mime_type() to get the mime type. |
| 437 | * |
| 438 | * @param string $path File path to the image |
| 439 | * @return int|false Image Type constant value for the image type, false on failure |
| 440 | */ |
| 441 | function wp_get_image_type( $path ) { |
| 442 | if ( is_callable( 'exif_imagetype' ) ) { |
| 443 | return @exif_imagetype( $path ); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * In the case of exif_imagetype not being available, we use the |
| 448 | * slower more network heavy getimagesize |
| 449 | */ |
| 450 | $size = @getimagesize( $file ); |
| 451 | |
| 452 | if ( ! $size ) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | return $size[2]; |
| 457 | } |
| 458 | |
| 459 | /** |
433 | 460 | * Validate that file is an image. |
434 | 461 | * |
435 | 462 | * @since 2.5.0 |
… |
… |
function file_is_valid_image($path) { |
453 | 480 | function file_is_displayable_image($path) { |
454 | 481 | $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); |
455 | 482 | |
456 | | $info = @getimagesize( $path ); |
457 | | if ( empty( $info ) ) { |
458 | | $result = false; |
459 | | } elseif ( ! in_array( $info[2], $displayable_image_types ) ) { |
460 | | $result = false; |
461 | | } else { |
462 | | $result = true; |
463 | | } |
| 483 | $result = in_array( wp_get_image_type( $path ), $displayable_image_types ); |
464 | 484 | |
465 | 485 | /** |
466 | 486 | * Filter whether the current image is displayable in the browser. |
diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
index e3fb353..90d3c0c 100644
a
|
b
|
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { |
2181 | 2181 | } |
2182 | 2182 | |
2183 | 2183 | // We're able to validate images using GD |
2184 | | if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) { |
| 2184 | if ( $type && 0 === strpos( $type, 'image/' ) ) { |
2185 | 2185 | |
2186 | | // Attempt to figure out what type of image it actually is |
2187 | | $imgstats = @getimagesize( $file ); |
| 2186 | $image_mime_type = wp_get_image_type( $file ); |
2188 | 2187 | |
2189 | | // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME |
2190 | | if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) { |
| 2188 | // If the real MIME doesn't match the claimed MIME |
| 2189 | if ( $image_mime_type != $type ) { |
2191 | 2190 | /** |
2192 | 2191 | * Filter the list mapping image mime types to their respective extensions. |
2193 | 2192 | * |
… |
… |
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { |
2204 | 2203 | ) ); |
2205 | 2204 | |
2206 | 2205 | // Replace whatever is after the last period in the filename with the correct extension |
2207 | | if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) { |
| 2206 | if ( ! empty( $mime_to_ext[ $image_mime_type ] ) ) { |
2208 | 2207 | $filename_parts = explode( '.', $filename ); |
2209 | 2208 | array_pop( $filename_parts ); |
2210 | | $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ]; |
| 2209 | $filename_parts[] = $mime_to_ext[ $image_mime_type ]; |
2211 | 2210 | $new_filename = implode( '.', $filename_parts ); |
2212 | 2211 | |
2213 | 2212 | if ( $new_filename != $filename ) { |
diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php
index 9cc3f09..ed03810 100644
a
|
b
|
class Tests_Image_Functions extends WP_UnitTestCase { |
351 | 351 | remove_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) ); |
352 | 352 | WP_Image_Editor_Mock::$save_return = array(); |
353 | 353 | } |
| 354 | |
| 355 | public function test_wp_get_image_type() { |
| 356 | |
| 357 | $image_paths = array( |
| 358 | DIR_TESTDATA . '/images/test-image.jpg' => IMAGETYPE_JPEG, |
| 359 | DIR_TESTDATA . '/images/test-image.png' => IMAGETYPE_PNG, |
| 360 | DIR_TESTDATA . '/images/test-image.tiff' => IMAGETYPE_TIFF_MM, |
| 361 | DIR_TESTDATA . '/images/test-image.jp2' => IMAGETYPE_JP2, |
| 362 | DIR_TESTDATA . '/images/test-image.bmp' => IMAGETYPE_BMP, |
| 363 | DIR_TESTDATA . '/images/test-image.gif' => IMAGETYPE_GIF, |
| 364 | ); |
| 365 | |
| 366 | foreach ( $image_paths as $image_path => $type ) { |
| 367 | // use getimagesize as a comparison, as it needs to be compatibile |
| 368 | $info = getimagesize( $image_path ); |
| 369 | $this->assertEquals( $info[2], wp_get_image_type( $image_path ), 'getimagesize() returns different result for ' . $image_path ); |
| 370 | |
| 371 | $this->assertEquals( $type, wp_get_image_type( $image_path ), 'Image type incorrect for ' . $image_path ); |
| 372 | } |
| 373 | |
| 374 | $this->assertEquals( false, wp_get_image_type( '/some/broken/path' ) ); |
| 375 | } |
354 | 376 | } |