Graphics Php

/*
Configuration:
set $thumb_size to be a default maximum width/height if not passed via get
set $no_image to be an image to be used when GIF files are passed to the script (The GD library no longer supports gif images)
set $site_config['path_thumbnail'] to be a write-permissable folder on your server relative to the DOCUMENT_ROOT for storing images so they cache on the server.
set $quality to be a value 0-100 for the resulting thumbnail jpeg quality
______________________________________________________________________
Usage:

Where:
FILE = the file to retrieve
SIZE = the maximum size of the thumbnail in pixels
______________________________________________________________________
*/
//script configuration
$thumb_size = 128; //all thumbnails are this maximum width or height if not specified via get
$no_image = '/preview/images/no_image.png'; // used if no image could be found, or a gif image is specified
$site_config['path_thumbnail']='/images/thumbnails/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT
$quality = 70;
//define size of image (maximum width or height)- if specified via get.
if ($_GET["size"]<>0) {
$thumb_size=$_GET["size"];
}
//determine filesystem - *nix or windows - and set document_root. if the file is outside of the web server root path, the document_root is ignored and the absolute path of the image is used instead.
if (substr_count(strtolower($_ENV['OS']), "windows")>0)
{
//w32 filesystem
$site_config['document_root'] = str_replace(str_replace("/","\\\\",$_SERVER["SCRIPT_NAME"]),"",$_SERVER["PATH_TRANSLATED"]);
if (file_exists(str_replace("/","\\\\",$_GET['file'])))
{
$filename=str_replace("/","\\\\",$_GET['file']);
} else {
$filename=$site_config['document_root']."\\\\".str_replace("/","\\\\",$_GET['file']);
}
} else {
//linux filesystem
$site_config['document_root'] = $_SERVER['DOCUMENT_ROOT'];
if (file_exists($_GET["file"]))
{
$filename=$_GET["file"];
} else {
$filename=$site_config['document_root'].'/'.$_GET["file"];
}
}
$no_image=$site_config['document_root'].$no_image;
if (file_exists($site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg'))
{
Header('Content-type: image/jpeg');
Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size).'.jpeg'));
echo (join('', file( $site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg' )));
exit; //no need to create thumbnail - it already exists in the cache
}
//determine php and gd versions
$ver=intval(str_replace(".","",phpversion()));
if ($ver>=430)
{
$gd_version=gd_info();
}
$filename=str_replace("\'","'",$filename);
//define the right function for the right image types
$image_type_arr=getimagesize($filename);
$image_type=$image_type_arr[2];
switch ($image_type)
{
case 2: // JPG
$image = ImageCreateFromJPEG ($filename);
break;
case 3: // PNG
$image = ImageCreateFromPNG ($filename);
break;
case 1: // GIF - replace file with a 'no image' png
default:
$image = ImageCreateFromPNG ($no_image);
break;
}
//define size of original image
$image_width = imagesx($image);
$image_height = imagesy($image);
//define size of the thumbnail
$thumb_width = $thumb_size; //$image_width/$resize_factor;
$factor = $image_width/$thumb_size;
$thumb_height = $image_height / $factor; //($image_width.$width_factor); //$resize_factor;
if ($thumb_height>$thumb_size)
{
$thumb_height = $thumb_size; //$image_width/$resize_factor;
$factor = $image_height/$thumb_size;
$thumb_width = $image_width / $factor; //($image_width.$width_factor); //$resize_factor;
}
//define size if sizex and sizey given
if ($_GET["sizex"]<>0) {
$thumb_width=$_GET["sizex"];
$thumb_height=$_GET["sizey"];
}
$memsize=filesize($filename);
//create the thumbnail
if ($image_width<2000) //no point in resampling images larger than 2000 pixels wide - too much server processing overhead - a resize is more economical
{
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
} else {
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
}
//create thumbnail in the cache because it doesn't exist yet.
Header('Content-type: image/jpeg');
Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size).'.jpeg'));
imagejpeg($thumbnail,$site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg',$quality);
echo (join('', file( $site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg')));
//clear memory
imagedestroy ($image);
imagedestroy ($thumbnail);
?>