php 图片处理类

<?php
/**
 * 图片处理类
 * 2011/9/22
 * kcj
 * */
class UpPic{
	private $picPath;                   //图片路径信息
	private $picName;                   //图片的名称
	private $newName;                   //新图片的名称
	private $imageInfo;                 //图片信息
	private $img;                       //图片资源类型
	private $newImg;                    //新图片资源
	private $width;                     //新图片的宽度
	private $height;                    //新图片的高度
	
	function __construct($picPaht,$picName){
		$this->picName=$picName;
		$this->picPath=$picPaht;
		list($fileName,$extension)=explode(".",$picName);
		$this->newName=$fileName."_new".$extension;
		$this->imageInfo=$this->getInfo();
		$this->img=$this->getImg($picPaht.$picName);
	}
	private  function getInfo(){
		$data=getimagesize($this->picPath.$this->picName);
		$imageInfo['width']=$data[0];
		$imageInfo['height']=$data[1];
		$imageInfo['type']=$data[2];
		$imageInfo['name']=basename($file);
		$imageInfo['size']=filesize($file);
		return $imageInfo;
	}
	private function  getImg($sourFile){
		switch ($this->imageInfo['type']){
			case 1: //gif
			     $img=imagecreatefromgif($sourFile);
			     break;
			case 2: //jpg
			     $img=imagecreatefromjpeg($sourFile);
			     break;
			case 3://png
			     $img=imagecreatefrompng($sourFile);
			     break;
			 default:
			 	return false;
			 	break;
		}
		if($img){
			return $img;
		}else {
			return false;
		}
	}
	function makeThumb($maxWidth,$maxHeight,$new=true){    //缩图
		$isThumb=false;
		if($maxWidth<$this->imageInfo['width']){
			$width=$maxWidth;
			$isThumb=true;
		}else {
			$width=$this->imageInfo['width'];
		}
		if($maxHeight<$this->imageInfo['height']){
			$maxHeight=$this->imageInfo['height'];
			$isThumb=true;
		}else {
			$maxHeight=$this->imageInfo['height'];
		}
		if($isThumb){
			$scrW=$this->imageInfo['width'];
			$scrH=$this->imageInfo['height'];
			if($scrW*$width>$scrH * $height){
				$height=round($scrH*$width/$scrW);
			}else {
				$width=round($scrW*$height/$scrH);
			}
			$this->height=$height;
			$this->width=$width;
			$this->newImg=$this->kidOfImage($this->img,$scrH,$scrW);
			
			if($new){
				return $this->creatNewImage($this->picPath.$this->newName);
			}else {
				return $this->creatNewImage($this->picPath.$this->picName);
			}
		}else {
			if($new){
				copy($this->picPath.$this->picName,$this->picPath.$this->newName);
			}
			$this->newImg=$this->getImg($this->picPath.$this->picName);
			$this->width=$width;
			$this->height=$height;
		}
		return true;
	}
	private function kidOfImage($toImg,$ow,$oh){
		$imtn=imagecreatetruecolor($this->width,$this->height);
		$originaltransparentcolor=imagecolortransparent($toImg);  //将某个颜色定义为透明
		if($originaltransparentcolor>=0&&$originaltransparentcolor<imagecolorstotal($toImg)){      //取得一幅图像的调色板中颜色的数目
                $transparentcolor=imagecolorsforindex($toImg,$originaltransparentcolor);           //取得某索引的颜色
                $newtransparentcolor=imagecolorallocate($imtn,$transparentcolor['red'],$transparentcolor['green'],$transparentcolor['blue']);
                imagefill($imtn,0,0,$newtransparentcolor);
                imagecolortransparent($imtn,$newtransparentcolor);
		}
		imagecopyresized($imtn,$toImg,0,0,0,0,$this->width,$this->height,$ow,$oh);  //拷贝部分图像并调整大小
		return $imtn;
	}
	function waterMark($text){                          //水印
		$white=imagecolorallocate($this->newImg,255,255,255);
		$black=imagecolorallocate($this->newImg,0,0,0);
		$alpha=imagecolorallocatealpha($this->newImg,230,230,230,40);  //为一幅图像分配颜色 + alpha
		$fontName=$this->picPath."simsun.ttc";
		imagettftext($this->newImg,6.0,0,20,$this->height-16,$black,$fontName,$this->toCode($text[0])); //用 TrueType 字体向图像写入文本
		imagettftext($this->newImg,6.0,0,20,$this->height-6,$black,$fontName,$this->toCode($text[1])); //用 TrueType 字体向图像写入文本
         $this->toCode($text[1]);
         return $this->createNewImage($this->picPath.$this->newName);
	}
	private  function toCode($text){
		return iconv("GB2312","UTF-8",$text);
	}
	private function createNewImage($newName){
		$result=false;
		switch ($this->imageInfo['type']){
			case 1: 
			  $result=imagegif($this->newName,$newName);
			  break;
		    case 2:
			  $result=imagejpeg($this->newName,$newName);
			  break;
			case 3:
			  $result=imagepng($this->newImg,$newName);
			  break;
			default:
				return false;
				break;
		}
		if($result){
			return true;
		}else {
			return false;
		}
	}
	function closeImg(){
		imagedestroy($this->img);
		imagedestroy($this->newImg);
		$this->img=false;
	}
	function __destruct(){
		$this->closeImg();
	}
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chaojie2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值