#native_company# #native_desc#
#native_cta#

Thumbnail Script

By christo
on July 19, 2003

Version: 0.3

Type: Full Script

Category: Graphics

License: GNU General Public License

Description: Very useful for creating multiple sizes of image urls from single images
Creates a thumbnailed image based on info passed to it via get.

Images are cached on the server, so server processing overhead is only
needed for the first time the script runs on a particular image.

Updates available at http://mightystuff.net/thumbnail_script

<?php
/* 
	thumb.php v0.3
	______________________________________________________________________ 
	Creates a thumbnailed image based on info passed to it via $_GET. 
	
	Images are cached on the server, so server processing overhead is only 
	needed for the first time the script runs on a particular image.
	______________________________________________________________________
	Requires:
		GD Library
	______________________________________________________________________
	Copyright: 
		(C) 2003 Chris Tomlinson. [email protected]
		http://mightystuff.net
		
		This library is free software; you can redistribute it and/or
		modify it under the terms of the GNU Lesser General Public
		License as published by the Free Software Foundation; either
		version 2.1 of the License, or (at your option) any later version.
		
		This library is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
		Lesser General Public License for more details.
	
		http://www.gnu.org/copyleft/lesser.txt
	______________________________________________________________________
	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:
		<img src="thumb.php?file=FILE&size=SIZE">
		
		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);

?>