WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#44299 closed defect (bug) (invalid)

Cropped images have wrong ratio

Reported by: tschortsch Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.6
Component: General Keywords:
Focuses: Cc:

Description

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

Change History (7)

#1 @sebastien@…
3 years ago

Hello @tschortsch and thanks for your ticket.
Seems not to ben any bug here.

Your original pictures is 1000px width... how possible to have 250px not existing pics?

Try the same with a largest img.

#2 @tschortsch
3 years ago

Of course this happens only when I upload images which are smaller than the crop size. The problem is I can't force our clients to always upload images which are bigger or the same as the biggest size. And since we rely on focus areas which crop the images exactly where it makes most sense the cropping in the defined ratio is important.

#3 @codestyling
3 years ago

What about fillup missing space at the insufficient h/v size like black bars in cinema tv format?

#4 @tschortsch
3 years ago

Hmm I think that doesn't really solve my problem since my clients are uploading the images and not all of them are able to do that. On the other side I crop the images that I can avoid something like a void (or in your case black) area.

#5 @sebastien@…
3 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Hello All,

The Trac is to correct Core Bug. It doesn't seems here to be the case.

Please use the forums to solve your problem.

https://wordpress.org/support/

#6 @tschortsch
3 years ago

When I crop an image to a specific ratio and get an image which doesn't have this ratio I see this as a bug. Isn't this the point of cropping? But fine for me I can use the described filter to fix the problem.

#7 @netweb
3 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.