本文實例為大家分享了多功能php圖片處理類,供大家參考,具體內容如下
<?php /** * Image.class.php 圖像處理類 * @author Administrator * */ class Image{ private $path=''; /** * 構造方法 * @param unknown $path */ function __construct($path=''){ if (!empty($path)) { $this->path=$path; } } /** +----------------------------------------------- * 等比縮放函數 +----------------------------------------------- * @param unknown $name 需處理圖片的名稱 * @param unknown $width 縮放後的寬度 * @param unknown $height 縮放後的高度 * @param string $thumb_prixs 縮放後的前綴名 * @return mixed $newname 返回的縮放後的文件名 */ function thumb($name,$width,$height,$thumb_prixs='th_'){ //獲取圖片信息 $Info=$this->ImageInfo($name); //圖片的寬度,高度,類型 //獲取圖片資源,各種類型的圖片都可以創建資源,jpg,gif,png $imagres=$this->Img_resouce($name, $Info); //獲取計算圖片等比例之後的大小, $size=$this->getNewSize($name,$width,$height,$Info); //獲取新的圖片資源,處理透明背景 $newimg=$this->getImage($imagres,$size,$Info); //另存為一個新的圖片,返回新的縮放後的圖片名稱 $newname=$this->SaveNewImage($newimg,$thumb_prixs.$name,$Info); return $newname; } /** +----------------------------------------------------------------------- * 水印標記函數 +----------------------------------------------------------------------- * @param unknown $backname 背景文件名 * @param unknown $watername 水印文件名 * @param number $waterpos 水印位置 * @param string $wa_prixs 水印前綴名 * @return boolean */ function waterMark($backname,$watername,$waterpos=0,$wa_prixs='wa_') { if (file_exists($this->path.$backname) && file_exists($this->path.$watername)) { $backinfo=$this->ImageInfo($backname); $waterinfo=$this->ImageInfo($watername); if(!$pos=$this->getPos($backinfo, $waterinfo, $waterpos)){ echo "水印圖片不應該比背景圖片小"; return false; } $backimg=$this->Img_resouce($backname, $backinfo); $waterimg=$this->Img_resouce($watername, $waterinfo); var_dump($backimg);exit(); $backimg=$this->CopyImage($backimg, $waterimg, $pos, $waterinfo); $this->SaveNewImage($backimg, $wa_prixs.$backname, $backinfo); }else{ echo "圖片或水印不存在"; return false; } } /** +----------------------------------------------------------------------- * 獲取圖片信息函數 +----------------------------------------------------------------------- * @param unknown $name * @return unknown */ private function ImageInfo($img) { $imageInfo=getimagesize($img); if ($imageInfo!==false) { $imageType=strtolower(substr(image_type_to_extension($imageInfo[2]),1)); $imageSize=filesize($img); $Info=array( "width" => $imageInfo[0], "height" => $imageInfo[1], "type" => $imageType, "size" => $imageSize, "mime" => $imageInfo['mime'] ); return $Info; }else{ return false; } } /** +-------------------------------------------------------- * 創建原圖像格式函數 +-------------------------------------------------------- * @param unknown $name * @param unknown $imaginfo * @return boolean|resource */ private function Img_resouce($name,$imageinfo){ $iamgeres=$this->path.$name; //var_dump($iamgeres);exit(); switch ($imageinfo['type']) { case 'gif': $img=imagecreatefromgif($name); break; case 'jpg': $img=imagecreatefromjpeg($name); break; case 'png': $img=imagecreatefrompng($name); break; } return $img; } /** +-------------------------------------------------- * 獲取等比縮放尺寸函數 +-------------------------------------------------- * @param unknown $name * @param unknown $width * @param unknown $height * @param unknown $imaginfo * @return Ambigous <unknown, number> */ private function getNewSize($name,$width,$height,$imaginfo){ $size['width']=$imaginfo['width']; $size['height']=$imaginfo['height']; if ($width<$imaginfo['width']) { $size['width']=$width; } if ($height<$imaginfo['height']) { $size['height']=$height; } //圖像等比例縮放算法 if ($imaginfo['width']*$size['width']>$imaginfo['height']*$size['height']) { $size['height']=round($imaginfo['height']*$size['width']/$imaginfo['width']); }else{ $size['width']=round($imaginfo['width']*$size['height']/$imaginfo['height']); } return $size; } private function getImage($imageres,$size,$imageinfo){ //新建一個真彩色圖像 $newimg=imagecreatetruecolor($size['width'], $size['height']); //將某個顏色定義為透明色 $otsc=imagecolortransparent($imageres); //獲取圖像的調色板的顏色數目 if ($otsc>=0&&$otsc<=imagecolorstotal($imageres)) { //取得某索引的顏色 $stran=imagecolorsforindex($imageres, $otsc); //為圖像分配顏色 $newt=imagecolorallocate($imageres, $stran['red'], $stran['green'], $stran['blue']); //區域填充函數 imagefill($newimg, 0, 0, $newt); //為圖像定義透明色 imagecolortransparent($newimg,$newt); } imagecopyresized($newimg, $imageres, 0, 0, 0, 0, $size['width'], $size['height'], $imageinfo['width'], $imageinfo['height']); imagedestroy($imageres); return $newimg; } /** +---------------------------------------------- *保存圖像函數 +---------------------------------------------- * @param unknown $newimg * @param unknown $newname * @param unknown $imageinfo * @return unknown */ private function SaveNewImage($newimg,$newname,$imageinfo){ switch ($imageinfo['type']){ case 1://gif $result=imagegif($newimg,$this->path.$newname); break; case 2://jpg $result=imagejpeg($newimg,$this->path.$newname); break; case 3://png $result=imagepng($newimg,$this->path.$newname); break; } imagedestroy($newimg); return $newname; } /** +----------------------------------------------------------------- * 獲取水印位置函數 +----------------------------------------------------------------- * @param unknown $backinfo 背景信息 * @param unknown $waterinfo 水印信息 * @param unknown $waterpos 水印位置 * @return boolean|multitype:number 返回坐標數組 */ private function getPos($backinfo,$waterinfo,$waterpos) { if ($backinfo['width']<$waterinfo['width']||$backinfo['height']<$waterinfo['height']) { return false; } switch ($waterpos) { case 1://左上角 $posX=0; $posY=0; break; case 2://中上方 $posX=$backinfo['width']-$waterinfo['width']/2; $posY=0; break; case 3://右上角 $posX=$backinfo['width']-$waterinfo['width']; $posY=0; break; case 4://左中方 $posX=0; $posY=$backinfo['height']-$waterinfo['height']/2; break; case 5://正中間 $posX=$backinfo['width']-$waterinfo['width']/2; $posY=$backinfo['height']-$waterinfo['height']/2; break; case 6://右中方 $posX=$backinfo['width']-$waterinfo['width']; $posY=$backinfo['height']-$waterinfo['height']/2; break; case 7://底部靠左 $posX=0; $posY=$backinfo['height']-$waterinfo['height']; break; case 8://底部居中 $posX=$backinfo['width']-$waterinfo['width']/2; $posY=$backinfo['height']-$waterinfo['height']; break; case 9://底部靠右 $posX=$backinfo['width']-$waterinfo['width']; $posY=$backinfo['height']-$waterinfo['height']; break; case 0: default : $posX=rand(0,$backinfo['width']-$waterinfo['width']); $posY=rand(0,$backinfo['height']-$waterinfo['height']); break; } return array('posX'=>$posX,'posY'=>$posY); } /** +------------------------------------------------------------------- * 拷貝圖像 +------------------------------------------------------------------- * @param unknown $backimg 背景資源 * @param unknown $waterimg 水印資源 * @param unknown $pos 水印位置 * @param unknown $waterinfo 水印信息 * @return unknown */ private function CopyImage($backimg,$waterimg,$pos,$waterinfo) { imagecopy($backimg, $waterimg, $pos['posX'], $pos['posY'], 0, 0, $waterinfo['width'], $waterinfo['height']); imagedestroy($waterimg); return $backimg; } } ?>
以上就是本文的全部內容,希望對大家學習PHP程序設計有所幫助。