TimThumb: Improved Speed and Other Changes

TimThumb is no longer supported or maintained.
More information →

Recently, when I haven’t been working on WPAdvent, I have been tweaking and improving TimThumb. In fact there’s been a couple of cool new features added that make it a much more useful peice of kit.

The big changes boil down to speed, simplicity, and Youtube!

Speed

I’ve always been very conscious of the speed of TimThumb. I know that resizing images is slow and uses a lot of memory, so caching has been in there from the start. However I thought I would spend a few days trying to speed it up even more and the changes made have increased the speed a lot more that I had imagined. Some of the things I did to speed TimThumb up include:

  • Move cache loading higher up in the code so that it does less processing if the cache file exists
  • Improve browser caching
  • Reduce the amount of regular expressions (processor intensive commands)
  • Reduce the amount of file requests (file_exists, file age etc) – server intensive commands
  • Do the cache cleaning after the image generation rather than before

All these add up to a script that is considerably faster, and uses considerably fewer server resources – so it should be a win for everyone.

Thanks go to indiepath, kieranjallen, and LeeWillis77 for their help with this.

Simplicity

When I was developing the offsite functionality I introduced a ‘temp’ directory requirement. At the time it was necessary as I wasn’t sure how the folder would be used, and was concerned there could be filename clashes. However Magnus from WooThemes mentioned that he’d tested it without and found there to be no problems – so it’s gone. The cache requirements now are a single folder. This should mean less debugging if there are problems. A simple change, but welcome none the less.

Youtube (beta!)

This one is a nice little easter egg. Lot’s of people have asked for Youtube video resizing and I thought it would be a cool thing to add. So now you can pass a Youtube video url to TimThumb as the image source and it will work out the video thumbnail and resize that.

This functionality has already been added to my Elemental WordPress theme framework so now if you embed a video in a post with no images then the post thumbnail will become the video thumbnail without any work from you.

An example of this is below – the link is for a festive Creature Comforts video.

timthumb.php?src=http://www.youtube.com/watch?v=9Ks8jtU5-fY&w=250&h=180

Which will give you:

[thumb width=”250″ height=”180″]http://www.youtube.com/watch?v=9Ks8jtU5-fY[/thumb]

I should point out that the Youtube functionality is in the beta stage. It’s in the active version of the script but I have only done light testing with it. If you have any problems then please do post a comment below and I will see if I can improve it some more.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

05 Aug 2010

Using TimThumb part 1: Getting Started

TimThumb has always been built with simplicity in mind. However there are a few things it can do that have not been exposed before.Inspired by a comment from RBhavesh I have decided to write a series of posts in which...
04 Nov 2010

TimThumb Troubleshooting Secrets

I often get asked questions about TimThumb and why it doesn’t work in certain situations. I can generally tell what is wrong with the script within about 60 seconds of being sent a demo url. Below are my top tips...
01 Jul 2014

I No Longer Use TimThumb – Here’s What I do Instead

Last week there was a second exploit found in TimThumb. Thankfully it was no-where near as bad as the first one – but it raised an interesting question of whether TimThumb is even needed anymore.TimThumb was made to be useful...
06 Jul 2009

A Brief History of TimThumb

When we were building Mimbo Pro – Darren came up with the idea of automating the image thumbnail resizing – and this was the idea that sparked the development of TimThumb.This was all part of making the theme site as...
11 Oct 2009

How to Make TimThumb Work With WordPress Multisite

TimThumb is a popular image resizing script that was created for Mimbo Pro – but it’s never worked properly with WordPress MU, so I wanted to change that.The reason it doesn’t work is quite simple. Because of the way WordPress...