Skip to content

Commit d80c313

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55278 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0821c7a commit d80c313

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/wp-includes/class-wp-image-editor-imagick.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ public function make_subsize( $size_data ) {
503503
$size_data['crop'] = false;
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

508512
if ( is_wp_error( $resized ) ) {

tests/phpunit/tests/media.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,6 +3770,40 @@ public function test_quality_with_image_conversion_file_sizes() {
37703770
}
37713771
}
37723772

3773+
/**
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+
37733807
/**
37743808
* Add threshold to create a `-scaled` output image for testing.
37753809
*/

0 commit comments

Comments
 (0)