[wp-trac] [WordPress Trac] #34384: No way of preventing image_get_intermediate_size from returning cropped image

WordPress Trac noreply at wordpress.org
Wed Jul 13 14:14:43 UTC 2016

#34384: No way of preventing image_get_intermediate_size from returning cropped
 Reporter:  pputzer                               |       Owner:  joemcgill
     Type:  defect (bug)                          |      Status:  accepted
 Priority:  normal                                |   Milestone:  4.6
Component:  Media                                 |     Version:  3.0
 Severity:  normal                                |  Resolution:
 Keywords:  has-patch has-unit-tests 2nd-opinion  |     Focuses:
Changes (by joemcgill):

 * keywords:  has-patch dev-feedback => has-patch has-unit-tests 2nd-opinion
 * status:  reviewing => accepted


 I took some time to refactor the internals of
 `image_get_intermediate_size()`  in [attachment:34384.2.diff] to make the
 logic much cleaner and address a few concerns at once.

 First, this moves the test for aspect ratios into a new helper function,
 `wp_image_matches_ratio()`, which is the same logic used in
 `wp_calculate_image_srcset()`. Now, when we're evaluating available sizes
 against an array of dimensions, we only add a size to our `$candidates`
 array if the ratio matches. A side affect of this optimization, is that we
 also fix #34980 since we wouldn't have two different image sizes with
 different dimensions but the same aspect ratio and surface area.

 If we end up with no sizes matching the requested aspect ratio, then we
 fall back to the `thumbnail` size. This still doesn't explicitly keep a
 divergent ratio from being returned, as requested by @pputzer, but the
 filter should suffice if we end up falling back to the 'thumbnail' size,
 which is required to maintain backwards compatibility.

 Another side affect of this cleanup is that since we no longer return
 `$data` at different places in the logic tree, we are always able to
 return the `$path` and `$url` instead of only when a named size is passed.
 The downside would be for anyone who was passing `$sizes` as an array in
 order to avoid this calculation for performance reasons.

 We already have decent test coverage for `image_get_intermediate_size()`
 and this adds an additional test case to make sure an array of dimensions
 doesn't return the `thumbnail` size when another size matches the same
 aspect ratio. If this breaks other edge cases, we may need to add
 additional unit tests.

Ticket URL: <https://core.trac.wordpress.org/ticket/34384#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list