My solution for image thumbnails in PHP

A very common dilemma I come across as a PHP developer is how I want to handle creating thumbnails for lots of images. Sure, I could use some software and make thumbnails, but I'm talking about bigger projects that need to handle it for thousands of photos. I have been down the path many times of using PHP GDI+ and writing a function to do resizing. A couple of years ago I got fancier and made some proportiate resizing functions so that those pesky pictures people take holding their cameras vertically don't have to be squished into a pre-determined width of 100px. However, each and every time I do a project like this I always think how messy it is. If you have 50 thousand photos, you will have 100 thousand files once you pre-make all those thumnails. And what if the client comes back next year and wants a different size, or you decide to do something in-between sizes for another use? Heck, what if you update a photo, you have to remember to do the thumbnail. Don't forget to remove them too, or you are just creating a new project for yourself 5 years later when you are running low on disk space and backups take forever because of the 10 gazillion thumbnails left rotting on your system that link to nothing.

My new approach to doing thumbnails is to not do them at all. That's not quite what I mean, but I have really been sold on the idea of dynamically generated thumbnails. There is very little performance hit if it is done correctly and I have been extremely happy with the results so far. My development time has been reduced and I never worry about wanting to add more sizes later, changing / updating existing thumbs. Even better, I can remotely host images and pull them in doing the thumbnail version on the fly.

This was very useful to me in revamping a real estate web site design project my company, Vermilion Design Group, built many years ago. I actually removed nearly 60 thousands photos from the site and off-loaded only the good ones to a single site that can be shared for all of my real estate web site customers.

My new method involves two items. First, I use phpThumb(), an open source thumbnail generator from http://phpthumb.sourceforge.net/. I really like this solution because it uses the most efficient method available to generate thumbnails. It determines the available memory when creating images, so massive bulk conversions won't crash and burn. It also chooses the best option depending on if you have ImageMagic installed on your server or GDI+, etc... Finally, I am really impressed that it can do all of this work on remote images, just as if they were local.

Implementing phpThumb was very easy. I put it in a folder and then call it just like using a normal img tag. The trick is you can add all kinds of parameters to the end of it:

The above will show myphoto.jpg from the images folder and on the fly proportionally resize it to 400px wide.

In addition to incorporating phpThumb for image manipulation, I needed a quick solution for determing if an image existed before showing it. The fast way of course is if the image is stored on the same site, you could do:

if (file_exists("images/myphoto.jpg")) {

}

However, the really cool part is using it with remote images. You can do this many ways, but I chose to remain in PHP and test to see if I get a header back from the image. In the next example, I am calling a remote image to be 400px wide, but only if it exists, that way I could show a friendly photo if it wasn't there. In my case, these photos were of real estate houses, so I didn't know for any 1 listing whethere there would be 1 or 15 photos.

function url_exists($url) {
$hdrs = @get_headers($url);
return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;
}

if (url_exists("http://www.othersite.com/myphoto.jpg")) {
<img src="phpThumb.php?src=http://www.othersite.com/myphoto.jpg&w=400">
}

While there are many options for handling thumbnails and a variety of tools, I think this combination is a very fast, comprehensive solution to the problem. If you find this helpful, I encourage you to post any additional solutions or any corrections to the comments.

As always, if you would like to hire me for a web development project, please visit www.vermiliondesign.com



Read and post comments | Send to a friend

Comments

Popular posts from this blog

Apple TV - Recover from nothing Take 2

Convert a LiveCycle Form back to an Acrobat Form

Converting a MySQL database to SQLite on a Mac