Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#34980 closed defect (bug) (fixed)

image_get_intermediate_size() sometimes skips image sizes

Reported by: flixos90's profile flixos90 Owned by: joemcgill's profile joemcgill
Milestone: 4.6 Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: has-patch needs-unit-tests
Focuses: Cc:


Sometimes it can happen that an image size is not considered in image_get_intermediate_size() although it applies for the array.

This is because the product of the image width and height is used as array key, however there are cases where different image sizes could share the same product (for example 150x300 and 200x225).

Attachments (1)

34980.diff (3.1 KB) - added by flixos90 9 years ago.
Fixes the bug by allowing multiple sizes of the same width*height

Download all attachments as: .zip

Change History (7)

9 years ago

Fixes the bug by allowing multiple sizes of the same width*height

#1 @flixos90
9 years ago

  • Keywords has-patch added

#2 @swissspidy
9 years ago

  • Keywords needs-unit-tests added

#3 @joemcgill
8 years ago

  • Milestone changed from Awaiting Review to 4.6
  • Owner set to joemcgill
  • Status changed from new to reviewing

Yup, this is indeed a bug. Will need unit tests, but am moving to 4.6 for consideration.

#4 @joemcgill
8 years ago

If we end up going with something like 34384.2.diff to fix #34384, we should end up avoiding this issue.

This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.

8 years ago

#6 @joemcgill
8 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 38086:

Media: Prevent image_get_intermediate_size() from returning cropped images.

When $size is passed to image_get_intermediate_size() as an array of width
and height values and an exact image size matching those values isn't available,
the function loops through the available attachment sizes and returns the
smallest image larger than the requested dimensions with the same aspect ratio.

The aspect ratio check is skipped for the 'thumbnail' size to provide a fallback
for small sizes when no other image option is available. This resulted in a poor
selection when the size requested was smaller than the 'thumbnail' dimensions
but a larger size matching the requested ratio existed.

This refactors the internals of image_get_intermediate_size() to ensure the
'thumbnail' size is only returned as a fallback to small sizes once all other
options have been considered, and makes the control flow easier to follow.

This also introduces a new helper function, wp_image_matches_ratio() for
testing whether the aspect ratios of two sets of dimensions match. This function
is also now used in wp_calculate_image_srcset() during the selection process.

Props flixos, joemcgill.
Fixes #34384, #34980.

Note: See TracTickets for help on using tickets.