Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,24 +774,28 @@ function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
$imagedata['width'] = $imagedata['sizes']['full']['width'];
}

foreach ( $imagedata['sizes'] as $_size => $data ) {
foreach ( $imagedata['sizes'] as $_size => $size_data ) {
if ( ! is_array( $size_data ) || ! isset( $size_data['width'] ) || ! isset( $size_data['height'] ) ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_array() isn't needed here as the isset() is checking it.

continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silently skipping isn't preferred here. Why?

wp_get_attachment_metadata() returns an array on success and 'sizes' is expected to be an array. If it's not an array, then something went wrong. Silently skipping makes it harder for developers to debug what happened and could lead to confusion for users.

}

// If there's an exact match to an existing image size, short circuit.
if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
if ( (int) $size_data['width'] === (int) $size[0] && (int) $size_data['height'] === (int) $size[1] ) {
$candidates[ $size_data['width'] * $size_data['height'] ] = $size_data;
break;
}

// If it's not an exact match, consider larger sizes with the same aspect ratio.
if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
if ( $size_data['width'] >= $size[0] && $size_data['height'] >= $size[1] ) {
// If '0' is passed to either size, we test ratios against the original file.
if ( 0 === $size[0] || 0 === $size[1] ) {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] );
$same_ratio = wp_image_matches_ratio( $size_data['width'], $size_data['height'], $imagedata['width'], $imagedata['height'] );
} else {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] );
$same_ratio = wp_image_matches_ratio( $size_data['width'], $size_data['height'], $size[0], $size[1] );
}

if ( $same_ratio ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
$candidates[ $size_data['width'] * $size_data['height'] ] = $size_data;
}
}
}
Expand Down