I have been a fan of the TimThumb script for some time now. It can be used as an alternative to using WordPress Featured Images to display and automatically resize images.
Recently there has been quite a lot of activity in TimThumbville. A security vulnerability was discovered which allowed third parties to upload and execute PHP code in the TimThumb cache directory.
Following the news of the vulnerability, the current TimThumb developer, Ben Gillbanks has been working to update it via the Google Code Project.
Enter WordThumb
Another development since the vulnerability was discovered is that the developer who discovered it, Mark Maunder, did a complete rewrite of the script and named it WordThumb.
“TimThumb is a simple, flexible, PHP script that resizes images. You give it a bunch of parameters, and it spits out a thumbnail image that you can display on your site.” — from the TimThumb Project PageThe Saga Continues
While I was actually writing this post, I discovered that The WordThumb project has now become TimThumb version 2.0, which makes more sense to me. We don’t need two scripts out there that do the exact same thing. So I would guess that the new 2.0 version of TimThumb has all the latest and greatest security enhancements and more.
How to Use TimThumb to add Thumbnails to Your Posts
What I like about the script is that is will resize images without having to re-upload the image, which is the case with WordPress Featured Images. I actually like both methods, but it depends on the site you’re working on as to which you will use.
If you have many posts with images, and you decide to add thumbnails to the posts on your home page, or maybe in the sidebar, then having to re-upload the images if you’re using WordPress Featured Images can be a problem.
So, what I’ve done in cases like this is to grab the first image in the post, then use TimThumb to resize the image and place it where I want.
How to Use the First Image in Your Posts with TimThumb
One little spin on using TimThumb is to have it automatically use the first image in your post. Now, keep in mind that TimThumb doesn’t have anything to do with this script I’m going to use to grab the first image, I’m just using it in conjunction with TimThumb for this purpose.
So, let’s say you want to add thumbnail images to the posts on your home page and you want to automatically resize them, and also use the first image in your post. I’m going to use some code I first discovered on the wprecipes.com blog which will grab the first image. Visit that site and grab the code, then paste it in your custom-functions.php file.
Now that we have the first image, let’s use TimThumb to display it at the desired size.
You will need to make sure you have installed TimThumb correctly. It’s really easy and there are instructions over on the TimThumb Project Homepage.
I will just add that you should save the file in a folder called “scripts” inside your Thesis custom folder. You should also create the two additional folders as explained in the instructions, called “cache” and “temp” and be sure you place them in the same folder as timthumb.php.
Now that you have it installed, paste this into your custom-functions.php file:
function thumb_images() {
global $post;
$img = '<img class="alignleft" src="' . THESIS_CUSTOM_FOLDER .'/scripts/timthumb.php?src='.catch_that_image($post).'&h=150&w=150&zc=1" alt="">';
echo $img;
}
add_action('thesis_hook_before_headline','thumb_images');
Here’s what’s happening, and some points to note:
- We are using the standard TimThumb syntax in the URL to resize and display the image, but we’re using the script that grabs the first image in the post and then passing that information to the TimThumb URL parameters.
- You can change the width (w) and height (h) values to anything you want. Keep in mind too that in this code, the timthumb.php file is saved in a folder called “scripts” inside the Thesis custom folder.
- I’m using the Thesis hook thesis_hook_before_headline to insert the image before the post headline.
- You can use other Thesis hooks to place the image wherever you want.
- I added the css class of alignleft which is already built into Thesis and which will float the image to the left. Again, you can change this class to anything you want.
- Please note that this code is written to work with Thesis, but you can adapt it to work in your non-Thesis based WordPress site.
What’s really nice about TimThumb is that let’s say you decide to change the size of the thumbnails, then all you do it update the width and height values, re-upload your custom-functions.php file, and kablammo, the images are resized.
So that does it. You now have it set up so your images are getting automatically resized by TimThumb and it’s using the first image in your post.
I’ve provided some links below for additional reading.
- Grab the first image from the post
- TimThumb Project Homepage
- TimThumb on Google Code
- TimThumb Security Vulnerability Discovered.
- WordThumb Project Merges with TimThumb.
Was this post helpful? Consider subscribing to my blog via RSS or








































