Friday, January 14, 2011

TimThumb, Host Gator and Absolute (External) URLs - Click here http://bit.ly/f4CHBi for full article

If you’ve ever played with or developed an advanced WordPress theme, you surely have come across the wonderful timthumb.php PHP script. It was developed specifically for WordPress and makes the dreaded task of image resizing a real joy.

"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," according to BinaryMoon.

It does exactly what it promises to do and the results are simply magnificent. It's definitely comparable to ImageMagick’s powerful shell options.

Unfortunately, we don’t live in a perfect world, and now and then, we have to be creative to overcome certain obstacles.

I recently encountered two of these obstacles: Host Gator hosting and WordPress's use of absolute URLs when adding new images to the media library. When combined, instead of a pretty resized image, you get an ugly error message telling you that the resource/URL is not available or that you don’t have access and have been reported to the FBI. (Okay, maybe not quite that bad but darn close!)

In this instance, it has nothing to do with permissions (which could be an issue, as well). The reason for the error is TimThumb’s effort to resize an image referenced by an absolute URL (starting with http://www...).

In my case, the URL was on the same server but Host Gator’s settings prevented TimThumb from accessing it.

Here’s a simple solution that costs you five seconds and will save you a lot of frustration: When referencing an image inside your page or post that might require resizing for various display purposes, change the location of the image from a URL to a path (I am using an absolute path to circumvent issues with relative paths, as those depend on your folder structure).

Before: http://www.mydomain.com/wordpress/images/myimage.jpg
After: /wordpress/images/myimage.jpg

Take it from a (WordPress) developer: incorporate this practice into your content development and you will save yourself a lot of time and headaches.

No comments:

Post a Comment