Update: I don’t use Timthumb script anymore because it has security vulnerability. So please use it with your own risk.
TimThumb is a small open source library which is aimed to provide resized copies of given images. TimThumb is developed by Ben Gillbanks which is firstly create this script for the WordPress themes Mimbo Pro. It handles cropping, zooming and resizing web images in several formats such as jpg, png, and gif. It’s very useful and simple enough.
Follow the guide below to integrate timthumb to your WordPress blog:
- Grab the timthumb.php script from Google code.
- Put it under your themes folder. For example my themes folder is “codemaniac”, i put it in wp-content/themes/codemaniac/tools/timthumb.php.
- To use the timthumb.php you have to use custom field in your every post. I’m using name “thumb”. Read more about WordPress custom field here.
- Now use the code below to use timthumb:
<?php if( get_post_meta($post->ID, "thumb", true) ): ?> <img class="thumb" width="50" height="50" src="<?php bloginfo('template_directory'); ?>/tools/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=50&w=50&zc=1" alt="<?php the_title(); ?>" /> <?php endif; ?>
explanation: the code above will find whether post meta name “thumb” is exist for existing post. If it exist then show the output image from timthumb. timthumb.php?src=/path/to/image.png&width=50&height=50&zc=1 will output image.png with 50×50 pixels in size.
Timthumb have a very good cache mechanism, that’s why this is very handy and less resource consuming script. Even it is already a good and handy script, there are still possible ways to tweak it for more efficient and faster.
Tweak timthumb script:
- Image quality. This will use to compress png image files with PHP functiom imagepng. The default compression is 5. You can increase it to 9. Change this code:
$quality = preg_replace("/[^0-9]+/", '', get_request('q', 50));
$quality = preg_replace("/[^0-9]+/", '', get_request('q', 100));
- Increase “CACHE_SIZE”, the default is 250. Increase it to 1000. Most of the hosting server give unlimited space, or very large space. By increase it to 1000 files in cache folder will increase the performance a little bit, as it have larger cache space.
- Increase expires header. Find this code:
$gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +10 days')) . ' GMT';
$gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +30 days')) . ' GMT';
it will increase the Expire header to 1 month. So the image file will cache in user browser for 1 month.