Here is a short script I made a while back which creates thumbnails in various forms depending on the input recieved via HTTP GET.
examples:
1) 'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480'
Creates a thumbnail, preserving the aspect ratio of the original image (so it isnt squished/stretched at all) and limits the size to fitting inside a 640 x 480 pixel box.
2) 'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480&stretch'
Same as before except the image is stretched to fit exactly in a 640 x 480 box.
Notes: The script will attempt to cache the file with the name <original_file_name>thumb<pixel_width>x<pixel_height>_<scale/stretch> unless the variable 'no_cache' or 'no_write' exists in the HTTP GET stream; if the cache is successful, then all recurring requests will be loaded from the cache file.
example of http url which tells the script not to cache:
'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480&stretch&no_cache'
<?
// Jon Newman - jnewman (at) oplink (d0t) net (2003)
function create_thumb($im_source, $max_x, $max_y, $output_file, $stretch=FALSE)
{
$im_prop=getimagesize($im_source);
if(!$im_prop[2]==2)
return(0);
else
{
$src_im=imagecreatefromjpeg($im_source);
$src_x=imagesx($src_im);
$src_y=imagesy($src_im);
$thumb_x=$src_x;
$thumb_y=$src_y;
if($src_x>$max_x && $src_x>=$src_y)
{
$thumb_x=$max_x;
if(!$stretch)
$thumb_y=(int)(($max_x/$src_x)*$src_y);
else
$thumb_y=$max_y;
}
else if($src_y>$max_y && $src_y>$src_x)
{
$thumb_y=$max_y;
if(!$stretch)
$thumb_x=(int)(($max_y/$src_y)*$src_x);
else
$thumb_x=$max_x;
}
if(($thumb_y=="0") && ($thumb_x=="0"))
return(0);
elseif($thumb_y == "0")
{
$scale_x=$thumb_x/($src_x-1);
$thumb_y=$src_y*$scale_x;
}
elseif($thumb_x=="0")
{
$scale_y=$thumb_y/($src_y-1);
$thumb_x=$src_x*$scale_y;
}
$thumb_x=(int)($thumb_x);
$thumb_y=(int)($thumb_y);
$dest_im=imagecreatetruecolor($thumb_x, $thumb_y);
if(!imagecopyresized($dest_im, $src_im, 0, 0, 0, 0, $thumb_x, $thumb_y, $src_x, $src_y))
{
imagedestroy($src_im);
imagedestroy($dest_im);
return(0);
}
else
{
imagedestroy($src_im);
if($output_file!='')
{
if(@imagejpeg($dest_im,$output_file))
{
imagedestroy($dest_im);
return(-1);
}
else
{
imagejpeg($dest_im);
imagedestroy($dest_im);
return(0);
}
}
else
{
imagejpeg($dest_im);
imagedestroy($dest_im);
return(-1);
}
imagedestroy($dest_im);
}
return(0);
}
}
header("Content-type: image/jpeg");
if(isset($_GET['img']) && file_exists($_GET['img']))
{
$_file=$_GET['img'].'_thumb_'.$_GET['limit_width'].'x'.$_GET['limit_height'].(isset($_GET['stretch']) ? '_stretch' : '_scale');
if(!file_exists($_file) || isset($_GET['no_cache']))
{
if(($t=create_thumb($_GET['img'],(isset($_GET['limit_width']) ? $_GET['limit_width'] : 100),(isset($_GET['limit_height']) ? $_GET['limit_height'] : 100),(!isset($_GET['nowrite']) && !isset($_GET['no_cache']) ? $_file : ''),(isset($_GET['stretch']) ? TRUE : FALSE)))==-1)
imagejpeg(@imagecreatefromjpeg($_file));
else
{
$im=imagecreatetruecolor(100,100);
$background_color=imagecolorallocate($im, 0, 0, 0);
$text_color=imagecolorallocate($im, 233, 14, 91);
imagestring($im, 2, 35, 40, ": X :", $text_color);
imagejpeg($im);
}
}
else
{
imagejpeg(@imagecreatefromjpeg($_file));
}
}
else
{
$im=imagecreatetruecolor(100,100);
$background_color=imagecolorallocate($im, 0, 0, 0);
$text_color=imagecolorallocate($im, 233, 14, 91);
imagestring($im, 2, 35, 40, ": X :", $text_color);
imagejpeg($im);
}
?>
Although this doesn't do exactly what you want you should be able to modify it to suit your needs.