您好,登錄后才能下訂單哦!
本篇內容介紹了“PHP水印類支持添加圖片、文字、填充顏色區域的代碼分享”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
*自己整理的一個水印類*
支持添加圖片、文字、填充顏色區域
<?php /** * 圖片加水印類,支持文字水印、透明度設置、自定義水印位置等。 * 使用示例: * $obj = new WaterMask($imgFileName); //實例化對象 * $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; //水印類型:啟用圖片水印 public $waterTypeStr = false; //水印類型:啟用文字水印 public $pos = 0; //水印位置 public $transparent = 45; //水印透明度(0---100)數值越大越不透明 public $waterStr = 'icp.niufee.com'; //水印文字 public $fontSize = 14; //文字字體大小 public $fontColor = array(0,0,0); //水印文字顏色(RGB) 默認黑色 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坐標 private $y = ''; //水印y坐標 public $output_img = ''; //存儲輸出圖片到哪里 public $is_draw_rectangle = false; //是否繪制矩形區域 (暫不支持自定義位置) //public $rectange_color = ''; //繪制矩形區域的顏色 private $result_array = array(); //結果數組 public function __construct($img) { //析構函數 //$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.')格式不對,只支持PNG、JPEG、GIF。'); return array('data'=>'','info'=>'原圖片('.$this->srcImg.')格式不對,只支持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.')格式不對,只支持PNG、JPEG、GIF。'); return array('data'=>'','info'=>'水印圖片('.$this->srcImg.')格式不對,只支持PNG、JPEG、GIF。','status'=>0); } } private function waterpos() { //水印位置算法 switch ($this->pos) { case 0: //隨機位置 $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; } } /** * 水印文字圖片位置,根據需求調整 */ 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'); } /** * 繪制矩形區 * 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);//填充資源,填充的坐標(類似PS魔棒),顏色 /* * 1--------------畫長方形-------------- * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) * 參數: 畫布資源, 左上角x坐標,左上y坐標,右下x坐標,右下y坐標,顏色 */ $color = imagecolorallocate($this->im,255,255,255);//創建矩形邊框顏色和填充顏色 //========================================================================= //繪制矩形區域并填充 // 參數說明: //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) // im:為將圖像載入為圖像資源 // $x1:表示矩形左上角的X坐標 // $y1:表示矩形左上角的Y坐標 // $x2:表示矩形右下角的X坐標 // $y2:表示矩形右下角的Y坐標 // $color:為填充的RGB顏色 // imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color); //不要使用下方的函數填充,下方填充函數為魔棒填充,容易導致填充不完整 //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充資源,填充的坐標(魔棒),顏色 } function output() { $this->imginfo(); //是否創建矩形區域 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';//擴展名 $imgFileName = './'.$file.$file_ext;//需要加水印圖片路徑 $obj = new WaterMask($imgFileName); //實例化對象 $obj->waterTypeStr = true; //開啟文字水印 $obj->waterTypeImage = true; //開啟圖片水印 $obj->pos = 9; //定義水印圖片位置 $obj->waterImg = './water.png'; //水印圖片 $obj->transparent = 100; //水印透明度 $obj->waterStr = '保險經紀人:劉測試 電話:02052552'; //水印文字 $obj->fontSize = 9; //文字字體大小 $obj->fontColor = array(0,0,0); //水印文字顏色(RGB) $obj->fontFile = './font/msyh.ttc'; //字體文件,這里是微軟雅黑 $obj->is_draw_rectangle = TRUE; //開啟繪制矩形區域 $obj ->output_img = './'.$file.'_n'.$file_ext;//輸出的圖片路徑 $obj->output();
“PHP水印類支持添加圖片、文字、填充顏色區域的代碼分享”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。