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:
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.
Thank you very much for this update!
The new TimThumb is flying, have not expected such a big boost!
December 7, 2010 • @dumitru
awesome - that's really good to hear!
December 7, 2010
That's great! Can't wait to test out the youtube feature.
December 8, 2010
men, that is super awesome of you to include the auto thumbnail for youtube,
thanks a lot
December 10, 2010 • @bebek_ijo
no problem - glad you like the feature
December 10, 2010
Nice work! I use TimThumb quite a lot and I can only agree with Dumitru — this thing flies!
December 10, 2010 • @ufoeyes
Wow, I got here because of the external images issue (the version in the theme I was using was apparently OLD). The speed in this latest version is absolutely phenomenal. Must be some kind of quantum magic
December 28, 2010 • @wwday3
December 30, 2010
Great tool thank you oh so much! I need help with getting the program to delete the cached files. Any help would be great and thanks inadvance.
Below are the setting in timThumb.php:
// number of files to store before clearing cache
define ('CACHE_SIZE', 30);
// maximum number of files to delete on each cache clear
define ('CACHE_CLEAR', 10);
// use the cache files? (mostly for testing)
define ('CACHE_USE', TRUE);
// version number (to force a cache refresh)
define ('VERSION', '1.19');
// cache directory
define ('DIRECTORY_CACHE', './cache');
January 6, 2011
TimThumb should clear the cache files automatically, you don't need to do anything.
January 7, 2011
Ben please review this line in the code.
Cache-Control: max-age=864000
This number = 240 HOURS.
Should it be 86400 which = 24 hours
January 22, 2011
Very cool! The youtube thing is the best, holy crap, I had no idea you could do that! I... must... kiss you now... no no, don't fight it, it will only make things worse for you... lol
January 10, 2011 • @NancyMRamirez
lol - glad you find it useful
January 13, 2011
Hi...quick question.
I have tested the file and it works great with small and medium pictures but when I try with big high resolution pictures the script doesn't work.
I was wondering if you can post some tips regarding PHP.ini, server conf, hosting, etc.....i mean, what would be the best practice for thumbs.....Anyone?
January 20, 2011
With large images you are likely to hit a php memory limit. The best solution is not to resize large pictures. I've certainly not had any big problems with resizing images so they must be really hefty.
I don't have any recommended specifications I am afraid. I have always just used the default settings with no complaints.
January 20, 2011
thanks for your quick response....
By the way, this is a class that I used to use...
http://www.mywebmymail.com/...ent/easyphpthumbnail-class
now i am planing to switch to your script.
January 20, 2011
Large images no problem. I'm using 1920pxx1080px size images.
Yes, I'm on a virtual private server. My ISP defaults to 11Meg php memory limit.
I'm using TimThumb to resize the above for full browser window background images.
This works in all modern browers including iPhone, iPad etc. to detect the browser window height and width. Yes, it's javascript.
pagewidth=document.documentElement.clientWidth;
pageheight=document.documentElement.clientHeight;
I append that to the URL and read that with php to let TimThumb do it's thing.
<?php
$w=$_GET['width'];
$h=$_GET['height'];
$string="";
echo($string);
?>
Work in progress is at :
http://www.downtownsanclemente.com/client4/
January 20, 2011
Large images no problem.
<?php
$w=$_GET['width'];
$h=$_GET['height'];
$string="";
echo($string);
?>
January 20, 2011
I need help with getting the program to delete the cached files.!
March 10, 2011 • @martinvaresio
Hello,
How to apply for Youtube TimThumb?
April 18, 2011
Just use the latest version of TimThumb from Google Code. To use it, pass the youtube video url as the src parameter
April 18, 2011
What changed in the code:
ID, 'thumb', true) ) { ?>
<a href="" rel="bookmark" title="Permanent Link to "><img class="fotnew" src="/timthumb.php?src=ID, "thumb", $single = true); ?>&h=75&w=75&zc=1" alt="" width="75" height="75" />
April 18, 2011
Sorry:
<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img class="fotnew" src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=75&w=75&zc=1" alt="<?php the_title(); ?>" width="75" height="75" /></a>
<?php } ?>
April 18, 2011