Inspired by [url=http://www.webdeveloper.com/forum/showthread.php?t=109807]this thread
[code=php]
<?php
/*
FILE: thumb.php
PURPOSE: create thumbnail image “on demand” for image file in same directory
CREATED: June 2006 by Charles Reace (www.charles-reace.com)
USAGE: <img src=”images/thumb.php?file=image.png” alt=””>
*/
// change these values to desired max dimensions in pixels:
$maxWidth = 100;
$maxHeight = 75;
if(!empty($_GET[‘file’]))
{
$file = basename($_GET[‘file’]); // will only work on files in this directory
if(is_readable($file))
{
$sizeInfo = getimagesize($file);
if($sizeInfo !== FALSE)
{
$imgTypes = array( // IMAGETYPE_* for getimagesize() index 2
IMAGETYPE_GIF => IMG_GIF, // IMG_* for imagetype() check
IMAGETYPE_JPEG => IMG_JPG,
IMAGETYPE_PNG => IMG_PNG,
IMAGETYPE_WBMP => IMG_WBMP
);
if(isset($imgTypes[$sizeInfo[2]]) and
(imagetypes() & $imgTypes[$sizeInfo[2]]))
{
$ratio = (($sizeInfo[0]/$sizeInfo[1]) > ($maxWidth/$maxHeight)) ?
$maxWidth / $sizeInfo[0] :
$maxHeight / $sizeInfo[1];
$newWidth = $sizeInfo[0] * $ratio;
$newHeight = $sizeInfo[1] * $ratio;
$thumb = imagecreatetruecolor($newWidth, $newHeight);
switch($sizeInfo[2]) // image type
{
case IMAGETYPE_GIF:
$source = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$source = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$source = imagecreatefrompng($file);
break;
case IMAGETYPE_WBMP:
$source = imagecreatefromwbmp($file);
break;
default:
header(“HTTP/1.0 404 Not Found”);
exit;
}
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $sizeInfo[0], $sizeInfo[1]);
header(“Content-type: image/jpeg”);
imagejpeg($thumb);
// All done
exit;
}
}
}
}
// For simplicity’s sake, just send 404 if any errors
header(“HTTP/1.0 404 Not Found”);
?>