Using Timthumb with Custom URLs for Site Optimization

Using Timthumb with Custom URLs for Site Optimization

Tutorial Details
  • Program: WordPress
  • Difficulty: Intermediate
  • Estimated Completion Time: 30 minutes

In this article, I will share on how to get a better page speed score even when you are using TimThumb as your thumbnail resizer for your site. On my Personal Blog I’ve been using this method for quite some time and the result is quite impressive. Let’s begin the tutorial!


Step 1 Preparing TimThumb

First! Older versions of timthumb are notoriously unsecure… So if you have an old version of timthumb, please first download the latest version from TimThumb Google Code. Open the files and look for line 27

define ('FILE_CACHE_DIRECTORY', './cache'); 

and replace it with

define ('FILE_CACHE_DIRECTORY', ''); 

This makes for a more secure setup, but you can still use the folder ‘cache’ or your own defined name. Hackers and bots know to look for that cache folder, which in older versions would have users set the folder permissions to lower than safe levels.


Step 2 Setting Up New Location for TimThumb

Normally, a theme developer will use timthumb within their theme folder; This is obviously to help users easily use the feature out of the box, but in the interest of security we will change the location for the timthumb to a new folder or to a new subdomain (I use this option on my personal blog). I’ll show you both methods:


Step 2.1 Using a Subfolder

Create new folder “media” on your main domain, ie: yourdomain.com/media

After that, put inside the folder “media” a .htaccess file with code shown below.


RewriteEngine On
RewriteBase /media/
RewriteRule ^resizer/(.*)x(.*)/r/(.*) resizer/thumb.php?src=http://$3&h=$2&w=$1&zc=1

Next, add a subfolder under “media” named “resizer”, ie: your-domain.com/media/resizer/. Upload the timthumb to this folder and make sure you name your timthumb file as thumb.php. The file structure will be like shown below

  1. /media
  2. /media/.htaccess
  3. /media/resizer/
  4. /media/resizer/thumb.php

If you enable the cache folder, you need to create the cache folder under “resizer”.


Step 2.2 Using a Subdomain

First you need to setup your subdomain, for example www3.your-domain.com.

After that, put inside the main folder for your subdomain a .htaccess file with code shown below.


RewriteEngine On
RewriteBase /
RewriteRule ^resizer/(.*)x(.*)/r/(.*) resizer/thumb.php?src=http://$3&h=$2&w=$1&zc=1

Next, add a subfolder “resizer”, ie: www3.your-domain.com/resizer/. Upload the timthumb to this folder and make sure you name your timthumb file as thumb.php. The file structure will be like shown below

  1. /.htaccess
  2. /resizer/
  3. /resizer/thumb.php

If you enable the cache folder, you need to create the cache folder under “resizer”.


Step 3 Usage

After you done with step 2, now you ready to use the timthumb with a custom url. The format you can use for the new custom url is shown below:

Subfolder

http://your-domain.com/media/resizer/250x150/r/your-image-url.jpg

Subdomain

http://www3.your-domain.com/resizer/250x150/r/your-image-url.jpg

The format use for the url is http://www3.your-domain.com/resizer/[image-width]x[image-height]/r/[image-url]

  1. [image-height] – define the height of the thumbnail
  2. [image-width] – define the width of the thumbnail
  3. [image-url] – define the url of the image source, remove http:// from the url, or else the thumbnail generation will failed

Step 4 Usage with Automagic Thumbnail/Image Management

My previous article was about Automagic post thumbnail/image management, if you want to use this custom url feature together with the post thumbnail management, please follow the steps below, there some editing to ensure everything works properly.

First you need to add extra function to your functions.php file.

function remove_http($url = '')
        {
                if ($url == 'http://' OR $url == 'https://'){
                        return $url;
                }
                $matches = substr($url, 0, 7);
                if ($matches=='http://'){
                        $url = substr($url, 7);         
                }else{
                        $matches = substr($url, 0, 8);
                        if ($matches=='https://') 
                        $url = substr($url, 8);
                }
                return $url;
        }

After that, look into the function get_attachment_picture(), before the closing bracket, you will see the code as shown below:

echo $related_thumbnail;

Change the code to

    echo remove_http($related_thumbnail);

After that, you can use the custom url together with the Post Thumbnail/image Management function. Example of usage:

echo '';

Conclusion

