Use get/the_post_thumbnail with Custom Size & Crop

We know that the_post_thumbnail() outputs the thumbnail & that the_post_thumbnail( array(100,100) ) outputs the thumbnail at a specified size.

And we know that we can use add_image_size( 'category-thumb', 300, 9999, $crop ); to add a custom image size which we can call using the_post_thumbnail('category-thumb') with scaling and cropping applied.

Is there a way to use the_post_thumbnail to crop an image when it’s displayed?

set_post_thumbnail_size could help, but I’m asking specifically if there is a way to do this with the_post_thumbnail

This is relevant: the_post_thumbnail scaling not hard cropping

( if nothing’s changed the answer is no )

1 Answer
1

No, there’s not a way to do this by using the_post_thumbnail(), neither it’s recommended.

What you are looking for is cropping the images on the fly. This will cost heavy server resources, especially when you have a lot of images to work with.

But in theory, to do so, you can make a script that takes an image as input, crops the image using PHP, and then returns the image data without saving the actual file. But again, it’s expensive.

Leave a Comment