Make WordPress Core

Ticket #50866: 50866.2.diff

File 50866.2.diff, 3.6 KB (added by costdev, 18 months ago)

Patch refreshed against trunk.

  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index a0e7ef9c6c..7fbe889cde 100644
    a b function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
    775775                }
    776776
    777777                foreach ( $imagedata['sizes'] as $_size => $data ) {
     778                        // If the original image matches the required size, use it instead of a generated size and short circuit.
     779                        if ( (int) $imagedata['width'] === (int) $size[0] && (int) $imagedata['height'] === (int) $size[1] ) {
     780                                $candidates[ $data['width'] * $data['height'] ] = array(
     781                                                'width'  => $imagedata['width'],
     782                                                'height' => $imagedata['height'],
     783                                                'file'   => wp_basename( $imagedata['file'] ),
     784                                );
     785                                break;
     786                        }
     787
    778788                        // If there's an exact match to an existing image size, short circuit.
    779789                        if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) {
    780790                                $candidates[ $data['width'] * $data['height'] ] = $data;
    function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
    817827                // Constrain the width and height attributes to the requested values.
    818828                list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
    819829
     830        } elseif ( (int) $imagedata['width'] === $imagedata['sizes'][ $size ]['width'] && (int) $imagedata['height'] === $imagedata['sizes'][ $size ]['height'] ) {
     831                $data = array(
     832                                'width'  => $imagedata['width'],
     833                                'height' => $imagedata['height'],
     834                                'file'   => wp_basename( $imagedata['file'] ),
     835                );
    820836        } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
    821837                $data = $imagedata['sizes'][ $size ];
    822838        }
  • 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..e69de29bb2
    diff --git a/tests/phpunit/data/images/test_500x500.png b/tests/phpunit/data/images/test_500x500.png
    new file mode 100644
    index 0000000000..e69de29bb2
    diff --git a/tests/phpunit/tests/image/intermediateSize.php b/tests/phpunit/tests/image/intermediateSize.php
    index 830359427a..fc2f9d6088 100644
    a b class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 
    1515                parent::tear_down();
    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 { 
    8888                }
    8989        }
    9090
     91        /**
     92         * @ticket 50866
     93         * @ticket 47713
     94         */
     95        function test_get_intermediate_size_with_same_as_original_size() {
     96                add_image_size( 'test-size', 100, 200, true );
     97
     98                $file = DIR_TESTDATA . '/images/test_100x200.png';
     99                $id   = $this->_make_attachment( $file, 0 );
     100
     101                // Look for a size by name.
     102                $image = image_get_intermediate_size( $id, 'test-size' );
     103
     104                // Test that WordPress returns the original image path and not an intermediate size
     105                $this->assertSame(
     106                        $image['file'],
     107                        'test_100x200.png',
     108                        'did not return original before removing image size'
     109                );
     110
     111                $image = image_get_intermediate_size( $id, [100, 200] );
     112
     113                // Cleanup.
     114                remove_image_size( 'test-size' );
     115
     116                // Test that WordPress returns the original image path and not an intermediate size
     117                $this->assertSame(
     118                        $image['file'],
     119                        'test_100x200.png',
     120                        'did not return original after removing image size'
     121                );
     122        }
     123
    91124        /**
    92125         * @ticket 17626
    93126         * @requires function imagejpeg