PHP图片水印缩放类实现

<?php
class Image{

	private $path;


	function __construct($path='./'){
		$this->path=rtrim($path,'/').'/';
	}

	//水印函数,参数:背景图,水印图,位置,前缀,TMD透明度

	public function water($b,$l,$pos=9,$prefix='wa_',$tmd=100){
		//拼接路径
		$b=$this->path.$b;
		$l=$this->path.$l;
		//判断路径
		if(file_exists($b)&&file_exists($l)){
		//获取信息
		$binfo=self::getImageInfo($b);
		$linfo=self::getImageInfo($l);
		//判断大小
		if(!self::checkSize($binfo,$linfo)){
			exit('背景图片大小小于水印图片大小');
		}
		//取得位置
		$position=self::getPosition($binfo,$linfo,$pos);
		//打开资源
		$bRes=self::openImg($b,$binfo['type']);
		$lRes=self::openImg($l,$linfo['type']);
		//合成
		$newRes=self::mergeImg($bRes,$lRes,$linfo,$position,$tmd);
		//保存

		if($name=self::saveImg($newRes,$this->path,$prefix,$binfo)){
			return $name;
		}
		
		}else{
			echo '路径不存在';
			exit;
		}
	}



	//thumb 等比缩放方法

	public function thumb($img,$width,$height,$prefix='th_'){
		$img=$this->path.$img;

		if(!file_exists($img)){
			exit('楼下有美女,提示你图片路径不存在');
		}
		
		$info=self::getImageInfo($img);
		
		$newSize=self::getNewSize($width,$height,$info);
		
		$res=self::openImg($img,$info['type']);

		$newRes=self::kidOfImage($res,$newSize,$info);

		if($name=self::saveImg($newRes,$this->path,$prefix,$info)){
			return $name;
		}

		
	}

	static private function saveImg($res,$path,$prefix,$binfo){
		$name=$path.$prefix.$binfo['name'];

		switch($binfo['type']){
			case 'image/jpeg':
			case 'image/jpg':
			case 'image/pjpeg':
				imagejpeg($res,$name);
				break;
			case 'image/png':
			case 'image/x-png':
				imagepng($res,$name);
				break;
			case 'image/gif':
				imagegif($res,$name);
				break;
			case 'image/wbmp':
				imagewbmp($res,$name);
				break;
			default:
				echo '不支持这个图片类型解析';
				break;

		}
		return $name;

	}

	static private function mergeImg($b,$l,$linfo,$p,$tmd){
		imagecopymerge($b,$l,$p['x'],$p['y'],0,0,$linfo['width'],$linfo['height'],$tmd);

		return $b;
	}

	static private function openImg($path,$type){
		switch($type){
			case 'image/jpeg':
			case 'image/jpg':
			case 'image/pjpeg':
				$res=imagecreatefromjpeg($path);
				break;

			case 'image/gif':
				$res=imagecreatefromgif($path);
				break;
			case 'image/wbmp':
				$res=imagecreatefromwbmp($path);
				break;
			case 'image/png':
			case 'image/x-png':
				$res=imagecreatefrompng($path);
				break;
			default:
				echo '图片类型不支持';
				exit;
		}
		return $res;

	}

	static private function getPosition($b,$l,$pos){
		switch($pos){

			case 1:
				$x=0;
				$y=0;
				break;
			case 2:
				$x=floor(($b['width']-$l['width'])/2);
				$y=0;
				break;
			case 3:
				$x=$b['width']-$l['width'];
				$y=0;
				break;
			case 4:
				$x=0;
				$y=ceil(($b['height']-$l['height'])/2);
				break;
			case 5:
				$x=floor(($b['width']-$l['width'])/2);
				$y=ceil(($b['height']-$l['height'])/2);
				break;
			case 6:
				$x=$b['width']-$l['width'];
				$y=ceil(($b['height']-$l['height'])/2);
				break;
			case 7:
				$x=0;
				$y=$b['height']-$l['height'];
				break;
			case 8:
				$x=floor(($b['width']-$l['width'])/2);
				$y=$b['height']-$l['height'];
				break;
			case 9:
				$x=$b['width']-$l['width'];
				$y=$b['height']-$l['height'];
				break;
			default:
				$x=mt_rand(0,$b['width']-$l['width']);
				$y=mt_rand(0,$b['height']-$l['height']);
				break;
		}

		return array('x'=>$x,'y'=>$y);
	}

	static private function checkSize($b,$l){
		if($b['width']<$l['width']||$b['height']<$l['height']){

			return false;
		}else{

			return true;
		}
	}
	
	static private function getImageInfo($path){
		$info=getimagesize($path);
		
		$data['width']=$info[0];
		$data['height']=$info[1];
		$data['type']=$info['mime'];
		$data['name']=basename($path);
		return $data;

	}

	static private function kidOfImage($srcImg,$size, $imgInfo){
			$newImg = imagecreatetruecolor($size["width"], $size["height"]);		
			$otsc = imagecolortransparent($srcImg);
			if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {
		  		 $transparentcolor = imagecolorsforindex( $srcImg, $otsc );
		 		 	 $newtransparentcolor = imagecolorallocate(
			   		 $newImg,
			  		 $transparentcolor['red'],
			   	         $transparentcolor['green'],
			   		 $transparentcolor['blue']
		  		 );

		  		 imagefill( $newImg, 0, 0, $newtransparentcolor );
		  		 imagecolortransparent( $newImg, $newtransparentcolor );
			}

		
			imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
			imagedestroy($srcImg);
			return $newImg;
		}

	static private function getNewSize($width, $height,$imgInfo){	
			$size["width"]=$imgInfo["width"];          //将原图片的宽度给数组中的$size["width"]
			$size["height"]=$imgInfo["height"];        //将原图片的高度给数组中的$size["height"]
			
			if($width < $imgInfo["width"]){
				$size["width"]=$width;             //缩放的宽度如果比原图小才重新设置宽度
			}

			if($width < $imgInfo["height"]){
				$size["height"]=$height;            //缩放的高度如果比原图小才重新设置高度
			}

			if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
				$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
			}else{
				$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
			}

			return $size;
	}		
	

}
?>
 

你可能感兴趣的:(PHP)