php 图片处理类

来源:互联网 发布:办公软件套装 编辑:程序博客网 时间:2024/04/28 02:10
<?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();}}?>

原创粉丝点击