How to make the Get The Image WordPress plugin to work with TimThumb and Related posts
I’ve been using the wordpress plugin ‘Get the Image‘ by developer Justin Tadlock for a while. ‘Get the image’ does a fantastic job searching through posts looking for images, so you don’t have to add thumbnails manually, but it always bothered me that it downscaled images in the html instead of scaling and cropping them properly. I also use the lovely timThumb script on my blog, to scale, scrop and cache images on the fly, and I wanted to find a way to combine the two scripts into one.
The wordpress community is so huge, so I was certain that someone, somewhere, would have written about this, but to my surprise I couldn’t find any solution to this that didn’t involve writing a new plugin or adding code to functions.php. But I did find alot of people looking for the same as me.
But after taking a look at the source code to ‘Get the image’, I found the solution myself, and to my great joy there is no need to modify the plugin, which would have meant that it would break every time I’d upgrade ‘Get the image’. The trick is to use ‘format’ => ‘array’ to make ‘Get the Image’ return an array instead of a finished img tag. This option is not mentioned in the readme included with the ‘Get the Image’ plugin, for some weird reason, but you can see it defined in the code, defaulting to ‘format’ => ‘img’ . When returning the array, you can get hold of the url to the image, and with that you can build a you timthumb url.
After a few minutes of fiddling, I came up with the method I’m now running in the sidebar of pappmaskin.no to display the last 5 posts posted in my photography category (cat=3), and I wanted to share it with people looking for a fix. Here is the code:
Bonus: How to make the Get The Image WordPress plugin to work with Related Posts (YARPP)
Once you have Get The Image up and running, you can do even more interesting things, like adding thumbnails to your related posts. You can see it in action on the bottom of this article. There are numerous plugins for listing related posts, but I’ve been using Releated Posts (YARPP) off and on for a few years, and think It works greate. It doesn’t have support for thumbnails out of the box, but that is easy to fix, especially if you have Get the Image and timthumb up and running.
YARPP support templates, so create yarpp-template-post-thumbnail.php and put it in your theme folder. Then add the following code:
Issues and things to be aware of
1. You can put timthumb anywhere on your server, but I noticed that it came bundled with the ‘WordPress popular posts‘ plugin. I use that plugin in the sidebar so instead of installing timthumb again, I simply point to the same script. ‘WordPress popular posts’ is a really useful and well written plugin, and it also has the same functionality as the ‘Get the image’ plugin buildt in.
2. Timthumb can only scale images that are stored on the same server, for security and cpu reasons. There are other scripts out there for sucking in external images, scale and cache them, like Autothumb, but I haven’t had time to look at it yet.
3. Because timthumb demands an image stored on the same server, and because of the order ‘Get the image’ scans for images in a post, I ran into a litte problem with the default thumbnail size in the WordPress media settings. I wanted the images in the sidebar to be 220 pixels wide, but because I had set the wordpress thumbnail size to 183px wide the images that are show in the sidebar are too small. Get the images automatically looks for images attached to a post, and when I upload an image, wordpress automatically scales a thumbnail of it set to 183px wide, so that is the image ‘Get the image’ delivers. To solve this I’ve changed the media settings on my blog to generate an image that is 220 px, but that means I have to go in manually and eighter upload a new image (and remove earlier images), or add an image url to a customfield (the default custom fields is «thumbnail» I believe, but you can define your own).
4. I believe timthumb caches 200 images and then start deleting older thumbs. But ‘Get the image’ doesnt seem to have any methods to reduse the stress on the server. That means that every time you list 5 posts like I do, you have to scan the posts again and again and again, every time someone visits your blog, and if you use the plugin in many places in combination with timthumb, chances are that you have to scale the thumbnails over and over again as well. I have found a couple of people who’ve made a fix for this, storing the url to the thumbnail as a custom field, and setting up get the image to scan for this customfield first. if found, there is no need to scan any further. I think I will look into improving the logic there at a later date.Posted on: 27. mars 2010, by : Morten Skogly