diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 3c675dd05d957..d5ed29d67ec43 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1818,7 +1818,7 @@ function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = // Is it a full size image? if ( - isset( $image_meta['file'] ) && + isset( $image_meta['file'], $image_meta['width'], $image_meta['height'] ) && str_contains( $image_src, wp_basename( $image_meta['file'] ) ) ) { $dimensions = array( diff --git a/tests/phpunit/tests/media/wpImageSrcGetDimensions.php b/tests/phpunit/tests/media/wpImageSrcGetDimensions.php new file mode 100644 index 0000000000000..f3e107a149e34 --- /dev/null +++ b/tests/phpunit/tests/media/wpImageSrcGetDimensions.php @@ -0,0 +1,95 @@ +attachment->create_object( + array( + 'file' => '2024/10/image.svg', + 'post_mime_type' => 'image/svg+xml', + ) + ); + } + + /** + * Tests that `wp_image_src_get_dimensions()` returns the full size dimensions from the image meta. + * + * @ticket 62312 + */ + public function test_should_return_full_size_dimensions() { + wp_update_attachment_metadata( + self::$attachment_id, + array( + 'file' => '2024/10/image.svg', + 'width' => 640, + 'height' => 480, + ) + ); + + $this->assertSame( + array( 640, 480 ), + wp_image_src_get_dimensions( + 'http://example.org/wp-content/uploads/2024/10/image.svg', + wp_get_attachment_metadata( self::$attachment_id ), + self::$attachment_id + ) + ); + } + + /** + * Tests that `wp_image_src_get_dimensions()` returns false without raising warnings + * when the full size image meta is missing width or height. + * + * @ticket 62312 + * + * @dataProvider data_should_return_false_when_full_size_dimensions_are_missing + * + * @param array $image_meta The image meta data. + */ + public function test_should_return_false_when_full_size_dimensions_are_missing( $image_meta ) { + wp_update_attachment_metadata( self::$attachment_id, $image_meta ); + + $this->assertFalse( + wp_image_src_get_dimensions( + 'http://example.org/wp-content/uploads/2024/10/image.svg', + wp_get_attachment_metadata( self::$attachment_id ), + self::$attachment_id + ) + ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_return_false_when_full_size_dimensions_are_missing() { + return array( + 'width and height missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + ), + ), + 'width missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + 'height' => 480, + ), + ), + 'height missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + 'width' => 640, + ), + ), + ); + } +}