<?php
if (isset($_GET['img'])) $img= $_GET['img'];
if (isset($_GET['size'])) $size= $_GET['size'];
if (isset($_GET['margin'])) $margin= $_GET['margin'];
if (isset($_GET['border'])) $border= $_GET['border'];
if (isset($_GET['shadow'])) $shadow= $_GET['shadow'];
if (isset($_GET['tofile'])) $tofile= $_GET['tofile'];

//thumbnail max size
if (!isset($size)) $size = 180;

		//max additional size created by margin
		if (!isset($margin)) $margin = 0;

		//draw a border?
		if (!isset($border)) $border = true;

		//draw a drop shadow?
		if (!isset($shadow)) $shadow = false;


//should it output to a file also ?
if (!isset($tofile)) $tofile = '';

$margin=0;
// $border=true;
$shadow=false;

//where are the images?
$jpegdir = '';

//nothing else to configure

$jpeg = $jpegdir .''. $img;

if($d=getimagesize($jpeg)){
	$dim = ($d[1]>$d[0]) ? 1 : 0;
	$mod = $size / $d[$dim];

	$w = (int) ($d[0] * $mod);
	$h = (int) ($d[1] * $mod);

	$x = (int)(($size + $margin - $w) / 2);
	$y = (int)(($size + $margin - $h) / 2);

	//echo "<br>$w x $h";
	//echo "<br>$x x $y";

	header('Content-type: image/jpeg');
//error_reporting (E_ALL); 
//ini_set ('display_errors', 1); 

	$src = imagecreatefromjpeg($jpeg);
		if (!isset($sizev)) $sizev = imagesy($src)/imagesx($src)*$size;

	$dst = imagecreatetruecolor($size+($margin * 2), $sizev+($margin * 2));
	$white = imagecolorallocate($dst,255,255,255);
	imagefill($dst,0,0,$white);
	if($border){
		$black = imagecolorallocate($dst,0,0,0);
		imagerectangle($dst,0,0,$size+$margin-2,$sizev+$margin-2,$black);
	}
	if($shadow){
		$lighter = imagecolorallocate($dst,220,220,220);
		imagefilledrectangle($dst,($x+3),($y+3),($w+$x+3),($h+$y+3),$lighter);
		$light = imagecolorallocate($dst,180,180,180);
		imagefilledrectangle($dst,($x+1),($y+1),($w+$x+1),($h+$y+1),$light);
		$dark = imagecolorallocate($dst,140,140,140);
		imagefilledrectangle($dst,($x),($y),($w+$x),($h+$y),$dark);
	}
//	imagecopyresampled($dst,$src,$x+1,$y+1,0,0,$w-2.5,$h-2.5,$d[0],$d[1]);

//	imagecopyresampled($dst,$src,1,1,0,0,$size-2.5,$sizev-2.5,$d[0],$d[1]);
	imagecopyresampled($dst,$src,0,0,0,0,$size,$sizev,$d[0],$d[1]);

//	imagecopyresized($dst,$src,1,1,1,1,$size-1.5,$size-1.5,$d[0],$d[1]);
	if (isset($tofile)) imagejpeg($dst,$tofile,60);
	imagejpeg($dst,'',60);
	imagedestroy($dst);
	imagedestroy($src);
}

?>