Make WordPress Core

Changeset 55278


Ignore:
Timestamp:
02/07/2023 05:22:41 PM (2 years ago)
Author:
danielbachhuber
Message:

Media: Bail early if image is already the requested size.

In WP_Image_Editor_Imagick, bail early in make_subsize() if the image is already the requested size. Previously, make_subsize() would create another copy of the file. WP_Image_Editor_GD doesn't have the same problem.

Props wojtekn, danielbachhuber.
Fixes #57370.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r54226 r55278  
    504504        }
    505505
     506        if ( ( $this->size['width'] == $size_data['width'] ) && ( $this->size['height'] == $size_data['height'] ) ) {
     507            return new WP_Error( 'image_subsize_create_error', __( 'The image already has the requested size.' ) );
     508        }
     509
    506510        $resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    507511
  • trunk/tests/phpunit/tests/media.php

    r55171 r55278  
    37723772
    37733773    /**
     3774     * Test that an image size isn't generated if it matches the original image size.
     3775     */
     3776    public function test_wp_generate_attachment_metadata_doesnt_generate_sizes_for_150_square_image() {
     3777        $temp_dir = get_temp_dir();
     3778        $file     = $temp_dir . '/test-square-150.jpg';
     3779        copy( DIR_TESTDATA . '/images/test-square-150.jpg', $file );
     3780
     3781        $attachment_id = self::factory()->attachment->create_object(
     3782            array(
     3783                'post_mime_type' => 'image/jpeg',
     3784                'file'           => $file,
     3785            )
     3786        );
     3787
     3788        $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
     3789        $this->assertEquals(
     3790            array(),
     3791            $metadata['sizes']
     3792        );
     3793        $this->assertEquals(
     3794            'test-square-150.jpg',
     3795            basename( $metadata['file'] )
     3796        );
     3797        $this->assertEquals(
     3798            '150',
     3799            $metadata['width']
     3800        );
     3801        $this->assertEquals(
     3802            '150',
     3803            $metadata['height']
     3804        );
     3805    }
     3806
     3807    /**
    37743808     * Add threshold to create a `-scaled` output image for testing.
    37753809     */
Note: See TracChangeset for help on using the changeset viewer.