Make WordPress Core

Ticket #28077: 28077.2.diff

File 28077.2.diff, 4.7 KB (added by joehoyle, 9 years ago)
  • src/wp-admin/includes/image.php

    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 ) { 
    259259        if ( ! file_exists( $file ) )
    260260                return false;
    261261
    262         list( , , $sourceImageType ) = getimagesize( $file );
     262        $sourceImageType = wp_get_image_type( $file );
    263263
    264264        /*
    265265         * EXIF contains a bunch of data we'll probably never need formatted in ways
    function wp_read_image_metadata( $file ) { 
    430430}
    431431
    432432/**
     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 */
     441function 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/**
    433460 * Validate that file is an image.
    434461 *
    435462 * @since 2.5.0
    function file_is_valid_image($path) { 
    453480function file_is_displayable_image($path) {
    454481        $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP );
    455482
    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 );
    464484
    465485        /**
    466486         * Filter whether the current image is displayable in the browser.
  • src/wp-includes/functions.php

    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 ) { 
    21812181        }
    21822182
    21832183        // 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/' ) ) {
    21852185
    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 );
    21882187
    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 ) {
    21912190                        /**
    21922191                         * Filter the list mapping image mime types to their respective extensions.
    21932192                         *
    function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { 
    22042203                        ) );
    22052204
    22062205                        // 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 ] ) ) {
    22082207                                $filename_parts = explode( '.', $filename );
    22092208                                array_pop( $filename_parts );
    2210                                 $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
     2209                                $filename_parts[] = $mime_to_ext[ $image_mime_type ];
    22112210                                $new_filename = implode( '.', $filename_parts );
    22122211
    22132212                                if ( $new_filename != $filename ) {
  • tests/phpunit/tests/image/functions.php

    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 { 
    351351                remove_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) );
    352352                WP_Image_Editor_Mock::$save_return = array();
    353353        }
     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        }
    354376}