gusucode.com > 同城苏州黄页系统php源码程序 > lib/piczoom.class.php

    <?
# 定义常量
define("ERR_NOPIC",-1);
define("ERR_NOPIC_STR","图片路径错误");

define("ERR_NOTPIC",-2);
define("ERR_NOTPIC_STR","不是合法的文件");

define("ERR_NOTPYE",-3);
define("ERR_NOTPYE_STR","不是支持的图片格式");

define("ERR_LOADBAD",-4);
define("ERR_LOADBAD_STR","图片载入失败");



/**
 * 对图像进行缩放,只有两个函数,构造函数 和 get_pic
 *
 * @author		alee<aleee201@163.com>
 * @version	第一个版本		2005/4/26
 * @version	1.1.0			2005/4/26		增加 cutBorder 裁减方式
 */
class piczoom
{
# 属性
##############################################################

	var $picscr		= "";
	var $type		= "";
	var $picw		= 0;
	var $pich		= 0;
	var $zoomw		= 0;
	var $zoomh		= 0;
	var $zoomtype	= "";
	var $allZoomType= array("mini","LT","MT","RT","LC","MC","RC","LB","MB","RB","CB");
	var $rw			= 0;
	var $rh			= 0;
	var $coN		= "";
	var $allCoN		= array("255","TrueColor");
	
	var $pic;
	var $newpic;

	var $err		= "";
	var $errn		= 0;

# 私有函数
##############################################################


	function createpic()
	{
		if($this->coN=="255")
			$this->newpic=imagecreate($this->rw,$this->rh);
		else
		{
			$this->newpic=imagecreatetruecolor($this->rw,$this->rh);
		}
	}


	function zoomsize()
	{
	
	
		if(($this->picw>=$this->zoomw)&&($this->pich<=$this->zoomh))
		{
			//print "1";
			$this->rw=$this->zoomw;
			$this->rh=floor($this->pich*$this->rw/$this->picw);
		}
		else if(($this->picw<=$this->zoomw)&&($this->pich>=$this->zoomh))
		{
			//print "2";
			$this->rh=strval($this->zoomh);
			$this->rw=floor($this->picw*$this->rh/$this->pich);
		}
		else if(($this->picw<$this->zoomw)&&($this->pich<$this->zoomh))
		{
			//print "3";
			$this->rw=$this->picw;
			$this->rh=$this->pich;
		}
		else if(($this->picw>$this->zoomw)&&($this->pich>$this->zoomh))
		{
			if(($this->picw/$this->pich)>=($this->zoomw/$this->zoomh))
			{
				//print "4";
				$this->rw=$this->zoomw;
				$this->rh=floor($this->pich*$this->rw/$this->picw);
			}
			else
			{
				//print "5";
				$this->rh=strval($this->zoomh);
				$this->rw=floor($this->picw*$this->rh/$this->pich);
			}
		}
	
	}
  
	function mini()
	{
		$this->zoomsize();
		// echo $this->rw;echo $this->rh;
		$this->createpic();
		imagecopyresampled ( $this->newpic,$this->pic, 0, 0, 0, 0, $this->rw,$this->rh,$this->picw,$this->pich);
	}
	
	function cut()
	{
		if($this->zoomw>$this->picw)
			$this->rw=$this->picw;
		else
			$this->rw=$this->zoomw;
		if($this->zoomh>$this->pich)
			$this->rh=$this->pich;
		else
			$this->rh=$this->zoomh;
		
		$x=0;
		$y=0;
		
		if( preg_match("/^M/",$this->zoomtype) )
			$x=($this->picw-$this->rw)/2;
		else if( preg_match("/^R/",$this->zoomtype) )
			$x=$this->picw-$this->rw;

		if( preg_match("/C$/",$this->zoomtype) )
			$y=($this->pich-$this->rh)/2;
		else if( preg_match("/B$/",$this->zoomtype) )
			$y=$this->pich-$this->rh;

		
		$this->createpic();
		//echo "$x,$y";
		imagecopyresized ( $this->newpic,$this->pic,0,0,$x,$y,$this->rw,$this->rh,$this->rw,$this->rh);

	}

	function cutBorder()
	{
		
		$allowScale=$this->zoomw/$this->zoomh;
		$thisScale=$this->picw/$this->pich;
		$this->rw=$this->zoomw;
		$this->rh=$this->zoomh;

		# 实际形状比需要缩放的形状看起来高,保留宽,剪掉多余的高
		if($thisScale<$allowScale)
		{
			$copyw=$this->picw;
			$copyh=($this->zoomh/$this->zoomw)*$copyw;
		}
		# 实际形状比需要缩放的形状看起来宽,保留高,剪掉多余的宽
		elseif($thisScale>$allowScale)
		{
			$copyh=$this->pich;
			$copyw=($this->zoomw/$this->zoomh)*$copyh;
		}
		# 实际形状与需要缩放形状一致
		else
		{
			$copyw=$this->picw;
			$copyh=$this->pich;
		}

		//echo "原图保留:{$copyw}x{$copyh}<br>";
		$this->createpic();
		imagecopyresampled ( $this->newpic,$this->pic, 0, 0, 0, 0,$this->rw,$this->rh, $copyw,$copyh);

	}

	function print_pic($outFile="--none--")
	{

		if($outFile=="--none--")					// 直接输出
		{
			switch ($this->type)
			{
				case IMAGETYPE_GIF:

					header ("Content-type: image/gif");
					imagegif($this->newpic);
					break;
				case IMAGETYPE_JPEG:
					header ("Content-type: image/jpeg",'',100);
					imagejpeg($this->newpic);
					break;
			}
		}
		else										// 输出到文件
		{

			switch ($this->type)
			{
				case IMAGETYPE_GIF:

					imagegif($this->newpic,$outFile);
					break;
				case IMAGETYPE_JPEG:

					imagejpeg($this->newpic,$outFile,100);
					break;
			}
		}


	}

