Media: Improve handling of non-image files in wp_get_image_mime.

This prevents non-image fileypes from returning a mime type of "application/octet-stream" when `exif_imagetype()` returns `false`.

Props blobfolio.
Fixes #40017.

Merges [40397] to the 4.7 branch.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40403 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2017-04-10 14:27:23 +00:00
parent e86ab4f969
commit 9e500a9b06
2 changed files with 49 additions and 1 deletions

View File

@ -2366,7 +2366,8 @@ function wp_get_image_mime( $file ) {
*/
try {
if ( is_callable( 'exif_imagetype' ) ) {
$mime = image_type_to_mime_type( exif_imagetype( $file ) );
$imagetype = exif_imagetype( $file );
$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
} elseif ( function_exists( 'getimagesize' ) ) {
$imagesize = getimagesize( $file );
$mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;

View File

@ -899,6 +899,18 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertEquals( $uuids, $unique_uuids );
}
/**
* @ticket 40017
* @dataProvider _wp_get_image_mime
*/
public function test_wp_get_image_mime( $file, $expected ) {
if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) {
$this->markTestSkipped( 'The exif PHP extension is not loaded.' );
}
$this->assertEquals( $expected, wp_get_image_mime( $file ) );
}
/**
* @ticket 39550
* @dataProvider _wp_check_filetype_and_ext_data
@ -977,6 +989,41 @@ class Tests_Functions extends WP_UnitTestCase {
return $mimes;
}
/**
* Data profider for test_wp_get_image_mime();
*/
public function _wp_get_image_mime() {
$data = array(
// Standard JPEG.
array(
DIR_TESTDATA . '/images/test-image.jpg',
'image/jpeg',
),
// Standard GIF.
array(
DIR_TESTDATA . '/images/test-image.gif',
'image/gif',
),
// Standard PNG.
array(
DIR_TESTDATA . '/images/test-image.png',
'image/png',
),
// Image with wrong extension.
array(
DIR_TESTDATA . '/images/test-image-mime-jpg.png',
'image/jpeg',
),
// Not an image.
array(
DIR_TESTDATA . '/uploads/dashicons.woff',
false,
),
);
return $data;
}
public function _wp_check_filetype_and_ext_data() {
$data = array(
// Standard image.