A lot of themes, especially the magazine layout themes that have become so popular, utilize a script called timthumb. Timthumb exists to dynamically alter the size, and cropping of an image and became popular prior to the WordPress 2.9 addition of “Post thumbnails”.
This script solved a problem when there was no other solution and has significantly aided the advent of new theme concepts over the years.
The problem, though, is that images are static content and should be loaded in a static fashion. If PHP has to be loaded into memory to generate an image, there will be a performance hit that is non-trivial and noticeable when many images are present.
Fortunately, with Post Thumbnails, the need to use timthumb is either eliminated or drastically reduced.
Here’s how to do it.
Inside your wp-admin, open up the Settings menu and click on the Media page. Here, set your image sizes accordingly.
Of course, these are just default sizes. You can also add specialty sizes as well. To do this, add the following to your theme’s functions.php.
[cc lang=”php”]
add_image_size( ‘long-and-wide’, 500, 50, false );
[/cc]
This will create a new image size called ‘long-and-wide’ that is 500 pixels wide, 50 pixels tall and is not cropped. (For more on image sizes, see the WordPress documentation). Of course, you can call your image size anything you want and make it any size you want. This is just an example.
Then, depending on where your theme is invoking the timthumb script, you can replace with code similar to this:
[cc lang=”php”]
if( has_post_thumbnail() )
{
the_post_thumbnail(‘long-and-wide’);
}
[/cc]
This technique uses built in WordPress APIs and reduces the overhead on the servers and page load time and should be used whenever possible.
yalamber subba says
What about the condition when we need zooming or croping?
James Cochrane says
I was searching for a solution for one of the members of Wealthy Affiliate (WA) who was distressed at the fact that hosting at WA did not allow the Tomthumb script and disables it. But the member wanted to know a workaround for it and I found this solution here at WP Engine. It seems like it will fix the situation for him and I have forwarded the post. I found it to be very helpful.
Best Regards,
Jim
Austin Gunter says
Awesome! That’s great to hear, James. Thanks for the post!