On-The-Fly Dynamic Thumbnails with PHP
I hate thumbnails. There. I've said it. Well... I suppose hate is a strong word... but I seriously strongly dislike them. I guess it's not the thumbnails themselves I hate... it's the extra work involved. Open up the image, crop, resize, save, upload, not right? rinse, lather, repeat until right.
To save myself the time (and sanity), I compiled this script... (I say compiled because it uses bits and pieces of other scripts i've seen over the past few years, with my own changes and additions) that dynamically creates a thumbnail. The script is called as follows:
<img src='/dfn_thumbnailer.php&img=/images/my_images.jpg&mw=150&mh=150' />
What the script does is take three $_GET arguments, img, mw, and mh, and returns a jpeg image. The main difference between this script and the others I've found online is that it doesn't just resize the image, it resamples it. This makes a world of difference, as simply resized images will appear pixelated.
<? # ---------------------------------------------------------------------- # DFN Thumbnailer # http://www.digifuzz.net # digifuzz@gmail.com # ---------------------------------------------------------------------- # Constants $IMAGE_BASE = "http://your.base.url.here"; $image_file = $_GET['img']; $MAX_WIDTH = $_GET['mw']; $MAX_HEIGHT = $_GET['mh']; global $img; # No Image? No go. if( !$image_file || $image_file == "" ) { die( "NO FILE FOUND."); } # if no max width is set, set one. if( !$MAX_WIDTH || $MAX_WIDTH == "" ) { $MAX_WIDTH="150"; } # if not max height is set, set one. if( !$MAX_HEIGHT || $MAX_HEIGHT == "" ) { $MAX_HEIGHT="150"; } # Get image location $image_path = $IMAGE_BASE . $image_file; # Load image $img = null; $ext = strtolower(end(explode('.', $image_path))); if ($ext == 'jpg' || $ext == 'jpeg') { $img = @imagecreatefromjpeg($image_path); } else if ($ext == 'png') { $img = @imagecreatefrompng($image_path); } else if ($ext == 'gif') { # Only if your version of GD includes GIF support $img = @imagecreatefromgif($image_path); } # If an image was successfully loaded, test the image for size if ($img) { # Get image size and scale ratio $width = imagesx($img); $height = imagesy($img); $scale = min($MAX_WIDTH/$width, $MAX_HEIGHT/$height); # If the image is larger than the max shrink it if ($scale < 1) { $new_width = floor($scale*$width); $new_height = floor($scale*$height); # Create a new temporary image $tmp_img = imagecreatetruecolor($new_width, $new_height); # Copy and resize old image into new image imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($img); $img = $tmp_img; } } # Create error image if necessary if (!$img) { $img = imagecreate($MAX_WIDTH, $MAX_HEIGHT); imagecolorallocate($img,255,255,255); $c = imagecolorallocate($img,255,0,0); imageline($img,0,0,$MAX_WIDTH,$MAX_HEIGHT,$c2); imageline($img,$MAX_WIDTH,0,0,$MAX_HEIGHT,$c2); } # Display the image header("Content-type: image/jpeg"); imagejpeg($img,'',500); ?>
This script is a real time saver on my end. It saves me from having to write a bunch more code, and handle more file-uploads for my clients. For example, I recently implemented this script for a client who runs an online shoe store. Rather than having to upload 3 different images for every shoe (and get the image sizes right), he just uploads one. Doesn't matter what size it is, because it will be resized and resampled to fit on the fly. Saves on disk space too.
A few final words about the script - if you can't already tell just by looking at the source. It accepts jpegs, gifs, and pngs. If you don't set a max width and a max height, one will be set automagically. The image will be scaled to within the set dimensions, but the aspect ratio will be kept intact.
Happy coding!
digifuzz.
Posted on Friday, June 13th, 2008 at 8:43 am and is filed under Php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
July 14th, 2008 at 7:57 pm
Personally, to avoid the whole extension matching thing, I would use the following:
$img = @imagecreatefromstring(file_get_contents($image_path));
It will automagically detect what type of image it is, from any of JPEG, PNG, GIF, WBMP, and GD2.