WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 5 months ago

#29370 new defect (bug)

wp_get_attachment_image() returning unexpected image size

Reported by: martinleopold Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.2
Component: Media Keywords:
Focuses: Cc:
PR Number:

Description

I have a 'thumbnail' size image of 150x150 px and a 'thumb-250' size image of 166x250 px.

add_image_size('thumb-250', 0, 250, false);

I am using wp_get_attachment_image() to get the best image fit for a 133x200 format.

wp_get_attachment_image( $id, array(133,200) );

this returns the 'thumbnail' size which has an area that's smaller than the requested size, as well as a different aspect ratio. I would expect the 'thumb-250' size image to be returned, which has a bigger area and the exact aspect ratio that was requested.

I tracked this down to the first condition in the following line:
https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/media.php#L599

Why is the 'thumbnail' image size preferred in the selection process? This means the thumbnail is always returned if it's next in row even if it's area is smaller and the aspect ratio is off.

Change History (2)

#1 @martinleopold
5 years ago

this persists to wordpress 4.1:
https://core.trac.wordpress.org/browser/tags/4.1/src/wp-includes/media.php#L609

if ( 'thumbnail' != $_size && ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) )

which should be, imho:

if ( ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) )
Note: See TracTickets for help on using tickets.