Using phpThumb to auto-generate thumbnail images

What is phpThumb

“phpThumb() uses the GD library to create thumbnails from images (JPEG, PNG, GIF, BMP, etc) on the fly. The output size is configurable (can be larger or smaller than the source), and the source may be the entire image or only a portion of the original image. True color and resampling is used if GD v2.0+ is available, otherwise paletted-color and nearest-neighbour resizing is used. ImageMagick is used wherever possible for speed. Basic functionality is available even if GD functions are not installed (as long as ImageMagick is installed).” – http://phpthumb.sourceforge.net

phpThumb allows for the following functionality:

  • A watermark image or text can be applied over the thumbnail in one of 10 positions or tiled.
  • A border or frame can be applied.
  • Thumbnails can be a fixed dimension regardless of source aspect ratio and background filled with configurable color. Border corners can optionally be rounded (independant horizontal & vertical radius).
  • Unsharp Mask sharpening.
  • Alpha channel mask from mask file.
  • Auto Contrast / Levels.
  • Bevel edge.
  • Add Blur.
  • Change Brightness.
  • Colorize.
  • Alter Contrast.
  • Drop shadow.
  • Edge Detect
  • Flip Horizontal / Vertical.
  • Change Gamma
  • Grayscale Images.
  • Mean Removal
  • Negative Color
  • Reduce Color Depth
  • Rounded Corners
  • Saturation
  • Sepia
  • Smooth
  • Threshold
  • White Balance

There is a nice demo site where you can see what can be done with phpThumb here.

Using phpThumb to create dynamic thumbnail images

Lets say we are building a portfolio website for a photographer and our client wants the ability for new uploaded photos to be automatically displayed on the front page. The client wants visitors to be able to click on the image thumbnail and view the original image in high quality, but he does not want to create seperate thumbnail images for every photo he uploads.

Step 1: Installing phpThumb

Installing is a bit of a strong word, pretty much all you need is to do is go to http://phpthumb.sourceforge.net/, download the phpThumb library, extract it, and move it to a “phpThumb” folder in your site’s root folder.

Step 2: Setting up the htaccess file

Create a sub folder in the images directory, for this exercise the folder we will be creating is called “gallery”.

We now have a www.yoursite.com/phpthumb and a www.yoursite.com/images/gallery folder on the server; the “/phpthumb” directory contains your phpThumb library and the “/images/gallery” folder contains a bunch of images we want to have thumbnails generated for.

Next, we update the htaccess file to allow for “clean” URLs. Open the .htaccess file in your site root in an editor and add the following lines:

<IfModule mod_rewrite.c>
  # turn mod_rewrite on:
  RewriteEngine On
  # create an url path for thumbnails with a width of 200 pixels
  RewriteRule ^images/gallery/thumbs/(.*)$ /phpthumb/phpThumb.php?src=images/gallery/$1&w=200&aoe=1
</IfModule>

What this will do is cause Apache to “rewrite” URL requests from www.yoursite.com/images/gallery/thumbs/[some_image_file] to the phpThumb library at www.yoursite.com/phpthumb/phpThumb.php with the actual image path attached as a GET variable.

Once you have made the neccessary changes to the htaccess file, you can upload an image to your /images/gallery folder. For testing purposes we will upload a file named “test.jpg”, a 2000px by 2000px photo.

Open your browser and go to www.yoursite.com/images/gallery/thumb/test.jpg, you should see a thumbnail of test.jpg with a width of 200 pixels.

This means that we can now just use the thumbnail URL (images/gallery/thumb) for the thumbnails and the normal URL (images/gallery) for viewing the original images.

Final Thoughts

You can do a lot more than just resize images using phpThumb, take a look at the Parameter List at http://phpthumb.sourceforge.net/demo/docs/phpthumb.readme.txt and see for yourself.

phpThumb has the ability to overlay other images, crop images, add borders, add shadows, change color tones, and even change the image format between JPEG, GIF, PNG, and (if you have ImageMagick enabled on your server) BMP.

Tags: , , , , , , ,

No comments yet.

Leave a Reply