<?
class Thumbs {



function Thumbs() {
	global $thumbsDirName, $sys;
	$this->thumbsDirName = $thumbsDirName;
	if ($sys['viewStyle'] == "icons") {
		$this->currentImgHeight = "32";
		$this->currentImgWidth = "42";
	} else {
		$this->currentImgHeight = "68";
		$this->currentImgWidth = "90";
	}
}

function thumb ($src, $width, $height, $fixedSize = false) {
	if (strstr($src, $this->thumbsDirName) == false) {
		if (!$this->isCached($src, $width, $height)) {
			$this->makeThumb($src, $width, $height, false, $fixedSize);
		}
		$this->outputCache($src, $width, $height);
	} else {
		$this->makeThumb($src, $width, $height, $output = true, $fixedSize);
	}
}

function outputString($string, $width, $height) {
	$img = ImageCreate($width,$height);
	$bg = ImageColorAllocate($img,0,0,0);
	$tx = ImageColorAllocate($img,255,255,255);
	ImageFilledRectangle($img,0,0,$width,$height,$bg);
	ImageString($img,1,1,1,$string,$tx);
	if (imagetypes() & IMG_PNG) {
		header("content-type: image/png");
		imagePng($img);
	} else if (imagetypes() & IMG_JPG) {
		header("content-type: image/jpeg");
		imageJpeg($img,"","100");
	} else if (imagetypes() & IMG_GIF) {
		header("content-type: image/gif");
		imageGIF($img);
	}
}

function renameCache($src, $newfilename) {
	global $fm;
	$fm->rename($this->getImgThumbDir($src), $newfilename);
}

function clearCache($src) {
	global $fm;
	$fm->delete($this->getImgThumbDir($src));
	
	/* delete temp thumb folder if empty */
	$tmpfolderpath = gluePath($fm->dirname($src),$this->thumbsDirName);
	$tmpfoldersize = $fm->getDirSize($tmpfolderpath);
	if ($tmpfoldersize < 1) {
		$fm->delete($this->getImgThumbDir($tmpfolderpath));
	}
}

function outputCache($src, $width, $height) {
	global $fm;
	$thumbfile = $this->getThumbPath($src, $width, $height);
	if (imagetypes() & IMG_PNG) {
	 	header("Content-Type: image/png");
		@readfile($thumbfile);
	} else if (imagetypes() & IMG_JPG) {
		header("Content-Type: image/jpeg");
		@readfile($thumbfile);
	}	else if (imagetypes() & IMG_GIF) {
		header("Content-Type: image/gif");
		@readfile($thumbfile);
	}
}


function isCached($src, $width, $height) {
	clearstatcache();
	$thumb = $this->getThumbPath($src, $width, $height);
	if (file_exists($thumb)) {
		return true;
	} else {
		return false;
	}
}

function getImgThumbDir($src) {
	global $fm;
	return gluePath($fm->dirname($src),$this->thumbsDirName,$fm->basename($src),"/");
}

function getThumbPath($src, $width, $height) {
	global $fm;
	return gluePath($fm->dirname($src),$this->thumbsDirName,$fm->basename($src),"/",$this->getThumbFilename($width, $height));
}

function getThumbFilename($width, $height){
	return $width."_".$height.".png";
}

function makeThumb($src, $maxWidth, $maxHeight, $output=false, $fixedSize=false) {
	global $fm;
		  $srcSize = getImageSize($src);
		  // image dest size $destSize[0] = width, $destSize[1] = height
		  $srcRatio = $srcSize[0]/$srcSize[1]; // width/height ratio
		  $destRatio = $maxWidth/$maxHeight;
		  if ($destRatio > $srcRatio) {
				$destSize[1] = $maxHeight;
				$destSize[0] = $maxHeight*$srcRatio;
		  } 
		  else {
				$destSize[0] = $maxWidth; 
				$destSize[1] = $maxWidth/$srcRatio; 
		  }
		
		if (($srcSize[0] < $maxWidth) && ($srcSize[1] < $maxHeight)) {
			$destSize[0] = $srcSize[0];
		}
		
		if (($srcSize[1] < $maxHeight) && ($srcSize[0] < $maxWidth)) {
			$destSize[1] = $srcSize[1];
		}
		  // true color image, with anti-aliasing 

		if ($destSize[0] < 1) {$destSize[0] = 1;}
		if ($destSize[1] < 1) {$destSize[1] = 1;}

		if (!$fixedSize) {
		 $destImage = @imagecreatetruecolor($destSize[0],$destSize[1]);
		} else {
		 $destImage = @imagecreatetruecolor($maxWidth,$maxHeight);
		}
		  
		@imageAntiAlias($destImage,true);
		//white background

		$white = @imagecolorallocate($destImage, 255, 255, 254);
		imagecolortransparent($destImage, $white);
		@imagefill($destImage, 0, 0, $white);

		  // src image
		  switch ($srcSize[2]) {
				case 1: //GIF
				$srcImage = imageCreateFromGif($src);
				break;
				
				case 2: //JPEG
				$srcImage = imageCreateFromJpeg($src);
				break;
				
				case 3: //PNG
				$srcImage = imageCreateFromPng($src);
				break;
				
				default:
				return false;
				break;
		  }
		  // resampling 
		 
		 

		 if ($srcSize[0] < $maxWidth) {
		 $dstX = (($maxWidth/2)-($srcSize[0]/2));
		 } else {
		 $dstX = 0;
		 }
		 if ($srcSize[1] < $maxHeight) {
		 $dstY = (($maxHeight/2)-($srcSize[1]/2));
		 } else {
		 $dstY = 0;
		 }
		 
		 if ($destSize[0] < $maxWidth) {
		 $dstX = (($maxWidth/2)-($destSize[0]/2));
		 }
		 if ($destSize[1] < $maxHeight) {
		 $dstY = (($maxHeight/2)-($destSize[1]/2));
		 }
		
		  
		  if (!$fixedSize) {
		@imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
		  } else {
		@imageCopyResampled($destImage, $srcImage, $dstX, $dstY, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
		  }



		  // generating image to file or browser
		if (!$output) {
			clearstatcache();
			$thumbfile = $this->getThumbPath($src, $maxWidth, $maxHeight);
			if (!is_dir($fm->dirname($thumbfile))) {
				global $fm;
				$fm->createPath($fm->dirname($thumbfile), 755);
			}
			 if (imagetypes() & IMG_PNG) {
				@imagePng($destImage, $thumbfile);
			} else if (imagetypes() & IMG_JPG) {
				@imageJpeg($destImage, $thumbfile,"100");
			} else if (imagetypes() & IMG_GIF) {
				@imageGIF($destImage, $thumbfile);
			}
		} else {
			if (imagetypes() & IMG_PNG) {
				header("Content-type: image/png");
				@imagePng($destImage);
			} else if (imagetypes() & IMG_JPG) {
				header("Content-type: image/jpeg");
				@imageJpeg($destImage,"","100");
			} else if (imagetypes() & IMG_GIF) {
				header("Content-type: image/gif");
				@imageGIF($destImage);
			}
		}
}

}
?>