How does add_image_size work? Not all featured images cropped


I have this:

add_theme_support( 'post-thumbnails' );
add_image_size( 'mycustomsize', 600, 360, true );

Shouldnt this crop ALL featured images that use ‘mycustomsize’ to the correct ratio? If I for example upload an image that is 600x300… wouldnt this be cropped on the width a bit so that it get the correct ratio? Or would it only be cropped to this if the uploaded image is… 800x600 or something?

EDIT: Okey so i did some testing and yeah it seems like its only cropping the height/width value that is MORE than 600 or 360. Can I extend this function so that the images always get the correct ratio of 600X360 even if let say the image i upload is 444x400 ?

Basically what I am looking for is something like add_image_ratio rather then size , does something like this exist?

As far as I know there’s no native way of doing this. I think the idea is you crop/resize to save bandwidth, not necessarily to squeeze images to perfect ratios. You’d use CSS for that.

However, if you still want to size up, WordPress uses a filter called image_resize_dimensions() which you can hook into. I’m not sure what the specific code would be, but it would look something like this:

add_filter('image_resize_dimensions', function(null, $oldWidth, $oldHeight, $newWidth, $newHeight){
	return array(...);

I would just go the CSS route.

1 Like

Okey, thanks for the info, will go CSS then=)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.