	function outError($msg)
	{
		print str_replace("\n","<br>",$msg);
		exit();
	}

	function checkParameter($w,$h,$zoom,$coN)
	{
		if( $w<=0 or $h<=0 )
		{
			$this->outError("请求缩略图片的宽高分别为:{$w}、{$h}。\n它们必须都是大于0的整数。");
			return false;
		}

		if(!in_array($zoom,$this->allZoomType))
		{
			$this->outError("意外的缩略方式:{$zoom}。\n缩略方式必须为以下内容:\n".implode("、",$this->allZoomType));
			return false;
		}

		
		if(!in_array($coN,$this->allCoN))
		{
			$this->outError("意外的图像品质参数:{$coN}。\n该参数必须为以下内容:\n".implode("、",$this->allCoN));
			return false;
		}
	}
# 公有函数
##############################################################

	# 构造函数
	function piczoom($scr)
	{
		/*if( !file_exists($scr) )
		{
			$this->errn=ERR_NOPIC;
			$this->err=ERR_NOPIC_STR;

			return false;			
		}*/

		$this->picscr=$scr;
		$size=getimagesize($scr);

		if( !$size[2] )
		{
			$this->errn=ERR_NOTPIC;
			$this->err=ERR_NOTPIC_STR;

			return false;			
		}
		
		if( $size[2]== IMAGETYPE_GIF or $size[2]== IMAGETYPE_JPEG )
			$this->type=$size[2];
		else
		{
			$this->errn=ERR_NOTPYE;
			$this->err=ERR_NOTPYE_STR;

			return false;
		}

		
		$this->picw=$size[0];
		$this->pich=$size[1];
		
		if($this->type == IMAGETYPE_GIF)
			$this->pic=imagecreatefromgif($this->picscr);
		else if($this->type == IMAGETYPE_JPEG)
			$this->pic=imagecreatefromjpeg($this->picscr);
		
		if(!$this->pic)
		{
			$this->errn=ERR_LOADBAD;
			$this->err=ERR_LOADBAD_STR;

			return false;		
		}
	
		return true;

	}

	
	function get_pic($w,$h,$zoom="mini",$coN="255",$outFile="--none--")
	{

		// 检查参数
		if($this->checkParameter($w,$h,$zoom,$coN))
			return false;

		// 开始

		$this->zoomw=$w;
		$this->zoomh=$h;
		$this->zoomtype=$zoom;
		$this->coN=$coN;


		if ( $this->zoomtype=="mini" )
			$this->mini();
		else if(preg_match("/^CB/",$this->zoomtype))
			$this->cutBorder();
		else
			$this->cut();
			
		
		$this->print_pic($outFile);
	}
}

#############################################################################





#######################################################
##
##	增加缓存功能的图片缩略类
##	由 piczoom 类继承
##
##	alee alee201@163.com 2005-7-5
##
#######################################################

class piczoom_cache extends piczoom
{

# 属性
##############################################################
	var $cache_dir		= "./cache_zoomPic/";
	var $http_Path		= "";
	var $validTime		= 432000;				// 默认为5天
	var $ifLongValid	= false;



# 私有函数
##############################################################

	function createCachePic($w,$h,$zoom,$coN,$fileName)
	{
		$this->get_pic($w,$h,$zoom,$coN,$fileName);
	}

# 公有函数
##############################################################
	function setCacheDir($dir,$http_Path="")
	{
		if(!preg_match("/\/$/",$dir))
			$dir.="/";
		if(!preg_match("/\/$/",$http_Path) and $http_Path!="")
			$http_Path.="/";


		$this->cache_dir=$dir;
		$this->http_Path=$http_Path;

		if(!file_exists($dir))
			mkdir($dir);
	}

	function setValidTime($time)
	{
		$this->validTime=$time;
	}

	function ifLongValid($long=true)
	{
		$this->validTime=0;
		$this->ifLongValid=$long;
	}

	/*
	* 缓存文件名称的格式:原图片地址(urlencode).图片宽.图片高.缩略方式.色彩值.zmPic
	*/
	function getCachePic($w,$h,$zoom="mini",$coN="255")
	{
		// -- 检查参数 --
		if($this->checkParameter($w,$h,$zoom,$coN))
			return false;
		
		// -- 开始 --

		$cacheFileName=base64_encode($this->picscr).".".$w.".".$h.".".$zoom.".".$coN.".zmPic";
		$cacheFile=$this->cache_dir.$cacheFileName;

		if(file_exists($cacheFile))
		{

			if( $this->ifLongValid or filemtime($cacheFile)+$this->validTime > time() )
				// 重定向到 缓存文件
				header("Location: {$this->http_Path}{$cacheFileName}");
			else
			{	
				// 缓存文件以过期,重建缓存文件


				// 删除过期的 缩略图
				unlink($cacheFile);


				// 创建新的 缩略图
				$this->createCachePic($w,$h,$zoom,$coN,$cacheFile);


				// 递归调用 getCachePic ,获取新产生的 缩略图
				$this->getCachePic($w,$h,$zoom,$coN);
			}

		}
		else
		{
			// 创建新的 缩略图
			$this->createCachePic($w,$h,$zoom,$coN,$cacheFile);

			// 递归调用 getCachePic ,获取新产生的 缩略图
			$this->getCachePic($w,$h,$zoom,$coN);
		}
	}
}

#############################################################################
?>