{ 9 comments… read them below or add one }
Hi,
i have in line (06 $output = preg_match_all(‘//i’, $post->post_content, $matches); this error message (Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING)
somethink is wrong?
Hello,
i’ve just found the solution for my problem
$output = preg_match_all(‘//i’, $post->post_content, $matches);
i put only three \ in and now there is no error, but
when i put an image in my post, it doesn’t make a thumbnail.
By the way could you tell me how you make the thumbnails in Popular Posts and Recent Posts. Do you have a plugin for this? Or do you make it another way?
I hope hearing from you soon and give you a big compliment for your blog, very nice!
Thx and greetings
Kri
I checked the code now and it’s working for me. I uploaded an image and inserted it into the post. The code is then generating that thumbnail and it’s showing on the home page.
Make sure you have set the correct permissions on the temp and cache folders.
As to thumbnails for popular and recent posts, there’s probably a thumbnail out there which you can search for, but I’m using some custom code to display the posts. The code is something you can find in the WordPress codex and I’ll maybe do a post on it, but as far as the TimThumb thumbnails, you can incorporate this code anywhere you want. Just follow the basic syntax and rules.
Thanks for commenting.
Hi. You’re right. There was an error with that code. Some backslashes were being stripped out. I have been trying for the past fifteen minutes but they’re being stripped out every time.
I’m using the correct tags to display code as well as a syntax highlighter plugin but they keep getting stripped. No idea why as I’ve pasted code in many times on other posts.
I don’t have the time to mess with it right now but just visit this post and grab the code from there. For now I’ve just removed the incorrect code from this post. Thanks.
Thanks very much for your fast reply.
But i think i’ve made something wrong, because it don’t works for me. I googled all the time and don’t found a step by step declaration how to make it works.
I will leave you here the step’s i’ve done, may you can se where i have the mistake when you have a little bit time to reply i would be very happy
What i’ve done yet:
1. Copy the code from http://timthumb.googlecode.com/svn/trunk/timthumb.php and make a the .php file on my server in this order structure
/html/wp-content/themes/thesis_182/custom/scripts
and give the file the permisson 777
2. in the order scripts i also made 2 new orders named cache and temp and this two i give the permisson 777
3. im my order structure there is the custom_function.php like this
/html/wp-content/themes/thesis_182/custom/custom_functions.php
and there (in the custom_functions.php) i put that both code from you just copy and paste it (with the 3 backslashes) – no error appears any more
this file has the permisson 666
4. I made a new article, and then i put a picture as first in (with klick on the button”insert pic”) and then wrote my post in, shown like this:
An meinen Anfängen,……… i save the post and it shows the pic in the normal size
5. When i click the “Home” Button – the picture is shown in the same size like in the post – not thumbnailed.
So i may have a mistake but i don’t know where
… hope you could help me
Thanks and greetings
Kri
PS: i’ve found something on another site:
Once done, you can simply call the function within the loop to display the first image from the post:
is that maybe my mistake? i don’t know in which file i should put this tag, if the tag should be anywhere when using your version……
I am unable to open that image using the link you provided above. That could be the issue. The steps you’ve taken seem correct. Just make sure you upload the image using the WordPress image uploader inside the post edit screen. Right now, that images seems to not exist.
Hi there
Thanks for this assistance. It works well. However….
I did this on a site of mine but some of my posts don’t have images. The timthumb code is showing up on my category page whether there is an image or not.
In firefox that’s not a problem, but in IE it displays a box with a red X in it.
How can I make the code above conditional on there actually being an image to display?
Thanks a lot
Brett
Brett recently posted..Alive and Kicking #27 – Pain Tolerance in Martial Artists, a Breakthrough, and a Pep Talk
You could use a WordPress conditional tag to not run the script on the Category page. You can also set a default image if you don’t have an image in the post.
If you look at the ‘catch that image’ code, replace this line for the default image:
$first_img = get_bloginfo('template_url').'/custom/images/default.jpg';And if you delete that line entirely, it should do nothing and not insert any image.
Thanks for that. It’s actually the category page where I want this to display, but the problem is that some of my posts don’t have images.
But your response helped me. I went to the catch_that_mage part of the code, and when it was testing for the empty string I replaced the result with “NOIMAGE”.
Then in the timthumb part I conditionally tested for that text and only ran the script when it was not NOIMAGE. Typing this I now realize I only had to test for the default image path that catch_that_post used!
I’m not a coder, but I did happen to have a PHP book lying around so that saw me through.
Thanks
Brett
Brett recently posted..Forget 40. What About Martial Arts After 110!