###############################################################
	# Thumbnailer version 3.5.5                                   #
	# This program worked phenominally as a Perl CGI file and now #
	# it's time to take it to a new level, turning it into a  PHP #
	# function and a PHP image spitter-outer.                     #
	# Writen by and for Tweaked Solutions Inc. 2002-2004          #
	###############################################################
	# To Do for version 4: Text in image when missing, and inter- #
	# pret text into image and print on top of image.             #
	###############################################################
	# 3.5.5 - Made thumbnails come out of exif data if small req. #
	###############################################################
	# 3.4.0 - Added quality                                       #
	###############################################################
	# 3.3.5 - Fixed thumbcache problems.                          #
	#       - Fixed division by zero from cache problem.          #
	###############################################################
	# 3.3.0 - Make TIFs thumbnailable.. impossible.               #
	###############################################################
	# 3.2.5 - Changed the caching code to include file modified   #
	#         stamp and made md5() check the entire path, not     #
	#         just the filename.                                  #
	###############################################################
	# 3.2.0 - Made urlpath, imgWH, fill and distance, also supply #
	#         alt attribute.                                      #
	###############################################################
	# 3.1.0 - Made urlpath able to take imgWH properties          #
	###############################################################
	# 3.0.2 - Made urlpath, imgWH, and fill swappable with        #
	#         alignment when called as a function.                #
	###############################################################
	# 3.0.1 - Changed for register globals (shoulda been done!!!) #
	###############################################################
	# 3.0.0 - Added image caching options                         #
	###############################################################
	# 2.0.4 - Made function properties optional.                  #
	###############################################################
	# 2.0.3 - Changed imagecopyresized to imagecopyresampled to   #
	#         make things nicer looking on image output.          #
	###############################################################
	# 2.0.2 - Added imagecreate back to script to check for old   #
	#         PHP versions                                        #
	#       - Added error catch on non-existant filename          #
	###############################################################
	# 2.0.1 - Changed imagecreate to imagecreatetruecolor         #
	###############################################################
	if ($_GET["image"] == "" && $_GET["bimage"] == "") {
		# Since the user hasn't supplied an image for us to 
		# dig up, we load the function.
		# The function works a little different, it just makes the HTML code and calls 
		# this script.
		function thumbnailer($thumb_filepath, $thumb_filename, $thumb_urlpath = "", $thumb_imgWH = 64, $thumb_fill = "no", $thumb_distance = "", $thumb_alt = "", $thumb_nocache = 0, $thumb_outtype = "", $thumb_quality = 75) {
			# Distance is the distance from your other script to this one, ie: "../" or "/full/path/"
			$tcache = "/usr2/thumbcache/";
			if (!is_writable($tcache)) {
				if (file_exists("settings.inc")) {
					$thumbgrab = 1;
					include("settings.inc");
				}
				if (is_writable("thumbcache/") && !is_writable($tcache)) {
					$tcache = "thumbcache/";
				}
				if (is_writable("./") && !is_writable($tcache)) {
					mkdir("./thumbcache", 0700);
					$cache = "thumbcache/";
				}
			}			
			if ($thumb_urlpath == "right" || $thumb_urlpath == "left" || $thumb_urlpath == "center") {
				$align = $thumb_urlpath;
				$thumb_urlpath = "";
			}
			elseif ($thumb_urlpath + 1 > 1) {
				$thumb_imgWH = $thumb_urlpath;
				$thumb_urlpath = "";
			}
			elseif ($thumb_urlpath != "" && strpos($thumb_urlpath, "/") === FALSE) {
				# if it's not blank and doesn't contain a /, it's probably the alt field.
				$thumb_alt = $thumb_urlpath;
				$thumb_urlpath = "";
			}
			if ($thumb_imgWH == "right" || $thumb_imgWH == "left" || $thumb_imgWH == "center") {
				$align = $thumb_imgWH;
				$thumb_imgWH = 64;
			}
			elseif ($thumb_imgWH != "" && !($thumb_imgWH + 1 > 1)) {
				$thumb_alt = $thumb_imgWH;
				$thumb_imgWH = 64;
			} 
			if ($thumb_fill == "right" || $thumb_fill == "left" || $thumb_fill == "center") {
				$align = $thumb_fill;
				$thumb_fill = "no";
			}
			elseif ($thumb_fill != "" && $thumb_fill != "no" && $thumb_fill != "white" && $thumb_fill != "black" && $thumb_fill != "trans") {
				$thumb_alt = $thumb_fill;
				$thumb_fill = "no";
			}
			if (!file_exists($thumb_distance) && $thumb_distance != "") {
				$thumb_alt = $thumb_distance;
				$thumb_distance = "";
			}
			if ($thumb_imgWH == 0) {
				$thumb_imgWH = 64;
			}
			if ($thumb_imgWH > 3000) {
				# insanity check
				$thumb_imgWH = 3000;
			}
			$image = $thumb_filepath . $thumb_filename;
			# Check to see if the image is in the cache
			if (file_exists($tcache . md5($image) . date(".Y-m-d.H.i.s.", @filemtime($image)) . $thumb_imgWH . $thumb_quality) && $thumb_nocache == 0) {
				$imgSize = getimagesize($tcache . md5($image) . date(".Y-m-d.H.i.s.", @filemtime($image)) . $thumb_imgWH . $thumb_quality);
				
				if (!$imgSize) {
					print("Error, " . $image . " not found");
					return 0;
				}
				else {
					$width = $imgSize[0];
					$height = $imgSize[1];
					$ratio = $width / $thumb_imgWH;
					if ($height / $ratio > $thumb_imgWH) {
						$ratio = $height / $thumb_imgWH;
						$newheight = $thumb_imgWH;
						$newwidth = intval($width / $ratio);
						$left = intval(($thumb_imgWH - $newwidth) / 2);
						$top = 0;
					}
					else {
						$newwidth = $thumb_imgWH;
						$newheight = intval($height / $ratio);
						$top = intval(($thumb_imgWH - $newheight) / 2);
						$left = 0;
					}
					if ($thumb_fill == "white" || $thumb_fill == "black") {
						$usewidth = $thumb_imgWH;
						$useheight = $thumb_imgWH;
					}
					elseif ($thumb_fill == "trans" && preg_match("/\.gif/i", $image) && imagetypes() & IMG_GIF) {
						$usewidth = $thumb_imgWH;
						$useheight = $thumb_imgWH;
					}
					elseif ($thumb_fill == "trans" && (preg_match("/\.gif/i", $image) || preg_match("/\.png/i", $image)) && imagetypes() & IMG_PNG && (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"]) || preg_match("/Netscape6/", $_SERVER["HTTP_USER_AGENT"]))) {
						$usewidth = $thumb_imgWH;
						$useheight = $thumb_imgWH;
					}
					elseif ($thumb_fill == "trans") {
						$usewidth = $thumb_imgWH;
						$useheight = $thumb_imgWH;
					}
					else {
						$usewidth = $newwidth;
						$useheight = $newheight;
					}
					# Now that we've learned all we need to know, give output.
					if ($thumb_urlpath != "") {
						?>
					}
					?>; ?>thumbnailer.php?image=<? print($thumb_filepath . $thumb_filename); ?>&imgWH=<? print($thumb_imgWH); ?>&fill=<? print($thumb_fill); ?><?
						if ($thumb_nocache != 0) {
							?>&nocache=<? 
							print($thumb_nocache . ) &outtype=
							print($thumb_outtype);
						}
						if ($thumb_quality != 75) {
							?>&quality=
							print($thumb_quality);
						}
					?>" WIDTH=" print($usewidth); ?>" HEIGHT=" print($useheight); ?>" BORDER="0" ALT=" print($thumb_alt); ?>" TITLE=" print($thumb_alt); ?>"
					if ($align != "") {
						?> ALIGN=" print($align); ?>"
					}
					?>>
					if ($thumb_urlpath != "") {
						?>
					}
				}
			}
			else {
				# Check to see if this file includes exif data which might include a thumbnail...
				$exifthumb = @exif_thumbnail($image, $exthumbwidth, $exthumbheight, $exthumbtype);
				if ($exifthumb) {
					if ($exthumbwidth >= $thumb_imgWH || $exthumbheight >= $thumb_imgWH) {
						# okay, this thumbnail is probably better to make my thumbnail with...
						# now what?
						$gototown = 1;
						$imgSize = array($exthumbwidth, $exthumbheight, $exthumbtype);
					}
				}
				if ($gototown != 1) {
					$imgSize = @getimagesize($image);
					if (!$imgSize) {
						print("Error, " . $image . " not found");
						return 0;
					}
				}
				$width = $imgSize[0];
				$height = $imgSize[1];
				$ratio = $width / $thumb_imgWH;
				if ($height / $ratio > $thumb_imgWH) {
					$ratio = $height / $thumb_imgWH;
					$newheight = $thumb_imgWH;
					$newwidth = intval($width / $ratio);
					$left = intval(($thumb_imgWH - $newwidth) / 2);
					$top = 0;
				}
				else {
					$newwidth = $thumb_imgWH;
					$newheight = intval($height / $ratio);
					$top = intval(($thumb_imgWH - $newheight) / 2);
					$left = 0;
				}
				if ($thumb_fill == "white" || $thumb_fill == "black") {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans" && preg_match("/\.gif/i", $image) && imagetypes() & IMG_GIF) {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans" && (preg_match("/\.gif/i", $image) || preg_match("/\.png/i", $image)) && imagetypes() & IMG_PNG && (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"]) || preg_match("/Netscape6/", $_SERVER["HTTP_USER_AGENT"]))) {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans") {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				else {
					$usewidth = $newwidth;
					$useheight = $newheight;
				}
				if ($thumb_urlpath != "") {
					?>
				}
				?>
&outtype=
							print($thumb_outtype);
						}
						if ($thumb_quality != 75) {
							?>&quality=
							print($thumb_quality);
						}
					?>" WIDTH=" print($usewidth); ?>" HEIGHT=" print($useheight); ?>" BORDER="0" ALT=" print($thumb_alt); ?>" TITLE=" print($thumb_alt); ?>"
					if ($align != "") {
						?> ALIGN=" print($align); ?>"
					}
					?>>
					if ($thumb_urlpath != "") {
						?>
					}
				}
			}
			else {
				# Check to see if this file includes exif data which might include a thumbnail...
				$exifthumb = @exif_thumbnail($image, $exthumbwidth, $exthumbheight, $exthumbtype);
				if ($exifthumb) {
					if ($exthumbwidth >= $thumb_imgWH || $exthumbheight >= $thumb_imgWH) {
						# okay, this thumbnail is probably better to make my thumbnail with...
						# now what?
						$gototown = 1;
						$imgSize = array($exthumbwidth, $exthumbheight, $exthumbtype);
					}
				}
				if ($gototown != 1) {
					$imgSize = @getimagesize($image);
					if (!$imgSize) {
						print("Error, " . $image . " not found");
						return 0;
					}
				}
				$width = $imgSize[0];
				$height = $imgSize[1];
				$ratio = $width / $thumb_imgWH;
				if ($height / $ratio > $thumb_imgWH) {
					$ratio = $height / $thumb_imgWH;
					$newheight = $thumb_imgWH;
					$newwidth = intval($width / $ratio);
					$left = intval(($thumb_imgWH - $newwidth) / 2);
					$top = 0;
				}
				else {
					$newwidth = $thumb_imgWH;
					$newheight = intval($height / $ratio);
					$top = intval(($thumb_imgWH - $newheight) / 2);
					$left = 0;
				}
				if ($thumb_fill == "white" || $thumb_fill == "black") {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans" && preg_match("/\.gif/i", $image) && imagetypes() & IMG_GIF) {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans" && (preg_match("/\.gif/i", $image) || preg_match("/\.png/i", $image)) && imagetypes() & IMG_PNG && (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"]) || preg_match("/Netscape6/", $_SERVER["HTTP_USER_AGENT"]))) {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				elseif ($thumb_fill == "trans") {
					$usewidth = $thumb_imgWH;
					$useheight = $thumb_imgWH;
				}
				else {
					$usewidth = $newwidth;
					$useheight = $newheight;
				}
				if ($thumb_urlpath != "") {
					?>
				}
				?>; ?>thumbnailer.php?image=<? print($thumb_filepath . $thumb_filename); ?>&imgWH=<? print($thumb_imgWH); ?>&fill=<? print($thumb_fill); ?><?
					if ($thumb_nocache != 0) {
						?>&nocache=<? 
						print($thumb_nocache . ) &outtype=
						print($thumb_outtype);
					}
					if ($thumb_quality != 75) {
						?>&quality=
						print($thumb_quality);
					}
				?>" WIDTH=" print($usewidth); ?>" HEIGHT=" print($useheight); ?>" BORDER="0" ALT=" print($thumb_alt); ?>" TITLE=" print($thumb_alt); ?>"
				if ($align != "") {
					?> ALIGN=" print($align); ?>"
				}
				?>>
				if ($thumb_urlpath != "") {
					?>
				}
			}
		}
	}
	else {
		if (isset($_GET["bimage"])) {
			$_GET["image"] = base64_decode($_GET["bimage"]);
		}
		$_GET["image"] = stripslashes($_GET["image"]);		
		$log = 1;
		$id = md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"] . " nocache: " . $_GET["nocache"];
		$tcache = "/usr2/thumbcache/";
		if (!is_writable($tcache)) {
			if (file_exists("settings.inc")) {
				$thumbgrab = 1;
				include("settings.inc");
			}
			if (is_writable("thumbcache/") && !is_writable($tcache)) {
				$tcache = "thumbcache/";
			}
			if (is_writable("./") && !is_writable($tcache)) {
				mkdir("./thumbcache", 0700);
				$tcache = "thumbcache/";
			}
		}			
		if ($log == 1) {
			$flog = fopen($tcache . "/thumbnailer.log", "a");
			$start = microtime(1);
			fwrite($flog, $id . " - " . $start . ": Start\n");
		}
		if ($_GET["imgWH"] == 0) {
			$_GET["imgWH"] = 64;
		}
		if ($_GET["imgWH"] > 3000) {
			# insanity check
			$_GET["imgWH"] = 3000;
		}
		if ($_GET["quality"] == 0) {
			$_GET["quality"] = 75;
		}
		if (file_exists($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]) && $_GET["nocache"] == 0) {
			$imgSize = getimagesize($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
	
			$width = $imgSize[0];
			$height = $imgSize[1];
			$type = image_type_to_mime_type($imgSize[2]);
			if ($_GET["outtype"] != "") {
				$thumb_outtype = strtolower($_GET["outtype"]);
			}
			else {
				$thumb_outtype = $type;
			}
			$ratio = $width / $_GET["imgWH"];
			if ($height / $ratio > $_GET["imgWH"]) {
				$ratio = $height / $_GET["imgWH"];
				$newheight = $_GET["imgWH"];
				$newwidth = intval($width / $ratio);
				$left = intval(($_GET["imgWH"] - $newwidth) / 2);
				$top = 0;
			}
			else {
				$newwidth = $_GET["imgWH"];
				$newheight = intval($height / $ratio);
				$top = intval(($_GET["imgWH"] - $newheight) / 2);
				$left = 0;
			}
			if ($type == "image/gif") {
				$imgSrc = imagecreatefromgif($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagegif($imgSrc);
			}
			elseif ($type == "image/png") {
				$imgSrc = imagecreatefrompng($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
       	imagealphablending($imgSrc, false);
       	imagesavealpha($imgSrc, true);
       	imagealphablending($imgSrc, true);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagepng($imgSrc);
			}
			else {
				$imgSrc = imagecreatefromjpeg($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagejpeg($imgSrc);
			}
			imagedestroy($imgSrc);
		}
		else {
			# Check to see if this file includes exif data which might include a thumbnail...
			$exifinfo = @exif_read_data($_GET["image"]);
			if ($exifinfo["COMPUTED"]["Thumbnail.MimeType"] == "image/jpeg") {
				$exifthumb = @exif_thumbnail($_GET["image"], $exthumbwidth, $exthumbheight, $exthumbtype);
				if ($log == 1) {
					fwrite($flog, $id . " - " . microtime(1) . ": Checking exif data\n");
				}
				if ($exifthumb) {
					if ($log == 1) {
						fwrite($flog, $id . " - " . microtime(1) . ": Got exif data (" . strlen($exifthumb) . " bytes).. checking if it's worth it...\n");
					}
					if ($exthumbwidth >= $_GET["imgWH"] || $exthumbheight >= $_GET["imgWH"]) {
						if ($log == 1) {
							fwrite($flog, $id . " - " . microtime(1) . ": " . $exthumbwidth . " >= " . $_GET["imgWH"] . " || " . $exthumbheight . " >= " . $_GET["imgWH"] . " is True\n");
						}
						# okay, this thumbnail is probably better to make my thumbnail with...
						# now what?
	
						$tmpfname = tempnam($tcache, "thumbnailer") . ".jpg";
						$wrote = file_put_contents($tmpfname, $exifthumb);
						$gototown = 1;
						$id .= " exif thumb";
					}
					elseif ($log == 1) {
						fwrite($flog, $id . " - " . microtime(1) . ": " . $exthumbwidth . " >= " . $_GET["imgWH"] . " || " . $exthumbheight . " >= " . $_GET["imgWH"] . " is False\n");
					}
	
				}
			}
			if ($gototown != 1) {
				$imgSize = @getimagesize($_GET["image"]);
				if (!$imgSize) {
					print("Error, " . $_GET["image"] . " not found");
					return 0;
				}
			}
			else {
				$imgSize = getimagesize($tmpfname);
			}
			$width = $imgSize[0];
			$height = $imgSize[1];
			$type = $imgSize[2];
			if ($_GET["outtype"] != "") {
				$out = strtolower($_GET["outtype"]);
			}
			else {
				$out = image_type_to_mime_type($imgSize[2]);
			}
			$ratio = $width / $_GET["imgWH"];
			if ($height / $ratio > $_GET["imgWH"]) {
				$ratio = $height / $_GET["imgWH"];
				$newheight = $_GET["imgWH"];
				$newwidth = intval($width / $ratio);
				$left = intval(($_GET["imgWH"] - $newwidth) / 2);
				$top = 0;
			}
			else {
				$newwidth = $_GET["imgWH"];
				$newheight = intval($height / $ratio);
				$top = intval(($_GET["imgWH"] - $newheight) / 2);
				$left = 0;
			}
			if ($type == IMAGETYPE_GIF && $gototown == 0) {
				$imgSrc = imagecreatefromgif($_GET["image"]);
			}
			elseif ($type == IMAGETYPE_GIF && $gototown == 1) {
				$imgSrc = imagecreatefromgif($tmpfname);
			}
			elseif ($type == IMAGETYPE_PNG && $gototown == 0) {
				$imgSrc = imagecreatefrompng($_GET["image"]);
#				imagesavealpha($imgSrc, true);
				imagealphablending($imgSrc, true);
			}
			elseif ($type == IMAGETYPE_PNG && $gototown == 1) {
				$imgSrc = imagecreatefrompng($tmpfname);
			}
			elseif ($gototown == 0) {
				$imgSrc = imagecreatefromjpeg($_GET["image"]);
			}
			elseif ($gototown == 1) {
				$imgSrc = imagecreatefromjpeg($tmpfname);
			}
			if ($_GET["fill"] == "") {
				$_GET["fill"] = "no";
			}
			if ($_GET["fill"] == "white") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$white = imagecolorallocate($imgOut, 255, 255, 255);
			}
			elseif ($_GET["fill"] == "black") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$black = imagecolorallocate($imgOut, 0, 0, 0);
			}
			elseif ($_GET["fill"] == "trans") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$ugly = imagecolorallocate($imgOut, 91, 67, 40);
			   $trans = imagecolortransparent($imgOut, $ugly);
			}
			else {
				# no border for you..
				$imgOut = imagecreatetruecolor($newwidth, $newheight);
				if ($type == IMAGETYPE_PNG) {
					$trnprt_indx = imagecolortransparent($imgSrc);
					if ($trnprt_indx >= 0) {
	  	      imagealphablending($imgOut, false);
	 		      imagesavealpha($imgOut, true);
	        	$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
	        	$trnprt_indx  = imagecolorallocatealpha($imgOut, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue'], 127);
	        	imagefill($imgOut, 0, 0, $trnprt_indx);
 	        	imagecolortransparent($imgOut, $trnprt_indx);
						$_GET["nocache"] = 1;
 	      	}
	      	elseif ($type == IMAGETYPE_PNG) {
	        	imagealphablending($imgOut, false);
	        	$color = imagecolorallocatealpha($imgOut, 0, 0, 0, 127);
 	       		imagefill($imgOut, 0, 0, $color);
	        	imagesavealpha($imgOut, true);
	        	imagealphablending($imgOut, true);
					}
      	}
				elseif ($type == IMAGETYPE_GIF) {
					$trnprt_indx = imagecolortransparent($imgSrc);
					if ($trnprt_indx >= 0 && $trnprt_indx < 255) {
	  	      imagealphablending($imgOut, false);
	 		      imagesavealpha($imgOut, true);
	        	$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
	        	$trnprt_indx  = imagecolorallocate($imgOut, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
	        	imagefill($imgOut, 0, 0, $trnprt_indx);
 	        	imagecolortransparent($imgOut, $trnprt_indx);
						$_GET["nocache"] = 1;
					}
				}				
			if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$left = 0;
				$top = 0;
		}
			imagecopyresampled($imgOut, $imgSrc, $left, $top, 0, 0, $newwidth, $newheight, $width, $height);
			if ($out == "image/gif") {
				header("Content-type: image/gif");
				header("Content-disposition: inline; filename=" . time() . ".gif");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagegif($imgOut);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagegif($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			elseif ($out == "image/png") {
#				$white = imagecolorclosest($imgSrc, 255, 255, 255);
#				imagecolortransparent($imgSrc, $white);
				
				header("Content-type: image/png");
				header("Content-disposition: inline; filename=" . time() . ".png");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagepng($imgOut);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagepng($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			else {
				header("Content-type: image/jpeg");
				header("Content-disposition: inline; filename=" . time() . ".jpeg");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagejpeg($imgOut, "", $_GET["quality"]);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagejpeg($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			imagedestroy($imgSrc);
			imagedestroy($imgOut);
			if ($gototown == 1) {
				unlink($tmpfname);
				if (file_exists(substr($tmpfname, 0, -4))) {
					unlink(substr($tmpfname, 0, -4));
				}
			}
			if ($log == 1) {
				$end = microtime(1);
				fwrite($flog, $id . " - " . $end . ": Ended.\n" . $id . " - " . ($end - $start) . " seconds.\n\n");
				fclose($flog);
			}
		}
	}
	/* 
	Code From printeasy 
	$fontbase = "/home/printsmadeeasy/fonts/";
	$fontfile = $fontbase . "arial.ttf";
	##--  Modify the following parameters or get them through the URL --##
	##--  Make sure to substitute line breaks with the special code   -br-   The reason is to standardize the newline codes which could be a \n, \r, or
&outtype=
						print($thumb_outtype);
					}
					if ($thumb_quality != 75) {
						?>&quality=
						print($thumb_quality);
					}
				?>" WIDTH=" print($usewidth); ?>" HEIGHT=" print($useheight); ?>" BORDER="0" ALT=" print($thumb_alt); ?>" TITLE=" print($thumb_alt); ?>"
				if ($align != "") {
					?> ALIGN=" print($align); ?>"
				}
				?>>
				if ($thumb_urlpath != "") {
					?>
				}
			}
		}
	}
	else {
		if (isset($_GET["bimage"])) {
			$_GET["image"] = base64_decode($_GET["bimage"]);
		}
		$_GET["image"] = stripslashes($_GET["image"]);		
		$log = 1;
		$id = md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"] . " nocache: " . $_GET["nocache"];
		$tcache = "/usr2/thumbcache/";
		if (!is_writable($tcache)) {
			if (file_exists("settings.inc")) {
				$thumbgrab = 1;
				include("settings.inc");
			}
			if (is_writable("thumbcache/") && !is_writable($tcache)) {
				$tcache = "thumbcache/";
			}
			if (is_writable("./") && !is_writable($tcache)) {
				mkdir("./thumbcache", 0700);
				$tcache = "thumbcache/";
			}
		}			
		if ($log == 1) {
			$flog = fopen($tcache . "/thumbnailer.log", "a");
			$start = microtime(1);
			fwrite($flog, $id . " - " . $start . ": Start\n");
		}
		if ($_GET["imgWH"] == 0) {
			$_GET["imgWH"] = 64;
		}
		if ($_GET["imgWH"] > 3000) {
			# insanity check
			$_GET["imgWH"] = 3000;
		}
		if ($_GET["quality"] == 0) {
			$_GET["quality"] = 75;
		}
		if (file_exists($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]) && $_GET["nocache"] == 0) {
			$imgSize = getimagesize($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
	
			$width = $imgSize[0];
			$height = $imgSize[1];
			$type = image_type_to_mime_type($imgSize[2]);
			if ($_GET["outtype"] != "") {
				$thumb_outtype = strtolower($_GET["outtype"]);
			}
			else {
				$thumb_outtype = $type;
			}
			$ratio = $width / $_GET["imgWH"];
			if ($height / $ratio > $_GET["imgWH"]) {
				$ratio = $height / $_GET["imgWH"];
				$newheight = $_GET["imgWH"];
				$newwidth = intval($width / $ratio);
				$left = intval(($_GET["imgWH"] - $newwidth) / 2);
				$top = 0;
			}
			else {
				$newwidth = $_GET["imgWH"];
				$newheight = intval($height / $ratio);
				$top = intval(($_GET["imgWH"] - $newheight) / 2);
				$left = 0;
			}
			if ($type == "image/gif") {
				$imgSrc = imagecreatefromgif($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagegif($imgSrc);
			}
			elseif ($type == "image/png") {
				$imgSrc = imagecreatefrompng($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
       	imagealphablending($imgSrc, false);
       	imagesavealpha($imgSrc, true);
       	imagealphablending($imgSrc, true);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagepng($imgSrc);
			}
			else {
				$imgSrc = imagecreatefromjpeg($tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				header("Content-type: " . image_type_to_mime_type($imgSize[2]));
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagejpeg($imgSrc);
			}
			imagedestroy($imgSrc);
		}
		else {
			# Check to see if this file includes exif data which might include a thumbnail...
			$exifinfo = @exif_read_data($_GET["image"]);
			if ($exifinfo["COMPUTED"]["Thumbnail.MimeType"] == "image/jpeg") {
				$exifthumb = @exif_thumbnail($_GET["image"], $exthumbwidth, $exthumbheight, $exthumbtype);
				if ($log == 1) {
					fwrite($flog, $id . " - " . microtime(1) . ": Checking exif data\n");
				}
				if ($exifthumb) {
					if ($log == 1) {
						fwrite($flog, $id . " - " . microtime(1) . ": Got exif data (" . strlen($exifthumb) . " bytes).. checking if it's worth it...\n");
					}
					if ($exthumbwidth >= $_GET["imgWH"] || $exthumbheight >= $_GET["imgWH"]) {
						if ($log == 1) {
							fwrite($flog, $id . " - " . microtime(1) . ": " . $exthumbwidth . " >= " . $_GET["imgWH"] . " || " . $exthumbheight . " >= " . $_GET["imgWH"] . " is True\n");
						}
						# okay, this thumbnail is probably better to make my thumbnail with...
						# now what?
	
						$tmpfname = tempnam($tcache, "thumbnailer") . ".jpg";
						$wrote = file_put_contents($tmpfname, $exifthumb);
						$gototown = 1;
						$id .= " exif thumb";
					}
					elseif ($log == 1) {
						fwrite($flog, $id . " - " . microtime(1) . ": " . $exthumbwidth . " >= " . $_GET["imgWH"] . " || " . $exthumbheight . " >= " . $_GET["imgWH"] . " is False\n");
					}
	
				}
			}
			if ($gototown != 1) {
				$imgSize = @getimagesize($_GET["image"]);
				if (!$imgSize) {
					print("Error, " . $_GET["image"] . " not found");
					return 0;
				}
			}
			else {
				$imgSize = getimagesize($tmpfname);
			}
			$width = $imgSize[0];
			$height = $imgSize[1];
			$type = $imgSize[2];
			if ($_GET["outtype"] != "") {
				$out = strtolower($_GET["outtype"]);
			}
			else {
				$out = image_type_to_mime_type($imgSize[2]);
			}
			$ratio = $width / $_GET["imgWH"];
			if ($height / $ratio > $_GET["imgWH"]) {
				$ratio = $height / $_GET["imgWH"];
				$newheight = $_GET["imgWH"];
				$newwidth = intval($width / $ratio);
				$left = intval(($_GET["imgWH"] - $newwidth) / 2);
				$top = 0;
			}
			else {
				$newwidth = $_GET["imgWH"];
				$newheight = intval($height / $ratio);
				$top = intval(($_GET["imgWH"] - $newheight) / 2);
				$left = 0;
			}
			if ($type == IMAGETYPE_GIF && $gototown == 0) {
				$imgSrc = imagecreatefromgif($_GET["image"]);
			}
			elseif ($type == IMAGETYPE_GIF && $gototown == 1) {
				$imgSrc = imagecreatefromgif($tmpfname);
			}
			elseif ($type == IMAGETYPE_PNG && $gototown == 0) {
				$imgSrc = imagecreatefrompng($_GET["image"]);
#				imagesavealpha($imgSrc, true);
				imagealphablending($imgSrc, true);
			}
			elseif ($type == IMAGETYPE_PNG && $gototown == 1) {
				$imgSrc = imagecreatefrompng($tmpfname);
			}
			elseif ($gototown == 0) {
				$imgSrc = imagecreatefromjpeg($_GET["image"]);
			}
			elseif ($gototown == 1) {
				$imgSrc = imagecreatefromjpeg($tmpfname);
			}
			if ($_GET["fill"] == "") {
				$_GET["fill"] = "no";
			}
			if ($_GET["fill"] == "white") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$white = imagecolorallocate($imgOut, 255, 255, 255);
			}
			elseif ($_GET["fill"] == "black") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$black = imagecolorallocate($imgOut, 0, 0, 0);
			}
			elseif ($_GET["fill"] == "trans") {
				$imgOut = imagecreatetruecolor($_GET["imgWH"], $_GET["imgWH"]);
				if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$ugly = imagecolorallocate($imgOut, 91, 67, 40);
			   $trans = imagecolortransparent($imgOut, $ugly);
			}
			else {
				# no border for you..
				$imgOut = imagecreatetruecolor($newwidth, $newheight);
				if ($type == IMAGETYPE_PNG) {
					$trnprt_indx = imagecolortransparent($imgSrc);
					if ($trnprt_indx >= 0) {
	  	      imagealphablending($imgOut, false);
	 		      imagesavealpha($imgOut, true);
	        	$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
	        	$trnprt_indx  = imagecolorallocatealpha($imgOut, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue'], 127);
	        	imagefill($imgOut, 0, 0, $trnprt_indx);
 	        	imagecolortransparent($imgOut, $trnprt_indx);
						$_GET["nocache"] = 1;
 	      	}
	      	elseif ($type == IMAGETYPE_PNG) {
	        	imagealphablending($imgOut, false);
	        	$color = imagecolorallocatealpha($imgOut, 0, 0, 0, 127);
 	       		imagefill($imgOut, 0, 0, $color);
	        	imagesavealpha($imgOut, true);
	        	imagealphablending($imgOut, true);
					}
      	}
				elseif ($type == IMAGETYPE_GIF) {
					$trnprt_indx = imagecolortransparent($imgSrc);
					if ($trnprt_indx >= 0 && $trnprt_indx < 255) {
	  	      imagealphablending($imgOut, false);
	 		      imagesavealpha($imgOut, true);
	        	$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
	        	$trnprt_indx  = imagecolorallocate($imgOut, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
	        	imagefill($imgOut, 0, 0, $trnprt_indx);
 	        	imagecolortransparent($imgOut, $trnprt_indx);
						$_GET["nocache"] = 1;
					}
				}				
			if (!$imgOut) { $imgOut = imagecreate($_GET["imgWH"], $_GET["imgWH"]); }
				$left = 0;
				$top = 0;
		}
			imagecopyresampled($imgOut, $imgSrc, $left, $top, 0, 0, $newwidth, $newheight, $width, $height);
			if ($out == "image/gif") {
				header("Content-type: image/gif");
				header("Content-disposition: inline; filename=" . time() . ".gif");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagegif($imgOut);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagegif($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			elseif ($out == "image/png") {
#				$white = imagecolorclosest($imgSrc, 255, 255, 255);
#				imagecolortransparent($imgSrc, $white);
				
				header("Content-type: image/png");
				header("Content-disposition: inline; filename=" . time() . ".png");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagepng($imgOut);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagepng($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			else {
				header("Content-type: image/jpeg");
				header("Content-disposition: inline; filename=" . time() . ".jpeg");
				if ($_GET["nocache"] == 1) {
					header("Pragma: no-cache");
					header("Cache-Control: no-cache");
				}
				imagejpeg($imgOut, "", $_GET["quality"]);
				if ($tcache != "" && $_GET["nocache"] == 0) {
					imagejpeg($imgOut, $tcache . md5($_GET["image"]) . date(".Y-m-d.H.i.s.", @filemtime($_GET["image"])) . $_GET["imgWH"] . $_GET["quality"]);
				}
			}
			imagedestroy($imgSrc);
			imagedestroy($imgOut);
			if ($gototown == 1) {
				unlink($tmpfname);
				if (file_exists(substr($tmpfname, 0, -4))) {
					unlink(substr($tmpfname, 0, -4));
				}
			}
			if ($log == 1) {
				$end = microtime(1);
				fwrite($flog, $id . " - " . $end . ": Ended.\n" . $id . " - " . ($end - $start) . " seconds.\n\n");
				fclose($flog);
			}
		}
	}
	/* 
	Code From printeasy 
	$fontbase = "/home/printsmadeeasy/fonts/";
	$fontfile = $fontbase . "arial.ttf";
	##--  Modify the following parameters or get them through the URL --##
	##--  Make sure to substitute line breaks with the special code   -br-   The reason is to standardize the newline codes which could be a \n, \r, or 
	$TextString = "This is a test to see-br-if it is working!";
	$rotateDegrees = 0;
	$textsize = 20;
	$textalign = "right";
	#-- Make the text green --#
	#-- Let the function know it is in Hex format with the "0x"
	$textColorObj = GetRGBvalues("0xCC0000", false);
	##-- Figure out how big to create the image based on our font size and rotation --##
	##-- This function call may not be needed for your application if you already know how big the image should be and where the text is going --#
	$ImageSizeInfoArr = GetImageSizeForTextBlock($rotateDegrees, $TextString, $fontfile, $textsize);
	##-- Create the Image so we can place text inside of it.  			--##
	$imagePointer = imagecreate ($ImageSizeInfoArr["WIDTH"], $ImageSizeInfoArr["HEIGHT"]);
	//$imagePointer = imagecreate (300, 300);
	#-- Make the background white --#
	$bkgc = ImageColorAllocate ($imagePointer, 255, 255, 255);
	#-- Write text onto the image with the function below --#
	#-- You may need to put this function call into a loop if you are going to write multiple blocks of text on the same image.  --#
	DrawTextBlock($imagePointer, $ImageSizeInfoArr["X_POS"], $ImageSizeInfoArr["Y_POS"], $TextString, $fontfile, $textsize, $rotateDegrees, $textalign, $textColorObj);
	//DrawTextBlock($imagePointer, 20, 80, $TextString, $fontfile, $textsize, $rotateDegrees, $textalign, $textColorObj);
	#-- Send the image back to the browser --#
	Header ("Content-type: image/jpeg");
	imagejpeg ($imagePointer, "", 100);
	ImageDestroy ($imagePointer);
	#########-----------    Functions Below  ----------#############
	##-- This function will draw text onto an image.  The image should have already been created with the GD library.  We are passing it into this function by reference --#
	##-- Text will be generated on the image.  The function does not return anything 	--#
	##-- The other parameters define the text block, position, and properties			--#
	##-- The X & Y coodinates are where the registration point of the FIRST letter on the FIRST line should go... (if the text is left aligned) --#
	function DrawTextBlock(&$im, $Start_Text_Coord_x, $Start_Text_Coord_y, $TextString, $fontLocation, $fontSize, $rotationAngle, $alignment, $fontColorObj){
		#-- Clean out all of the "new line" characters. However, there should not be any --#
		#-- This function expects the special code of "-br-" for line breaks. 		 --#
		$TextString = preg_replace("/(\n|\r)/", "", $TextString);
		##-- Put each line into separate array elements. --##
		$IndividualLinesArr = split("-br-", $TextString);
		##-- Put the rotation angle into the form of radians for use with PHP's trigonometric functions --##
		$radians = M_PI / 180 * $rotationAngle;
		$LineInformation = array();
		##-- Fill up a 2D hash that will contain measurements for each line of the text block --##
		for($i=0; $i0){
				$LineInformation[$i]["VertDistToLineAbove"] = GetVerticalDistance($IndividualLinesArr[$i-1], $IndividualLinesArr[$i], $fontSize, $fontLocation);
			}
			$LineInformation[$i]["LineWidth"] = GetLineWidth($IndividualLinesArr[$i], $fontSize, $fontLocation);
		}
		##-- We can figure out how big the maximum line with is by measuring the total width of the text block BEFORE rotation --##
		$MaxLineWidth = GetLineWidth($TextString, $fontSize, $fontLocation);
		##--  Set $New_Coord_X and $New_Coord_Y to the start position of the first letter.  	--##
		##--  These variables will get overwritten every time we loop to the next line.		--##
		$New_Coord_X = $Start_Text_Coord_x;
		$New_Coord_Y = $Start_Text_Coord_y;
		##-- Loop through all lines within the text block --##
		for($i=0; $i0){
				$New_Coord_X += sin($radians) * $LineInformation[$i]["VertDistToLineAbove"];
				$New_Coord_Y += cos($radians) * $LineInformation[$i]["VertDistToLineAbove"];
			}
			##-- Find out where the first letter of the line is, relative to where the bounding box starts. 	--##
			##-- Skinny letters like "i" have the bounding box hugging tight around each pixel in the letter. 	--##
			##-- But the registration point for the coordinates may be several pixels away. 			--##
			##-- Letters like "g" have the registration point for the coordinates slightly above bounding box 	--##
			##-- because of the hook on the bottom of the letter. 							--##
			$TextBoxSizeForMargins = ImageTTFBBox ($fontSize, 0, $fontLocation, $IndividualLinesArr[$i]);
			$HangingBottomMargin = $TextBoxSizeForMargins[1];
			$HangingLeftMargin = $TextBoxSizeForMargins[0];
			##-- The current line that we are on needs be be shifted to account for the aligment 		--##										--##
			##-- By caclulating the max line with, the current line width, and the angle of rotation...	--##
			##-- we know how far to slide the text line 							--##
			if(strtoupper($alignment) == "CENTER"){
				$hypotenuse = $MaxLineWidth/2 - $LineInformation[$i]["LineWidth"]/2 - $HangingLeftMargin -2;
				$TextLinePlacment_X = $New_Coord_X + cos($radians) * $hypotenuse;
				$TextLinePlacment_Y = $New_Coord_Y - sin($radians) * $hypotenuse;
			}
			else if(strtoupper($alignment) == "RIGHT"){
				$hypotenuse = $MaxLineWidth - $LineInformation[$i]["LineWidth"] - $HangingLeftMargin -2;
				$TextLinePlacment_X = $New_Coord_X + cos($radians) * $hypotenuse;
				$TextLinePlacment_Y = $New_Coord_Y - sin($radians) * $hypotenuse;
			}
			else{
				$hypotenuse = -1;
				$TextLinePlacment_X = $New_Coord_X + cos($radians) * $hypotenuse;
				$TextLinePlacment_Y = $New_Coord_Y - sin($radians) * $hypotenuse;
			}
			##-- After the text has been shifted for alignment we can adjust the vertical spacing from the line above it. 	--##
			##-- The hanging margin is for letters like "g" or "j" ect.							--##
			$TextLinePlacment_X -= sin($radians) * $HangingBottomMargin;
			$TextLinePlacment_Y -= cos($radians) * $HangingBottomMargin;
			#-- Create the color for the text  --#
			$txtc = ImageColorAllocate ($im, $fontColorObj->red, $fontColorObj->green, $fontColorObj->blue);
			##-- Write the text line onto the image. --##
			ImageTTFText ($im, $fontSize, $rotationAngle, $TextLinePlacment_X, $TextLinePlacment_Y, -($txtc), $fontLocation, $IndividualLinesArr[$i]);
		}
	}
	##-- This function will calculate the verical distance between two lines.  --##
	function GetVerticalDistance($TextLineOne, $TextLineTwo, $fontSize, $fontLocation){
		$TxtBxSz_1st_line = ImageTTFBBox ($fontSize, 0, $fontLocation, $TextLineOne);
		$TxtBxSz_2nd_line = ImageTTFBBox ($fontSize, 0, $fontLocation, $TextLineTwo);
		$TxtBxSz_both_lines = ImageTTFBBox ($fontSize, 0, $fontLocation, $TextLineOne . "\n\r" . $TextLineTwo);
		$FirstLine_height = $TxtBxSz_1st_line[1] - $TxtBxSz_1st_line[7];
		$BothLines_height = $TxtBxSz_both_lines[1] - $TxtBxSz_both_lines[7];
		return $BothLines_height - $FirstLine_height;
	}
	##-- This function will return the Maximum line with of a text block.  It can be single line or multi line. --##
	function GetLineWidth($TextData, $fontSize, $fontLocation){
		$TxtBxSz = ImageTTFBBox ($fontSize, 0, $fontLocation, preg_replace("/-br-/", "\n\r", $TextData));
		return $TxtBxSz[2] - $TxtBxSz[0];
	}
	#-- Parameter 1 is the Color code.  It can be in Hex or decimal format ---#
	#-- 	If you are going to pass in a string with a HEX number.. do not just pass in "006600", instead pass in "0x006600"
	#-- Parameter 2 is a boolean value.  Indicating whether the returned color values should be in Hex or decimal format. --#
	function GetRGBvalues($colorCode, $hexYes){
		#-- If it is a string.. check if it is in Hex format or not.  Then convert string to integer.
		if(is_string($colorCode)){
			if(substr($colorCode,0,2) == "0x"){
				$colorValue = intval($colorCode, 16);
			}
			else{
				$colorValue = intval($colorCode);
			}
		}
		else{
			$colorValue = $colorCode;
		}
		if($colorValue > intval("0xFFFFFF", 16)){
			print "Color code is out of range";
			exit;
		}
		##-- This will ensure the values are 6 digits... EX  "0000FF"  instead of just "FF"
		$zeros = "000000";
		$rgbString = substr($zeros, 0, 6 - strlen (dechex($colorValue))) . dechex($colorValue);
		#-- Create a new color object to hold the values --#
		$colorObj = new ColorCode();
		if($hexYes){
			$colorObj->red = substr($rgbString,0,2);
			$colorObj->green = substr($rgbString,2,2);
			$colorObj->blue = substr($rgbString,4,2);
		}
		else{
			$colorObj->red = intval(substr($rgbString,0,2),16);
			$colorObj->green = intval(substr($rgbString,2,2),16);
			$colorObj->blue = intval(substr($rgbString,4,2),16);
		}
		return $colorObj;
	}
	##-- Define a class so that we can neatly get a Return of data from our GetRGBvalues function --##
	class ColorCode {
		var $red;
		var $green;
		var $blue;
	}
	##-- This function may not be needed for your application --#
	##-- Its purpose is to figure out how big to create an image so that we can fit a text block inside --#
	##-- Also we want to know the X & Y coordinates of where to start writing the text --#
	##-- It returns a hash with 4 keys for ..... WIDTH, HEIGHT, X_POS, Y_POS
	function GetImageSizeForTextBlock($rotateDegrees, $TextString, $fontLocation, $fontSize){
		##-- This will add a small margin area around the text block.  	--##
		##-- The margin grows with the font size.			--##
		$margin = intval(0.25 * $fontSize);
		##-- Convert the angle so it is always a number between 0 and 359 --##
		if($rotateDegrees > 359 || $rotateDegrees < -359){
			$rotateDegrees = intval($rotateDegrees % 360);
		}
		if($rotateDegrees < 0){
			$rotateDegrees = 360 + $rotateDegrees;
		}
		#-- Clean out all of the "new line" characters.  --#
		#-- The following function expects the special code of "-br-" for line breaks.  --#
		$TextString = preg_replace("/(\n|\r)/", "", $TextString);
		##-- Get the total size of the text block so that we know how big to create the Image --##
		$TextBoxSize = ImageTTFBBox ($fontSize, $rotateDegrees, $fontLocation, preg_replace("/-br-/", "\n\r", $TextString));
		##-- Put the variables into reader-friendly variables --##
		$TxtBx_Lwr_L_x = $TextBoxSize[0];
		$TxtBx_Lwr_L_y = $TextBoxSize[1];
		$TxtBx_Lwr_R_x = $TextBoxSize[2];
		$TxtBx_Lwr_R_y = $TextBoxSize[3];
		$TxtBx_Upr_R_x = $TextBoxSize[4];
		$TxtBx_Upr_R_y = $TextBoxSize[5];
		$TxtBx_Upr_L_x = $TextBoxSize[6];
		$TxtBx_Upr_L_y = $TextBoxSize[7];
		##-- The Text Box coordinates are relative to the font, regardless of the angle 		--##
		##-- We need to figure out the height and width of the text box accounting for the rotation 	--##
		if($rotateDegrees <= 90 || $rotateDegrees >= 270 ){
			$TextBox_width = max($TxtBx_Lwr_R_x, $TxtBx_Upr_R_x) - min($TxtBx_Lwr_L_x, $TxtBx_Upr_L_x);
			$TextBox_height = max($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y) - min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y);
			##-- This figures out where the coordinates of the first letter in the text block starts	--##
			##-- It is roughly the lower left-hand corner letter 					--##
			$Start_Text_Coord_x = -(min($TxtBx_Upr_L_x, $TxtBx_Lwr_L_x));
			$Start_Text_Coord_y = -(min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y));
		}
		else{
			$TextBox_width = max($TxtBx_Lwr_L_x, $TxtBx_Upr_L_x) - min($TxtBx_Lwr_R_x, $TxtBx_Upr_R_x);
			$TextBox_height = max($TxtBx_Upr_R_y, $TxtBx_Upr_L_y) - min($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y);
			$Start_Text_Coord_x = -(min($TxtBx_Lwr_R_x,$TxtBx_Upr_R_x));
			$Start_Text_Coord_y = -(min($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y));
		}
		##-- We need to add our margin to the coordinates of the first letter --##
		$Start_Text_Coord_x += $margin;
		$Start_Text_Coord_y += $margin - 2;  //Don't forget to account for the '0th' pixel at Y-coord 0
		##-- We are going to make the image just big enough to hold our text block... accounting for the rotation and font size. --##
		##-- We times the Margin by 2 so that there is a margin on all 4 sides   	--##
		$TotalImageWidth = $TextBox_width + $margin *2;
		$TotalImageHeight = $TextBox_height + $margin *2;
		##-- Send back a hash with our calculations --#
		return array("WIDTH"=>$TotalImageWidth, "HEIGHT"=>$TotalImageHeight, "X_POS"=>$Start_Text_Coord_x, "Y_POS"=>$Start_Text_Coord_y);
	}
	*/
?>