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 == "" )
-
{
-
}
-
-
# 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;
-
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);
-
-
# If the image is larger than the max shrink it
-
if ($scale < 1)
-
{
-
-
# 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
-
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.
April 4th, 2009 at 7:40 pm
Nice!
To be safe / prevent root path disclosure, you might put intval around the mw and mh variables before using them, and using a file_exists on the file
May 14th, 2009 at 12:35 am
Hey,
This is brilliant.
Thanks a lot.
June 17th, 2009 at 1:43 am
I understand the script, but I’m not understanding how to use it. Too new to Apache and too used to ASP.Net, I reckon. I don’t really know how to find the problem.
The script runs without errors, but using an image link as described in the example link, above, I don’t seem to be passing any parameters and get the “FILE NOT FOUND” message (I’m apparently not passing the image_width and _height parameters, also; slight changes to die without a width or height shows they aren’t being passsed to dfn_thumbnailer, too.
But when I hard-code the image name in the thumbnailer script, I get a text-view version of the page for output.
If you would not mind, could you give a bit more detail about how this is used for the stone dumb like me?
Thanks.
July 30th, 2009 at 5:06 pm
doesnt work with filenames with spaces… its not 1990 anymore yo!
September 20th, 2009 at 8:43 pm
LOL, great site looking through many things you have published. Just a little lost on how this is working.
I tried as directions show but a little messed up on base url and image url setup.
whether the $IMAGE_BASE is the site url or the site url with /images/ tagged on it. is there a zip working of this that I can download
thanks.
Mark.
October 6th, 2009 at 5:50 pm
I can’t get this to work at all. Anyone got it working?
October 9th, 2009 at 12:30 pm
Essentially, you call it like this:
< img src="/thumbnailer.php?img=/site_images/image.jpg&height=300&width=300" />
does that make sense?
And the BASE URL is just what it sounds like, what its set to depends on how you wish to use it. If all your image files will forever be located in http://www.yoursite.com/site_images/ then that would be your BASE URL and you could call it like: src=”/thumbnailer.php?img=image.jpg&height=300&width=300″. Otherwise, if you will vary where the image is coming from, then you can just specify your BASE URL to be http://www.yoursite.com and call it as shown in my first example.
Good luck.
November 1st, 2009 at 1:57 pm
Hi, it works very well, but you have a typo error over here:
imageline($img,0,0,$MAX_WIDTH,$MAX_HEIGHT,$c2);
imageline($img,$MAX_WIDTH,0,0,$MAX_HEIGHT,$c2);
The variable $c2 doesn’t exists, it should be $c
Best regards from Colombia-SouthAmerica
December 9th, 2009 at 12:58 am
Not my cup of tea but it
March 7th, 2010 at 9:22 am
Hi, It doesn’t seem to work for me. I get no errors, it just displays nothing. I am dropping the following code in to my html:
Thats correct right?
Thanks
Chris
March 19th, 2010 at 11:42 pm
i probably would not have decided this had been trendy quite a few years ago but it’s amusing precisely how age adjusts the means by which you perceive different ideas, thanks regarding the write-up it really is pleasing to browse some thing smart now and then instead of the regular nonsense mascarading as blogs on the net, i’m going to enjoy a smattering of hands of zynga poker, take care