Use And Tweak timthumb Script For WordPress Blog

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:

  1. Grab the timthumb.php script from Google code.
  2. Put it under your themes folder. For example my themes folder is “codemaniac”, i put it in wp-content/themes/codemaniac/tools/timthumb.php.
  3. 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.
  4. 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); ?>&amp;h=50&amp;w=50&amp;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:

  1. 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));
  2. 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.
  3. Increase expires header. Find this code:
    $gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +10 days')) . ' GMT';

    change to:

    $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.


  1. Thank Ivan.. excellent post

  2. rajkanuri says:

    Awesome, you are my savior. have fixed most of the things

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.