Location: PHPKode > scripts > Particle Gallery > generators/thumbnail.php
<?php
require("../includes/global.php");

$sql = "SELECT * FROM " . $dbprefix . "images WHERE imageid = " . intval($_GET["imageid"]);
$img = $db->execute($sql);
if ($img->rows < 1){ notfound(); }

// support for non-GD users
function default_thumb(){
	$cn = file_get_contents("../images/thumbnail.gif");
	Header("Content-type: image/gif");
	echo($cn); die();
}

if ($config["enablecache"] == "true"){
	if (is_writable("../cache/")){
		$usecache = 1;
		$cachepath = "../cache/thumb" . $img->fields["imageid"];
		
		if (file_exists($cachepath)){
			$stats = stat($cachepath);
			if ($stats[9] > (time() - 86400)){
				// use cache file
				$cn = file_get_contents($cachepath);
				Header("Content-type: " . $img->fields["format"]);
				echo($cn); die();
			}
		}
	}
}

// check for GD support and fopen wrapters
if ($gd === FALSE){ default_thumb(); }
if (ini_get("allow_url_fopen") == 0){ default_thumb(); }
if (ini_get("safe_mode") == 1){ default_thumb(); }

// create a temperory file
$filepath = str_replace("thumbnail", "original", "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"]);
$imageFile = file_get_contents($filepath . "?imageid=" . $img->fields["imageid"]);
$tmpname = tempnam("/tmp", "");
$fp = fopen($tmpname, "w");
fwrite($fp, $imageFile);
fclose($fp);

// thumbnail size
$thumbsize = intval($config["thumbnailsize"]);
$thumbsize = ($thumbsize < 10) ? 100 : $thumbsize;

// create variables
$size = GetImageSize($tmpname);
if ($size[0] < $thumbsize && $size[1] < $thumbsize){
	$thumbnail_width = $size[0];
	$thumbnail_height = $size[1];
} elseif($size[0] > $size[1]){
	$thumbnail_width = $thumbsize;
	$thumbnail_height = (int)($thumbsize * $size[1] / $size[0]);
} else {
	$thumbnail_width = (int)($thumbsize * $size[0] / $size[1]);
	$thumbnail_height = $thumbsize;
}

// create functions
$function_suffix = $gd_function_suffix[$img->fields["format"]];
$function_to_read = 'ImageCreateFrom' . $function_suffix;
$function_to_write = 'Image' . $function_suffix;

if (!(function_exists($function_to_read))){ default_thumb(); }
if (!(function_exists($function_to_write))){ default_thumb(); }

$source_handle = $function_to_read($tmpname);
$destination_handle = ImageCreateTrueColor($thumbnail_width, $thumbnail_height);
ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);

// write file to the cache?
if ($usecache == 1){
	//$c_handle = fopen($cachepath, "w");
	//fwrite($c_handle, $destination_handle);
	//fclose($c_handle);
	$function_to_write($destination_handle, $cachepath);
	@chmod($cachepath, 0777);
}

// output content
Header("Content-type: " . $img->fields["format"]);
$function_to_write($destination_handle);

// and delete the tmp line
unlink($tmpname);
?>
Return current item: Particle Gallery