您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在PHP中添加PNG圖片背景透明水印,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
圖片相關(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);//建立一個(gè)畫板 $bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取一個(gè)完全透明的顏色 $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 相對(duì)于目標(biāo)圖的x軸放置位置 左上角為 0 * $height 相對(duì)于目標(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)建一個(gè)新的,和大圖一樣大的畫布 $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ù)可以支持兩個(gè)圖像疊加時(shí),設(shè)置疊加層的透明度。imagecopymerge比imagecopy多一個(gè)參數(shù),來(lái)設(shè)置透明度 * PHP內(nèi)部源碼里,imagecopymerge在透明度參數(shù)為100時(shí),直接調(diào)用imagecopy函數(shù)。 * imagecopy 函數(shù)則不支持疊加透明,但拷貝時(shí)可以保留png圖像的原透明信息,而imagecopymerge卻不支持圖片的本身的透明拷貝 * 即:使用imagecopymerge函數(shù),可以實(shí)現(xiàn)打上透明度為30%的淡淡的水印圖標(biāo),但圖片本身的png就會(huì)變得像IE6不支持png透明那樣,背景不透明了。 * 如果使用imagecopy函數(shù),可以保留圖片本身的透明信息,但無(wú)法實(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)); //存儲(chǔ)圖片路徑 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')); }
以上就是怎么在PHP中添加PNG圖片背景透明水印,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。