Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
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!
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.
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.
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.
Which will give you:
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.