????????我們對圖片的處理主要是添加水印和等比縮放,在PHP中,封裝一個類來實現兩個功能。
源代碼如下:
<?php/***圖片處理*/
class?Image
{//路徑private?$path?=?'./upload/';//隨機文件名private?$isRandName;//初始化成員方法public?function?__construct($path?=?null?,?$r?=?true){if?(!is_null($path))?{$this->path?=?rtrim($path,'/').'/';}$this->isRandName?=?$r;}//water水印的方法//源(圖片?$dst)??目標(水印?$src)??位置(9宮格)?前綴($prefix)?透明度($tmd?)public?function?water($dst,$src,$pos?=?9,$prefix?=?'wa_',?$tmd?=?100){//判斷文件路徑是否存在$src?=?$this->path?.?$src;if?(!file_exists($dst)?||?!file_exists($src))?{exit('圖片或者水印不存在');}//獲取圖像(圖片和水印)的相關信息$dstInfo?=?self::getImageInfo($dst);$srcInfo?=?self::getImageInfo($src);//var_dump($dstInfo);//判斷寬高是否超過了目標圖片的寬高if?(!$this->_checkSize($dstInfo,$srcInfo))?{exit('水印圖片的寬、高不合法');}//擺放位置??1?2?3?4?5?6?7?8?9?九宮格(3行3列)$postion?=?self::getPostion($dstInfo,$srcInfo,$pos);//打開圖片$dstRes?=?self::openImage($dst,$dstInfo);$srcRes?=?self::openImage($src,$srcInfo);//將兩個圖片合并在一起??通過兩張圖片信息將圖片合并在一起??需要自定義一個方法$newRes?=?$this->_mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd);//判斷是否允許隨機命名【保存之前】if?($this->isRandName)?{//路徑?前綴?產生id?.??后綴//uniqid()?獲取一個帶前綴、基于當前時間微秒數的唯一ID$path?=?$this->path.$prefix?.?uniqid().?'.'?.$dstInfo['subfix'];}?else?{//路徑?前綴?文件原名$path?=?$this->path.$prefix?.?$dstInfo['basename'];}//保存圖片self::saveImage($newRes,$path,$dstInfo);//銷毀資源p_w_picpathdestroy($dstRes);p_w_picpathdestroy($srcRes);//返回路徑}//等比縮放//源圖片?寬?高?前綴public?function?thump($dst,$width,$height,$prefix?=?'thump_'){//判斷文件是否存在if?(!file_exists($dst))?{exit('文件路徑不存在');}//獲取圖像的信息??沒有信息就退出$info?=?self::getImageInfo($dst);//得到一個新的尺寸$newSize?=?self::getNewSize($width,$height,$info);//打開資源$res?=?self::openImage($dst,$info);//等比縮放這個資源??處理gif背景變黑的問題$newRes?=?self::kidOfImage($res,$newSize,$info);//保存$path?=?$this->path.$prefix.$info['basename'];self::saveImage($newRes,$path,$info);//銷毀資源p_w_picpathdestroy($newRes);//返回路徑return?$path;}//等比縮放處理private?static?function?kidOfImage($srcImg,?$size,?$imgInfo){$newImg?=?p_w_picpathcreatetruecolor($size["width"],?$size["height"]);$otsc?=?p_w_picpathcolortransparent($srcImg);if?(?$otsc?>=?0?&&?$otsc?<?p_w_picpathcolorstotal($srcImg))?{$transparentcolor?=?p_w_picpathcolorsforindex(?$srcImg,?$otsc?);$newtransparentcolor?=?p_w_picpathcolorallocate($newImg,$transparentcolor['red'],$transparentcolor['green'],$transparentcolor['blue']);p_w_picpathfill(?$newImg,?0,?0,?$newtransparentcolor?);p_w_picpathcolortransparent(?$newImg,?$newtransparentcolor?);}p_w_picpathcopyresized(?$newImg,?$srcImg,?0,?0,?0,?0,?$size["width"],?$size["height"],?$imgInfo["width"],?$imgInfo["height"]?);p_w_picpathdestroy($srcImg);return?$newImg;}//得到一個新的尺寸private?static?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;}//獲取圖片的相關信息public?static?function?getImageInfo($path){$data?=?[];//獲取圖片大小$info?=?getp_w_picpathsize($path);//var_dump($info);//根據打印出來的信息?將鍵所對應的值(文件的大小)賦值給data的數組中$data['width']?=?$info[0];$data['height']?=?$info[1];$data['mime']?=?$info['mime'];//獲取路徑??后綴?文件名信息$path?=?pathinfo($path);//var_dump($path);die;//根據打印出來的信息?將將鍵所對應的值(路徑和文件名)賦值給data的數組中$data['basename']?=?$path['basename'];$data['subfix']?=?$path['extension'];return?$data;}//檢查圖片和水印的寬高//將圖片的寬高和水印的寬高進行比較private?function?_checkSize($dstInfo,$srcInfo){//水印的寬應該小于圖片的寬度或者水印的高度應該小于圖片的高度?,只要其中一個不滿足就不能繼續if?($dstInfo['width']?<?$srcInfo['width']?||?$dstInfo['height']?<?$srcInfo['height'])?{return?false;}return?true;}//位置處理public?static?function?getPostion($dstInfo,$srcInfo,$pos){switch?($pos)?{case?1:$x?=?0;$y?=?0;break;case?2:$x?=?ceil(($dstInfo['width']?-?$srcInfo['width'])?/?2?);$y?=?0;break;case?3:$x?=?$dstInfo['width']?-?$srcInfo['width'];$y?=?0;break;case?4:$x?=?0;$y?=?ceil(($dstInfo['height']?-?$srcInfo['height'])?/?2?);break;case?5:$x?=?ceil(($dstInfo['width']?-?$srcInfo['width'])?/?2?);$y?=?ceil(($dstInfo['height']?-?$srcInfo['height'])?/?2?);break;case?6:$x?=?$dstInfo['width']?-?$srcInfo['width'];$y?=?ceil(($dstInfo['height']?-?$srcInfo['height'])?/?2?);break;case?7:$x?=?0;$y?=?$dstInfo['height']?-?$srcInfo['height'];break;case?8:$x?=?ceil(($dstInfo['width']?-?$srcInfo['width'])?/?2?);$y?=?$dstInfo['height']?-?$srcInfo['height'];break;case?9:$x?=?$dstInfo['width']?-?$srcInfo['width'];$y?=?$dstInfo['height']?-?$srcInfo['height'];break;}return?['x'?=>?$x?,'y'?=>$y];}//打開圖片//根據圖片的類型打開相應的圖片資源private?function?openImage($path,$info){switch?($info['mime'])?{case?'p_w_picpath/png':case?'p_w_picpath/x-png':$res?=?p_w_picpathcreatefrompng($path);break;case?'p_w_picpath/jpeg':case?'p_w_picpath/jpg':case?'p_w_picpath/pjpeg':$res?=?p_w_picpathcreatefromjpeg($path);break;case?'p_w_picpath/gif':$res?=?p_w_picpathcreatefromgif($path);break;case?'p_w_picpath/wbmp':case?'p_w_picpath/bmp':$res?=?p_w_picpathcreatefromwbmp($path);break;}//var_dump($res);die;return?$res;}//合并圖片?p_w_picpathcopymerge(圖片,水印,圖片坐標x,圖片坐標y,水印坐標x,水印坐標y,透明度)private?function?_mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd){p_w_picpathcopymerge($dstRes,$srcRes,$postion['x'],$postion['y'],0,0,$srcInfo['width'],$srcInfo['height'],$tmd);return?$dstRes;}//保存圖片處理方法//參數:需要保存的圖片資源,保存的路徑,保存的信息public?static?function?saveImage($res,$path,$info){//根據不同的圖片類型選擇不同的函數進行保存switch?($info['mime'])?{case?'p_w_picpath/png':case?'p_w_picpath/x-png':p_w_picpathpng($res,$path);break;case?'p_w_picpath/jpeg':case?'p_w_picpath/jpg':case?'p_w_picpath/pjpeg':p_w_picpathjpeg($res,$path);break;case?'p_w_picpath/gif':p_w_picpathgif($res,$path);break;case?'p_w_picpath/wbmp':case?'p_w_picpath/bmp':p_w_picpathwbmp($res,$path);break;}}
}
測試代碼:
$img?=?new?Image();
/*
$img->water('ly.png','logo.gif',3);
$img->water('ly.png','logo.gif',4);*/$img->thump('ly.png',100,100,'l1_');
轉載于:https://blog.51cto.com/chensenlin/1855589