I have noticed that many WordPress theme developers use timthumb for resizing images in the theme. I do not understand why would they want to use a third party script when WordPress has a built-in function to do exactly the same thing. Also, the fact being timthumb had some vulnerabilities which were exploited to hijack the complete WordPress installs. Another reason for not using TimThumb would be it resizes the images on the fly.Yes, I know the cache is there, but still it’s not as reliable as cropping an image at the time of upload and serving it using the WordPress’s built-in functions.

A nifty function called add_image_size does all the magic. It has an option to resize the image with hard/soft cropping mode. It actually registers the “new image size” within WordPress, so whenever a new image is uploaded – along with the standard image resizes like thumbnail, medium, large it also resizes the image for the requested size. Neat eh ? So let’s see how it works –

All you have to do is, write the following function inside your theme’s function file i.e. functions.php. Also note that before calling add_image_size, we need to call add_theme_support which tells the WordPress that our theme needs the resizing support. Infact, this is supported by default in all modern themes and also in both bundled themes twentyten and twentyeleven.

// Code snippet from WordPress Codex
if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
	add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)

Notice that how the name is assigned to this particular image size. This name will be passed in the other functions to render this particular size image in the front-end.

What about the image which have already been uploaded, how do I resize them ?

WordPress has a solution to almost everything and for every problem there is a plugin to it. Hence, we have a plugin which regenerates all the different sizes for the “already uploaded” images as well. I have personally used Regenerate Thumbnails and it’s quite decent. These plugins can be slow depending upon your blog’s server. But everything is simple click-click, so it should not pain.

I hope you will avoid using timthumb or any other third party library to resize thumbnails atleast in WordPress. This is right and safest way to resize your images inside the WordPress. I am sure this is going to save lot of your headache, when used.

Stay Digified!!
Sachin Khosla

Share this post: