一個(gè)經(jīng)典實(shí)用的PHP圖像處理類分享
更新時(shí)間:2014年11月18日 08:49:38 投稿:junjie
這篇文章主要介紹了一個(gè)經(jīng)典實(shí)用的PHP圖像處理類分享,本文提供的PHP圖像操作類可以滿足網(wǎng)站中的大部分功能需求,如圖片的縮放、加水印和裁剪等功能,需要的朋友可以參考下
本圖像處理類可以完成對(duì)圖片的縮放、加水印和裁剪的功能,支持多種圖片類型的處理,縮放時(shí)進(jìn)行優(yōu)化等。
<?php /** file: image.class.php 類名為Image 圖像處理類,可以完成對(duì)各種類型的圖像進(jìn)行縮放、加圖片水印和剪裁的操作。 */ class Image { /* 圖片保存的路徑 */ private $path; /** * 實(shí)例圖像對(duì)象時(shí)傳遞圖像的一個(gè)路徑,默認(rèn)值是當(dāng)前目錄 * @param string $path 可以指定處理圖片的路徑 */ function __construct($path="./"){ $this->path = rtrim($path,"/")."/"; } /** * 對(duì)指定的圖像進(jìn)行縮放 * @param string $name 是需要處理的圖片名稱 * @param int $width 縮放后的寬度 * @param int $height 縮放后的高度 * @param string $qz 是新圖片的前綴 * @return mixed 是縮放后的圖片名稱,失敗返回false; */ function thumb($name, $width, $height,$qz="th_"){ /* 獲取圖片寬度、高度、及類型信息 */ $imgInfo = $this->getInfo($name); /* 獲取背景圖片的資源 */ $srcImg = $this->getImg($name, $imgInfo); /* 獲取新圖片尺寸 */ $size = $this->getNewSize($name,$width, $height,$imgInfo); /* 獲取新的圖片資源 */ $newImg = $this->kidOfImage($srcImg, $size,$imgInfo); /* 通過本類的私有方法,保存縮略圖并返回新縮略圖的名稱,以"th_"為前綴 */ return $this->createNewImage($newImg, $qz.$name,$imgInfo); } /** * 為圖片添加水印 * @param string $groundName 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式 * @param string $waterName 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式 * @param int $waterPos 水印位置,有10種狀態(tài),0為隨機(jī)位置; * 1為頂端居左,2為頂端居中,3為頂端居右; * 4為中部居左,5為中部居中,6為中部居右; * 7為底端居左,8為底端居中,9為底端居右; * @param string $qz 加水印后的圖片的文件名在原文件名前面加上這個(gè)前綴 * @return mixed 是生成水印后的圖片名稱,失敗返回false */ function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){ /*獲取水印圖片是當(dāng)前路徑,還是指定了路徑*/ $curpath = rtrim($this->path,"/")."/"; $dir = dirname($waterName); if($dir == "."){ $wpath = $curpath; }else{ $wpath = $dir."/"; $waterName = basename($waterName); } /*水印圖片和背景圖片必須都要存在*/ if(file_exists($curpath.$groundName) && file_exists($wpath.$waterName)){ $groundInfo = $this->getInfo($groundName); //獲取背景信息 $waterInfo = $this->getInfo($waterName, $dir); //獲取水印圖片信息 /*如果背景比水印圖片還小,就會(huì)被水印全部蓋住*/ if(!$pos = $this->position($groundInfo, $waterInfo, $waterPos)){ echo '水印不應(yīng)該比背景圖片?。?; return false; } $groundImg = $this->getImg($groundName, $groundInfo); //獲取背景圖像資源 $waterImg = $this->getImg($waterName, $waterInfo, $dir); //獲取水印圖片資源 /* 調(diào)用私有方法將水印圖像按指定位置復(fù)制到背景圖片中 */ $groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo); /* 通過本類的私有方法,保存加水圖片并返回新圖片的名稱,默認(rèn)以"wa_"為前綴 */ return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); }else{ echo '圖片或水印圖片不存在!'; return false; } } /** * 在一個(gè)大的背景圖片中剪裁出指定區(qū)域的圖片 * @param string $name 需要剪切的背景圖片 * @param int $x 剪切圖片左邊開始的位置 * @param int $y 剪切圖片頂部開始的位置 * @param int $width 圖片剪裁的寬度 * @param int $height 圖片剪裁的高度 * @param string $qz 新圖片的名稱前綴 * @return mixed 裁剪后的圖片名稱,失敗返回false; */ function cut($name, $x, $y, $width, $height, $qz="cu_"){ $imgInfo=$this->getInfo($name); //獲取圖片信息 /* 裁剪的位置不能超出背景圖片范圍 */ if( (($x+$width) > $imgInfo['width']) || (($y+$height) > $imgInfo['height'])){ echo "裁剪的位置超出了背景圖片范圍!"; return false; } $back = $this->getImg($name, $imgInfo); //獲取圖片資源 /* 創(chuàng)建一個(gè)可以保存裁剪后圖片的資源 */ $cutimg = imagecreatetruecolor($width, $height); /* 使用imagecopyresampled()函數(shù)對(duì)圖片進(jìn)行裁剪 */ imagecopyresampled($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height); imagedestroy($back); /* 通過本類的私有方法,保存剪切圖并返回新圖片的名稱,默認(rèn)以"cu_"為前綴 */ return $this->createNewImage($cutimg, $qz.$name,$imgInfo); } /* 內(nèi)部使用的私有方法,用來確定水印圖片的位置 */ private function position($groundInfo, $waterInfo, $waterPos){ /* 需要加水印的圖片的長度或?qū)挾缺人∵€小,無法生成水印 */ if( ($groundInfo["width"]<$waterInfo["width"]) || ($groundInfo["height"]<$waterInfo["height"]) ) { return false; } switch($waterPos) { case 1: //1為頂端居左 $posX = 0; $posY = 0; break; case 2: //2為頂端居中 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2; $posY = 0; break; case 3: //3為頂端居右 $posX = $groundInfo["width"] - $waterInfo["width"]; $posY = 0; break; case 4: //4為中部居左 $posX = 0; $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2; break; case 5: //5為中部居中 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2; $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2; break; case 6: //6為中部居右 $posX = $groundInfo["width"] - $waterInfo["width"]; $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2; break; case 7: //7為底端居左 $posX = 0; $posY = $groundInfo["height"] - $waterInfo["height"]; break; case 8: //8為底端居中 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2; $posY = $groundInfo["height"] - $waterInfo["height"]; break; case 9: //9為底端居右 $posX = $groundInfo["width"] - $waterInfo["width"]; $posY = $groundInfo["height"] - $waterInfo["height"]; break; case 0: default: //隨機(jī) $posX = rand(0,($groundInfo["width"] - $waterInfo["width"])); $posY = rand(0,($groundInfo["height"] - $waterInfo["height"])); break; } return array("posX"=>$posX, "posY"=>$posY); } /* 內(nèi)部使用的私有方法,用于獲取圖片的屬性信息(寬度、高度和類型) */ private function getInfo($name, $path=".") { $spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/'; $data = getimagesize($spath.$name); $imgInfo["width"] = $data[0]; $imgInfo["height"] = $data[1]; $imgInfo["type"] = $data[2]; return $imgInfo; } /*內(nèi)部使用的私有方法, 用于創(chuàng)建支持各種圖片格式(jpg,gif,png三種)資源 */ private function getImg($name, $imgInfo, $path='.'){ $spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/'; $srcPic = $spath.$name; switch ($imgInfo["type"]) { case 1: //gif $img = imagecreatefromgif($srcPic); break; case 2: //jpg $img = imagecreatefromjpeg($srcPic); break; case 3: //png $img = imagecreatefrompng($srcPic); break; default: return false; break; } return $img; } /* 內(nèi)部使用的私有方法,返回等比例縮放的圖片寬度和高度,如果原圖比縮放后的還小保持不變 */ private function getNewSize($name, $width, $height, $imgInfo){ $size["width"] = $imgInfo["width"]; //原圖片的寬度 $size["height"] = $imgInfo["height"]; //原圖片的高度 if($width < $imgInfo["width"]){ $size["width"]=$width; //縮放的寬度如果比原圖小才重新設(shè)置寬度 } if($height < $imgInfo["height"]){ $size["height"] = $height; //縮放的高度如果比原圖小才重新設(shè)置高度 } /* 等比例縮放的算法 */ 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; } /* 內(nèi)部使用的私有方法,用于保存圖像,并保留原有圖片格式 */ private function createNewImage($newImg, $newName, $imgInfo){ $this->path = rtrim($this->path,"/")."/"; switch ($imgInfo["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; } /* 內(nèi)部使用的私有方法,用于加水印時(shí)復(fù)制圖像 */ private function copyImage($groundImg, $waterImg, $pos, $waterInfo){ imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"],$waterInfo["height"]); imagedestroy($waterImg); return $groundImg; } /* 內(nèi)部使用的私有方法,處理帶有透明度的圖片保持原樣 */ 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; } }
您可能感興趣的文章:
- 9段PHP實(shí)用功能的代碼推薦
- 幾個(gè)實(shí)用的PHP內(nèi)置函數(shù)使用指南
- 簡單實(shí)用的PHP防注入類實(shí)例
- 一款簡單實(shí)用的php操作mysql數(shù)據(jù)庫類
- 非常實(shí)用的PHP常用函數(shù)匯總
- 制作安全性高的PHP網(wǎng)站的幾個(gè)實(shí)用要點(diǎn)
- Thinkphp中的curd應(yīng)用實(shí)用要點(diǎn)
- 9個(gè)實(shí)用的PHP代碼片段分享
- PHP實(shí)用函數(shù)分享之去除多余的0
- 7個(gè)鮮為人知卻非常實(shí)用的PHP函數(shù)
- PHP實(shí)現(xiàn)簡單實(shí)用的驗(yàn)證碼類
- 6個(gè)超實(shí)用的PHP代碼片段
- PHP判斷字符串長度的兩種方法很實(shí)用
- PHPStrom中實(shí)用的功能和快捷鍵大全
- 四個(gè)PHP非常實(shí)用的功能
相關(guān)文章
php下關(guān)于中英數(shù)字混排的字符串分割問題
要用到短信平臺(tái)的緣故,短信每條又有字?jǐn)?shù)的限制,而短信平臺(tái)的服務(wù)端又不會(huì)自己分析。短信平臺(tái)的后臺(tái)會(huì)自己分割,但api卻有沒這個(gè)功能。2010-04-04PHP實(shí)現(xiàn)通用alert函數(shù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)通用alert函數(shù)的方法,實(shí)例分析了php自定義alert函數(shù)實(shí)現(xiàn)提示信息的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03PHP中Header使用的HTTP協(xié)議及常用方法小結(jié)
這篇文章主要介紹了PHP中Header使用的HTTP協(xié)議及常用方法,包含了各種錯(cuò)誤編碼類型及其含義,需要的朋友可以參考下2014-11-11有關(guān)PHP中MVC的開發(fā)經(jīng)驗(yàn)分享
經(jīng)過近一個(gè)月對(duì)MVC的研究,自己也通過網(wǎng)上有朋友的指導(dǎo),有了一套自己的MVC流程及框架,但是感覺缺限還是很多,靈活性方面還是欠缺,但又不知道怎么樣的具體改進(jìn),今天我就把我的流程及思想發(fā)布,希望有高人能夠指點(diǎn)指點(diǎn)2012-05-05PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字?jǐn)?shù)
PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字?jǐn)?shù),需要的朋友可以參考下。2011-11-11PHP實(shí)現(xiàn)二維數(shù)組根據(jù)key進(jìn)行排序的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)二維數(shù)組根據(jù)key進(jìn)行排序的方法,涉及php數(shù)組的遍歷與排序相關(guān)操作技巧,需要的朋友可以參考下2016-12-12