PHP添加PNG圖片背景透明水印操作類定義與用法示例
本文實(shí)例講述了PHP添加PNG圖片背景透明水印操作類定義與用法。分享給大家供大家參考,具體如下:
圖片相關(guān)操作類
class ImageTool { private $imagePath;//圖片路徑 private $outputDir;//輸出文件夾 public $memoryImg;//內(nèi)存圖像 public $path; public function __construct($imagePath, $outputDir = null) { $this->imagePath = $imagePath; $this->outputDir = $outputDir; $this->memoryImg = null; $this->path = null; } /** * 顯示內(nèi)存中的圖片 * @param $image */ public function showImage() { if ($this->memoryImg != null) { $info = getimagesize($this->imagePath); $type = image_type_to_extension($info[2], false); header('Content-type:' . $info['mime']); $funs = "image{$type}"; $funs($this->memoryImg); imagedestroy($this->memoryImg); $this->memoryImg = null; } } /** * 保存圖片 * @param $image 圖片路徑 * @return string */ private function saveImage($image) { $info = getimagesize($this->imagePath); $type = image_type_to_extension($info[2], false); $funs = "image{$type}"; if (empty($this->outputDir)) { $funs($image, md5($this->imagePath) . '.' . $type); return md5($this->imagePath) . '.' . $type; } else { $funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type); return $this->outputDir . md5($this->imagePath) . '.' . $type; } } /** * 壓縮圖片 * @param $width 壓縮后寬度 * @param $height 壓縮后高度 * @param bool $output 是否輸出文件 * @return resource */ public function compressImage($width, $height, $output = false) { $image = null; $info = getimagesize($this->imagePath); $type = image_type_to_extension($info[2], false); $fun = "imagecreatefrom{$type}"; $image = $fun($this->imagePath); imagesavealpha($image,true);// $thumbnail = imagecreatetruecolor($width, $height); imagealphablending($thumbnail,false);//這里很重要,意思是不合并顏色,直接用$img圖像顏色替換,包括透明色; imagesavealpha($thumbnail,true);// imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image); if ($output) { $path = $this->saveImage($thumbnail); $this->path = $path; } $this->memoryImg = $thumbnail; return $this; } /** * 為圖像添加文字標(biāo)記 * * @param $content 文本內(nèi)容 * @param $size 字體大小 * @param $font 字體樣式 * @param bool $output 是否輸出文件 * @return $this */ public function addTextmark($content, $size, $font, $output = false) { $info = getimagesize($this->imagePath); $type = image_type_to_extension($info[2], false); $fun = "imagecreatefrom{$type}"; $image = $fun($this->imagePath); $color = imagecolorallocatealpha($image, 0, 0, 0, 80); $posX = imagesx($image) - strlen($content) * $size / 2; $posY = imagesy($image) - $size / 1.5; imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content); if ($output) { $this->saveImage($image); } $this->memoryImg = $image; return $this; } /** * 為圖片添加水印 * * @param $watermark 水印圖片路徑 * @param $alpha 水印透明度(0-100) * @param bool $output 是否輸出文件 * @return $this */ public function addWatermark($watermark, $alpha, $output = false) { $image_info = getimagesize($this->imagePath); $image_type = image_type_to_extension($image_info[2], false); $image_fun = "imagecreatefrom{$image_type}"; $image = $image_fun($this->imagePath); $mark_info = getimagesize($watermark); $mark_type = image_type_to_extension($mark_info[2], false); $mark_fun = "imagecreatefrom{$mark_type}"; $mark = $mark_fun($watermark); $posX = imagesx($image) - imagesx($mark); $posY = imagesy($image) - imagesy($mark); imagesavealpha($mark, true); imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha); imagesavealpha($mark, true); if ($output) { $path = $this->saveImage($image); $this->path = $path; } $this->memoryImg = $image; return $this; } //用給定角度旋轉(zhuǎn)圖像,以jpeg圖像格式為例 /** * 水印圖片旋轉(zhuǎn) * @param $degrees 旋轉(zhuǎn)角度 * @param bool $output 是否保存圖片 * @return $this */ function rotateImage($degrees, $output = false) { $info = getimagesize($this->imagePath); $type = image_type_to_extension($info[2], false); $fun = "imagecreatefrom{$type}"; $image = $fun($this->imagePath); $block = imagecreatetruecolor(170,170);//建立一個畫板 $bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取一個完全透明的顏色 $image = imagerotate($image, $degrees, $bg ,0); imagesavealpha($image, true); header("Content-type: image/{$type}"); //旋轉(zhuǎn)后的圖片保存 if ($output) { $path = $this->saveImage($image); $this->path = $path; } $this->memoryImg = $image; return $this; } /** * 添加PNG透明圖片 * $bigImgPath 目標(biāo)圖片路徑 * $smallImgPath 水印圖片路徑 * $width 相對于目標(biāo)圖的x軸放置位置 左上角為 0 * $height 相對于目標(biāo)圖的y軸放置位置 左上角為0 * $bigImgPaths 合成后的圖片路徑 若路徑名與第一張或第二張路徑相同 直接覆蓋原圖 */ public function mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths) { $image_kuang = imagecreatefromstring(file_get_contents($smallImgPath)); $image_photo = imagecreatefromstring(file_get_contents($bigImgPath)); //創(chuàng)建一個新的,和大圖一樣大的畫布 $image_3 = imageCreatetruecolor(imagesx($image_photo), imagesy($image_photo)); //為真彩色畫布創(chuàng)建白色背景,再設(shè)置為透明 $color = imagecolorallocate($image_3, 255, 255, 255); imagefill($image_3, 0, 0, $color); imageColorTransparent($image_3, $color); /** * 先copy圖片,再copy畫框,實(shí)現(xiàn)png的透明效果,將圖片嵌入到畫框里 * imagecopymerge與imagecopy的不同: * imagecopymerge 函數(shù)可以支持兩個圖像疊加時,設(shè)置疊加層的透明度。imagecopymerge比imagecopy多一個參數(shù),來設(shè)置透明度 * PHP內(nèi)部源碼里,imagecopymerge在透明度參數(shù)為100時,直接調(diào)用imagecopy函數(shù)。 * imagecopy 函數(shù)則不支持疊加透明,但拷貝時可以保留png圖像的原透明信息,而imagecopymerge卻不支持圖片的本身的透明拷貝 * 即:使用imagecopymerge函數(shù),可以實(shí)現(xiàn)打上透明度為30%的淡淡的水印圖標(biāo),但圖片本身的png就會變得像IE6不支持png透明那樣,背景不透明了。 * 如果使用imagecopy函數(shù),可以保留圖片本身的透明信息,但無法實(shí)現(xiàn)30%的淡淡水印疊加, */ imagecopyresampled($image_3,$image_photo,0,0,0,0,imagesx($image_photo),imagesy($image_photo),imagesx($image_photo),imagesy($image_photo)); imagecopy($image_3,$image_kuang, $width,$height,0,0,imagesx($image_kuang),imagesy($image_kuang)); //存儲圖片路徑 imagejpeg($image_3, $bigImgPaths); return $bigImgPaths; } }
控制器調(diào)用方法
public function test() { $bigImgPath = 'ren.jpg';//原圖路徑 $waterImgPath = 'tae.png';//水印圖路徑 $imageTool = new ImageTool($waterImgPath, 'tmp/');//圖片路徑、輸出文件夾 $smallImgPath = $imageTool->rotateImage(45, true)->path;//旋轉(zhuǎn) $width = 0;//水印所在X坐標(biāo) $height = 0;//水印所在Y坐標(biāo) $bigImgPaths = 'new.png';//生成原圖加水印新圖路徑 $path = $this->mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths); return view('image', compact('path')); }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)
- PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
- PHP使用imagick讀取PDF生成png縮略圖的兩種方法
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- php縮放gif和png圖透明背景變成黑色的解決方法
- PHP實(shí)現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
- PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
- php 處理png圖片白色背景色改為透明色的實(shí)例代碼
- PHP實(shí)現(xiàn)對png圖像進(jìn)行縮放的方法(支持透明背景)
- 支持png透明圖片的php生成縮略圖類分享
- php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析
相關(guān)文章
Windows平臺實(shí)現(xiàn)PHP連接SQL Server2008的方法
這篇文章主要介紹了Windows平臺實(shí)現(xiàn)PHP連接SQL Server2008的方法,結(jié)合實(shí)例形式分析了Windows平臺PHP連接SQL Server2008所需的相關(guān)dll動態(tài)鏈接庫文件及相應(yīng)的配置與使用方法,需要的朋友可以參考下2017-07-07php array_intersect()函數(shù)使用代碼
array_intersect() 返回一個數(shù)組,該數(shù)組包含了所有在 array1 中也同時出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意鍵名保留不變。2009-01-01php中將一段數(shù)據(jù)存到一個txt文件中并顯示其內(nèi)容
這篇文章主要介紹了php中將一段數(shù)據(jù)存到一個txt文件中,并獲取其內(nèi)容顯示的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08php的webservice的wsdl的XML無法顯示問題的解決方法
本篇文章是對php的webservice的wsdl的XML無法顯示問題的解決方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03PHP學(xué)習(xí)散記_編碼(json_encode 中文不顯示)
今天使用json_encode轉(zhuǎn)換數(shù)組的時候,出現(xiàn)了中文不顯示的情況。2011-11-11PHP實(shí)現(xiàn)簡單鑒權(quán)的示例代碼
這篇文章主要為大家詳細(xì)介紹了php如何通過在header增加key,sign,timestamp來實(shí)現(xiàn)鑒權(quán),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-12-12