By now you should be able to use this function in any of your themes, if you have any additional suggestions or questions regarding custom url for timthumb, feel free to leave a comment!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://360signals.com Maor

    For me Timtumb does not exist anymore. It was the reason my WP site was hacked last summer and I’m not thrilled to deal with it again. Besides, it has no advantages over WP’s thumbnails API. Please use proofs if you think otherwise.

    • Salvador

      I dont like it either. The only reason I know its useful is for image sliders the default wordpress post_thumbnail() doesn’t really work. I know this from experience sorry to hear about your site.

    • Ben

      Hopefully this stays formatted.

      I’m bumping into issues with the way WordPress manages images. I am building an eCommerce site. Let’s say I put 1000 products on that site and I want to have 5 different images for each product.

      1000 products
      x 5 images
      ——–
      5000 images
      x 4 image sizes (thumb,medium,large,original)
      ——–
      20,000 total images not including random pics for the site design.

      1000 products = 1000 posts/pages/etc
      + 5000 extra posts because one is created for each image
      ———
      6000 total posts

      1000 post
      x 10 custom fields
      ———
      10,000 entries in _postmeta

      So, that’s:

      20,000 images
      6,000 posts
      10,000 _postmeta

      That is a lot of wasted space and database

      TimThumb Version
      6,000 images (1000 products X 5 sizes = 5000 + 1000 thumbnails = 6000)
      1,000 posts (no need for attachment posts)
      10,000 _postmeta (no change)

      All this is assuming that we don’t have more products than that. We will actually have way more than that and the size of that database and the storage needed just to work with WordPress’ image management will be enormous. I want to use wordpress and keep everything in-house, but I might have to opt out because of some of these issues. I can’t disable attachment posts being made and I can’t decide what folders my images will be in. They will either all be in one folder or in dated folders which wouldn’t make semantic sense in this setup. While TimThumb might not give everything back, at least it doesn’t add more.

      There is something to be said for using a tool like TimThumb. More in this case than many others. If it isn’t secure enough, maybe it can be adjusted to make it more secure. WordPress is currently stuck in how they do things and nobody seems to care about it.

      I would be happy to hear of any alternatives.

  • http://www.sennza.com.au/ Bronson Quick

    I definitely wouldn’t encourage usage of Tim Thumb for image resizing seeing WordPress has functions that do resizing for you and they’ll be future proof.

    Use http://codex.wordpress.org/Function_Reference/add_image_size to automatically resize the image and http://codex.wordpress.org/Function_Reference/the_post_thumbnail to display the image.

    • http://peterwilson.cc Peter Wilson

      I second Bronson on this.

      Another advantage of using the WordPress API is images are resized on upload, so your they’ll load faster for your visitors.

      • mikemick

        One BENEFIT of Timthumb would be that you only create the images you need. I have some sites that have over 15 different post image sizes. Lot’s of times, I’ll only really need one or two image sizes. That can become a pretty big waste of space. If you are concerned about storage space, then TimThumb can help.

        With that said, I removed Timthumb from my workflow when they had the security exploit about a year or so ago.

        • mikemick

          (Sorry, my comment was nested one level too deep)

  • http://wp.envato.com/ Japh

    A few people have commented about using WordPress built-in functions instead of TimThumb. While I agree that it’s certainly preferable, I have come across the occasional time where it just wasn’t possible to use the built-in functions, and had to use TimThumb instead. Seeing as there are some times where it’s unavoidable, it’s a great to know the best way to do it without killing your site’s performance :)

    • Brandon Jones

      Ditto – It’s usually a super rare situation, but I’ve had 2 or 3 themes in the last year that I built that really benefited from the flexibility of timThumb. Case in point: If you’re trying to build a dynamic image size system where users have full control on the backend to control the thumbnail sizes (for instance), it makes a lot more sense to use timThumb and pass in the Width/Height as variables from the database (theme options table) as opposed to registering a new image size or using other built in WP stuff. BUT, as WP grows, it gets easier and easier to just use the built in functions. Either way, for now, timThumb definitely has some practical uses for the creative themer ;)

      • http://www.etyhadar.com Ron

        Brandon – Good example for using TT rather than the WP image handeling (which I prefer using like many commenters here)

  • http://4up2date.info @mrhaw

    Have you gone illuminati? ;) LOL

  • http://sombokit.com LAO Sopheak

    Great! Thanks author!

  • Mike

    I have this code in the page that generates Timthumb. How do I change it to be a new Subdomain?

    <a href="” rel=”bookmark” title=”">

  • http://callthetricks.com/ sudeep

    I tried in sub domain but still the url remained same. It did not worked for me.