<?php
/**
 * 自动缩略图
 *
 * @author Wzb
 */
class COM_CreatMiniature {
	// 公共变量
	var $srcFile = ""; // 原图
	var $echoType; // 输出图片类型，link--不保存为文件；file--保存为文件
	var $im = ""; // 临时变量
	var $srcW = ""; // 原图宽
	var $srcH = ""; // 原图高
	var $isSupport = true; // GD库是否支持
	                       
	// 设置变量及初始化
	function SetVar($srcFile, $echoType) {
		if (! file_exists ( $srcFile )) {
			echo '源图片文件不存在!';
			exit ();
		}
		$this->srcFile = $srcFile;
		$this->echoType = $echoType;
		$info = "";
		$data = GetImageSize ( $this->srcFile, $info );
		switch ($data [2]) {
			case 1 :
				if (! function_exists ( "imagecreatefromgif" )) {
					$this->isSupport = false;
					return;
				}
				$this->im = ImageCreateFromGIF ( $this->srcFile );
				break;
			case 2 :
				if (! function_exists ( "imagecreatefromjpeg" )) {
					$this->isSupport = false;
					return;
				}
				$this->im = ImageCreateFromJpeg ( $this->srcFile );
				break;
			case 3 :
				if (! function_exists ( "imagecreatefrompng" )) {
					$this->isSupport = false;
					return;
				}
				$this->im = ImageCreateFromPNG ( $this->srcFile );
				break;
			default :
				$this->isSupport = false;
				return;
				break;
		}
		$this->srcW = ImageSX ( $this->im );
		$this->srcH = ImageSY ( $this->im );
	}
	
	// 生成背景填充的缩图
	function BackFill($toFile, $toW, $toH, $bk1 = 255, $bk2 = 255, $bk3 = 255) {
		if (! $this->isSupport) {
			copy ( $this->srcFile, $toFile );
			return;
		}
		if ($this->srcW <= $toW && $this->srcH <= $toH) {
			$ftoW = $this->srcW;
			$ftoH = $this->srcH;
		} else {
			$toWH = $toW / $toH;
			$srcWH = $this->srcW / $this->srcH;
			if ($toWH <= $srcWH) {
				$ftoW = $toW;
				$ftoH = $ftoW * ($this->srcH / $this->srcW);
			} else {
				$ftoH = $toH;
				$ftoW = $ftoH * ($this->srcW / $this->srcH);
			}
		}
		$ftoW = intval ( $ftoW ) <= 0 ? 1 : intval ( $ftoW );
		$ftoH = intval ( $ftoH ) <= 0 ? 1 : intval ( $ftoH );
		
		if (function_exists ( "imagecreatetruecolor" )) {
			@$cImg = ImageCreateTrueColor ( $toW, $toH );
			@$cImgTemp = ImageCreateTrueColor ( $this->srcW, $this->srcH );
			if (! $cImg) {
				$cImg = ImageCreate ( $toW, $toH );
			}
			if (! $cImgTemp) {
				$cImgTemp = ImageCreate ( $this->srcW, $this->srcH );
			}
		} else {
			$cImg = ImageCreate ( $toW, $toH );
			$cImgTemp = ImageCreate ( $this->srcW, $this->srcH );
		}
		
		$backcolor = imagecolorallocate ( $cImg, $bk1, $bk2, $bk3 ); // 填充的背景颜色
		$backcolorTemp = imagecolorallocate ( $cImgTemp, $bk1, $bk2, $bk3 ); // 填充的背景颜色
		ImageFilledRectangle ( $cImg, 0, 0, $toW, $toH, $backcolor );
		ImageFilledRectangle ( $cImgTemp, 0, 0, $this->srcW, $this->srcH, $backcolorTemp );
		ImageCopy ( $cImgTemp, $this->im, 0, 0, 0, 0, $this->srcW, $this->srcH );
		$this->im = $cImgTemp;
		
		if ($this->srcW > $toW || $this->srcH > $toH) {
			$proImg = $this->CreatImage ( $this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH );
			if ($ftoW < $toW) {
				ImageCopy ( $cImg, $proImg, ($toW - $ftoW) / 2, 0, 0, 0, $ftoW, $ftoH );
			} else if ($ftoH < $toH) {
				ImageCopy ( $cImg, $proImg, 0, ($toH - $ftoH) / 2, 0, 0, $ftoW, $ftoH );
			} else {
				ImageCopy ( $cImg, $proImg, 0, 0, 0, 0, $ftoW, $ftoH );
			}
		} else {
			ImageCopyMerge ( $cImg, $this->im, ($toW - $ftoW) / 2, ($toH - $ftoH) / 2, 0, 0, $ftoW, $ftoH, 100 );
		}
		return $this->EchoImage ( $cImg, $toFile );
		ImageDestroy ( $cImg );
	}
	
	// 创建图片
	function CreatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH) {
		$iscreat = false;
		if (function_exists ( "imagecreatetruecolor" )) {
			@$creatImg = ImageCreateTrueColor ( $creatW, $creatH );
			if ($creatImg) {
				ImageCopyResampled ( $creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH );
				$iscreat = true;
			}
		}
		if (! $iscreat) {
			$creatImg = ImageCreate ( $creatW, $creatH );
			ImageCopyResized ( $creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH );
		}
		return $creatImg;
	}
	
	// 输出图片，link---只输出，不保存文件。file--保存为文件
	function EchoImage($img, $to_File) {
		switch ($this->echoType) {
			case "link" :
				if (function_exists ( 'imagejpeg' ))
					return ImageJpeg ( $img );
				else
					return ImagePNG ( $img );
				break;
			case "file" :
				if (function_exists ( 'imagejpeg' ))
					return ImageJpeg ( $img, $to_File, 100 );
				else
					return ImagePNG ( $img, $to_File, 100 );
				break;
		}
	}
}
?>