[wp-trac] [WordPress Trac] #44299: Cropped images have wrong ratio
WordPress Trac
noreply at wordpress.org
Sun Jun 3 11:53:52 UTC 2018
#44299: Cropped images have wrong ratio
--------------------------+-----------------------------
Reporter: tschortsch | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 4.9.6
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
There seems to be a problem in the `image_resize_dimensions()` function
when `$crop` is set to `true`.
Here's an example:
I defined 3 cropped sizes with the same ratio (1:1):
- large-crop: 1250x1250
- medium-crop: 1000x1000
- small-crop: 500x500
When I upload an image with a size of 1250x1000px I get the following
sizes:
{{{
[large-crop] => Array
(
[file] => 1250x1000-1250x1000.png
[width] => 1250
[height] => 1000
[mime-type] => image/png
)
[medium-crop] => Array
(
[file] => 1250x1000-1000x1000.png
[width] => 1000
[height] => 1000
[mime-type] => image/png
)
[small-crop] => Array
(
[file] => 1250x1000-500x500.png
[width] => 500
[height] => 500
[mime-type] => image/png
)
}}}
As you can see the large-crop size gets generated with a wrong ratio
(5:4). When I now load this size `wp_get_attachment_image_srcset(
$attachment_id, 'large-crop' )` the `srcset` gets calculated with other
images that have a 5:4 ratio (if existing). I would expect to get a
`srcset` of images with a 1:1 ratio.
When I upload an image with a size of 1100x1000px (smaller than large-
crop) something similar happens. The `large-crop` doesn't get generated at
all. When I load the image with this size `wp_get_attachment_image_srcset(
$attachment_id, 'large-crop' )` it falls back to the original size (since
the size is not available). The original image has a ratio oh 11:10 which
means the calculated `srcset` consists of images with a wrong ratio.
I tracked this problem down to the `image_resize_dimensions()` function
and created a possible fix for it here:
https://github.com/rokka-io/rokka-wordpress-plugin/blob/fix-srcset-
calculation/src/class-wp-crop-bugfix.php#L57
I also created some unit tests which shows the original behaviour and the
behavior after the bugfix:
https://github.com/rokka-io/rokka-wordpress-plugin/blob/fix-srcset-
calculation/tests/test-wp-crop-bugfix.php
--
Ticket URL: <https://core.trac.wordpress.org/ticket/44299>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list