您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“PHP水印類支持添加圖片、文字、填充顏色區(qū)域的代碼分享”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
*自己整理的一個(gè)水印類*
支持添加圖片、文字、填充顏色區(qū)域
<?php /** * 圖片加水印類,支持文字水印、透明度設(shè)置、自定義水印位置等。 * 使用示例: * $obj = new WaterMask($imgFileName); //實(shí)例化對(duì)象 * $obj->$waterType = 1; //類型:0為文字水印、1為圖片水印 * $obj->$transparent = 45; //水印透明度 * $obj->$waterStr = 'icp.niufee.com'; //水印文字 * $obj->$fontSize = 18; //文字字體大小 * $obj->$fontColor = array(255,255,255); //水印文字顏色(RGB) * $obj->$fontFile = 'AHGBold.ttf'; //字體文件 * …… * $obj->output(); //輸出水印圖片文件覆蓋到輸入的圖片文件 * @modify liuzp111 */ class WaterMask{ public $waterTypeImage = false; //水印類型:?jiǎn)⒂脠D片水印 public $waterTypeStr = false; //水印類型:?jiǎn)⒂梦淖炙? public $pos = 0; //水印位置 public $transparent = 45; //水印透明度(0---100)數(shù)值越大越不透明 public $waterStr = 'icp.niufee.com'; //水印文字 public $fontSize = 14; //文字字體大小 public $fontColor = array(0,0,0); //水印文字顏色(RGB) 默認(rèn)黑色 public $fontFile = './font/simfang.ttf'; //字體文件 public $waterImg = 'logo.png'; //水印圖片 private $srcImg = ''; //需要添加水印的圖片 private $im = ''; //圖片句柄 private $water_im = ''; //水印圖片句柄 private $srcImg_info = ''; //圖片信息 private $waterImg_info = ''; //水印圖片信息 private $str_w = ''; //水印文字寬度 private $str_h = ''; //水印文字高度 private $x = ''; //水印X坐標(biāo) private $y = ''; //水印y坐標(biāo) public $output_img = ''; //存儲(chǔ)輸出圖片到哪里 public $is_draw_rectangle = false; //是否繪制矩形區(qū)域 (暫不支持自定義位置) //public $rectange_color = ''; //繪制矩形區(qū)域的顏色 private $result_array = array(); //結(jié)果數(shù)組 public function __construct($img) { //析構(gòu)函數(shù) //$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!'); if(file_exists($img)){ $this->srcImg = $img; }else{ return array('data'=>'','info'=>'源文件不存在!','status'=>0); } } private function imginfo() { //獲取需要添加水印的圖片的信息,并載入圖片。 $this->srcImg_info = getimagesize($this->srcImg); switch ($this->srcImg_info[2]) { case 3: $this->im = imagecreatefrompng($this->srcImg); break 1; case 2: $this->im = imagecreatefromjpeg($this->srcImg); break 1; case 1: $this->im = imagecreatefromgif($this->srcImg); break 1; default: //die('原圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。'); return array('data'=>'','info'=>'原圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。','status'=>0); } } private function waterimginfo() { //獲取水印圖片的信息,并載入圖片。 $this->waterImg_info = getimagesize($this->waterImg); switch ($this->waterImg_info[2]) { case 3: $this->water_im = imagecreatefrompng($this->waterImg); break 1; case 2: $this->water_im = imagecreatefromjpeg($this->waterImg); break 1; case 1: $this->water_im = imagecreatefromgif($this->waterImg); break 1; default: //die('水印圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。'); return array('data'=>'','info'=>'水印圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。','status'=>0); } } private function waterpos() { //水印位置算法 switch ($this->pos) { case 0: //隨機(jī)位置 $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]); $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]); break 1; case 1: //上左 $this->x = 0; $this->y = 0; break 1; case 2: //上中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = 0; break 1; case 3: //上右 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; $this->y = 0; break 1; case 4: //中左 $this->x = 0; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break 1; case 5: //中中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break 1; case 6: //中右 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break 1; case 7: //下左 $this->x = 0; $this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; break 1; case 8: //下中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; break 1; case 9: //下中偏上100px $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 100; break 1; default: //下右 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; $this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; break 1; } } /** * 水印文字圖片位置,根據(jù)需求調(diào)整 */ private function waterposStr() { $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 3; } private function waterimg($type='') { if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){ //die('水印比原圖大!'); return array('data'=>'','info'=>'水印比原圖大!','status'=>0); } if($type == 'waterstr'){ $this->waterposStr(); }else{ $this->waterpos(); } $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]); imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]); $pct = $this->transparent; imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]); imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct); } private function waterstr() { $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr); $w = abs($rect[2]-$rect[6]); $h = abs($rect[3]-$rect[7]); $fontHeight = $this->fontSize; $this->water_im = imagecreatetruecolor($w, $h); imagealphablending($this->water_im,false); imagesavealpha($this->water_im,true); $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127); imagefill($this->water_im,0,0,$white_alpha); $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]); imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr); $this->waterImg_info = array(0=>$w,1=>$h); $this->waterimg($type='waterstr'); } /** * 繪制矩形區(qū) * bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) * @author liuzp111 */ public function drawRectangle() { //imagefill($im,0,0,$gray);//填充資源,填充的坐標(biāo)(類似PS魔棒),顏色 /* * 1--------------畫(huà)長(zhǎng)方形-------------- * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) * 參數(shù): 畫(huà)布資源, 左上角x坐標(biāo),左上y坐標(biāo),右下x坐標(biāo),右下y坐標(biāo),顏色 */ $color = imagecolorallocate($this->im,255,255,255);//創(chuàng)建矩形邊框顏色和填充顏色 //========================================================================= //繪制矩形區(qū)域并填充 // 參數(shù)說(shuō)明: //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) // im:為將圖像載入為圖像資源 // $x1:表示矩形左上角的X坐標(biāo) // $y1:表示矩形左上角的Y坐標(biāo) // $x2:表示矩形右下角的X坐標(biāo) // $y2:表示矩形右下角的Y坐標(biāo) // $color:為填充的RGB顏色 // imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color); //不要使用下方的函數(shù)填充,下方填充函數(shù)為魔棒填充,容易導(dǎo)致填充不完整 //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充資源,填充的坐標(biāo)(魔棒),顏色 } function output() { $this->imginfo(); //是否創(chuàng)建矩形區(qū)域 if($this->is_draw_rectangle){ $this->drawRectangle(); } if ($this->waterTypeStr ) { $this->waterstr(); } if($this->waterTypeImage ) { $this->waterimginfo(); $this->waterimg(); } switch ($this->srcImg_info[2]) { case 3: $res_output = imagepng($this->im,$this->output_img); break 1; case 2: $res_output = imagejpeg($this->im,$this->output_img); break 1; case 1: $res_output = imagegif($this->im,$this->output_img); break 1; default: // die('添加水印失敗!'); return array('data'=>'','info'=>'添加水印失敗!','status'=>0); break; } imagedestroy($this->im); imagedestroy($this->water_im); return array('data'=>$res_output,'info'=>'添加水印成功!','status'=>1); } }
使用方式:
$file = '58368dddc8c51_22';//需要加水印的圖片 $file_ext = '.jpeg';//擴(kuò)展名 $imgFileName = './'.$file.$file_ext;//需要加水印圖片路徑 $obj = new WaterMask($imgFileName); //實(shí)例化對(duì)象 $obj->waterTypeStr = true; //開(kāi)啟文字水印 $obj->waterTypeImage = true; //開(kāi)啟圖片水印 $obj->pos = 9; //定義水印圖片位置 $obj->waterImg = './water.png'; //水印圖片 $obj->transparent = 100; //水印透明度 $obj->waterStr = '保險(xiǎn)經(jīng)紀(jì)人:劉測(cè)試 電話:02052552'; //水印文字 $obj->fontSize = 9; //文字字體大小 $obj->fontColor = array(0,0,0); //水印文字顏色(RGB) $obj->fontFile = './font/msyh.ttc'; //字體文件,這里是微軟雅黑 $obj->is_draw_rectangle = TRUE; //開(kāi)啟繪制矩形區(qū)域 $obj ->output_img = './'.$file.'_n'.$file_ext;//輸出的圖片路徑 $obj->output();
“PHP水印類支持添加圖片、文字、填充顏色區(qū)域的代碼分享”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(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)容。