Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#45449 closed defect (bug) (invalid)

Switched thumbnail scaling + cropping output using add_image_size

Reported by: sicco's profile sicco Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: Cc:


I have added the following to my functions.php in order to create thumbnails of 660x250 which are allowed to crop:

add_image_size( 'square-large-xs-sm', 660, 250, true);

This works fine for some images, but it gives the wrong result for others. E.g., if I add an image of size 2048x1365 it will result in a thumbnail of 375x250 instead of 660x250. Then if I add an image of 1636x1050 it will result in a thumbnail of the correct 660x250 size.

My theory is that the 1636x1050 can be scaled based on both the width and the height (of course without losing aspect ratio) as you end up with an image larger than 660x250 in both cases, so you can then just crop the remainder from the image. But with the 2048x1365 image just must scale the width to 660 which results in an image of 660x440 and then the height of 440px is cropped to 250px. If you first scale to a height of 250px you'll end up with an image of 375x250, where the width of 375px is thus already too small compared to the wanted 660px.

NOTE: as I specified at the start of this post, I actually end up with an image of 375x250, so it seems that the wrong scaling is applied to my image (based on height instead of width). To make it even more interesting. If I add an image that needs to actually be scaled based on height instead of width (so the opposite of the case I just described), it will end up scaling based on width! So it seems that function tries to determine whether to use width or height for scaling, but then ends up using the wrong one!

Am I correct, or am I totally confusing things?

Change History (4)

#1 @sicco
5 years ago

  • Summary changed from Switched thumbnail scaling + cropping output to Switched thumbnail scaling + cropping output using add_image_size

#2 @irfanbajwa
5 years ago

Hi @sicco
First of all, we welcome you on WordPress Core Track.
I have tested this case at my end and I am unable to reproduce it.its working fine with image that have dimension 2048x1365 and produce 600*250 thumbnail.
add_image_size( 'square-large-xs-sm', 660, 250, true);
when you use this code on function.php file. where you want to show this it must be as the_post_thumbnail('square-large-xs-sm');

For test i used this image
and after this these thumbnail generated by WordPress

#3 @sicco
5 years ago

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

Thanks for the quick reply! This made me look better at what was going on and I noticed my theory is wrong. PNG and GIF files were processed correctly, but JPG files weren't. In the end it turned out that libjpeg-dev was not installed, so I installed it, compiled gd with it (has to specify the correct path) and restarted php-fpm. It works now :D.

#4 @ocean90
5 years ago

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