WordPress.org

Make WordPress Core

Ticket #50866: 50866.1.patch

File 50866.1.patch, 3.8 KB (added by Mista-Flo, 5 months ago)
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index e1091b9d8e..a1c29ba73f 100644
    a b function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
    777777                }
    778778
    779779                foreach ( $imagedata['sizes'] as $_size => $data ) {
     780                        // If the original image match the required size, use it instead of a generated size and short circuit.
     781                        if ( (int) $imagedata['width'] === (int) $size[0] && (int) $imagedata['height'] === (int) $size[1] ) {
     782                                $candidates[ $data['width'] * $data['height'] ] = array(
     783                                                'width'  => $imagedata['width'],
     784                                                'height' => $imagedata['height'],
     785                                                'file'   => wp_basename( $imagedata['file'] ),
     786                                );
     787                                break;
     788                        }
     789
    780790                        // If there's an exact match to an existing image size, short circuit.
    781                         if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) {
     791                        if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) {
    782792                                $candidates[ $data['width'] * $data['height'] ] = $data;
    783793                                break;
    784794                        }
    function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
    819829                // Constrain the width and height attributes to the requested values.
    820830                list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
    821831
     832        } elseif ( (int) $imagedata['width'] === $imagedata['sizes'][ $size ]['width'] && (int) $imagedata['height'] === $imagedata['sizes'][ $size ]['height'] ) {
     833                $data = array(
     834                                'width'  => $imagedata['width'],
     835                                'height' => $imagedata['height'],
     836                                'file'   => wp_basename( $imagedata['file'] ),
     837                );
    822838        } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
    823839                $data = $imagedata['sizes'][ $size ];
    824840        }
  • tests/phpunit/tests/image/intermediateSize.php

    diff --git a/tests/phpunit/data/images/test_100x200.png b/tests/phpunit/data/images/test_100x200.png
    new file mode 100644
    index 0000000000..ed0b6f898d
    Binary files /dev/null and b/tests/phpunit/data/images/test_100x200.png differ
    diff --git a/tests/phpunit/data/images/test_500x500.png b/tests/phpunit/data/images/test_500x500.png
    new file mode 100644
    index 0000000000..c0d6e82432
    Binary files /dev/null and b/tests/phpunit/data/images/test_500x500.png differ
    diff --git a/tests/phpunit/tests/image/intermediateSize.php b/tests/phpunit/tests/image/intermediateSize.php
    index c2ac785058..c3561a1ff9 100644
    a b class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 
    1515                parent::tearDown();
    1616        }
    1717
    18         public function _make_attachment( $file, $parent_post_id = 0 ) {
     18        public function _make_attachment( $file = '', $parent_post_id = 0 ) {
    1919                $contents = file_get_contents( $file );
    2020                $upload   = wp_upload_bits( wp_basename( $file ), null, $contents );
    2121
    class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 
    8585                $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
    8686        }
    8787
     88        /**
     89         * @ticket 50866
     90         * @ticket 47713
     91         */
     92        function test_get_intermediate_size_with_same_as_original_size() {
     93                add_image_size( 'test-size', 100, 200, true );
     94
     95                $file = DIR_TESTDATA . '/images/test_100x200.png';
     96                $id   = $this->_make_attachment( $file, 0 );
     97
     98                // Look for a size by name.
     99                $image = image_get_intermediate_size( $id, 'test-size' );
     100
     101                // Test that WordPress returns the original image path and not an intermediate size
     102                $this->assertSame( $image['file'], 'test_100x200.png' );
     103
     104                $image = image_get_intermediate_size( $id, [100, 200] );
     105
     106                // Cleanup.
     107                remove_image_size( 'test-size' );
     108
     109                // Test that WordPress returns the original image path and not an intermediate size
     110                $this->assertSame( $image['file'], 'test_100x200.png' );
     111        }
     112
    88113        /**
    89114         * @ticket 17626
    90115         */