*自己整理的一個水印類*
支持添加圖片、文字、填充顏色區域
代碼如下 復制代碼<?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
*/
classWaterMask{
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(); //結果數組
publicfunction__construct($img) { //析構函數
//$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
if(file_exists($img)){
$this->srcImg =$img;
}else{
returnarray('data'=>'','info'=>'源文件不存在!','status'=>0);
}
}
privatefunctionimginfo() { //獲取需要添加水印的圖片的信息,並載入圖片。
$this->srcImg_info =getimagesize($this->srcImg);
switch($this->srcImg_info[2]) {
case3:
$this->im = imagecreatefrompng($this->srcImg);
break1;
case2:
$this->im = imagecreatefromjpeg($this->srcImg);
break1;
case1:
$this->im = imagecreatefromgif($this->srcImg);
break1;
default:
//die('原圖片('.$this->srcImg.')格式不對,只支持PNG、JPEG、GIF。');
returnarray('data'=>'','info'=>'原圖片('.$this->srcImg.')格式不對,只支持PNG、JPEG、GIF。','status'=>0);
}
}
privatefunctionwaterimginfo() { //獲取水印圖片的信息,並載入圖片。
$this->waterImg_info =getimagesize($this->waterImg);
switch($this->waterImg_info[2]) {
case3:
$this->water_im = imagecreatefrompng($this->waterImg);
break1;
case2:
$this->water_im = imagecreatefromjpeg($this->waterImg);
break1;
case1:
$this->water_im = imagecreatefromgif($this->waterImg);
break1;
default:
//die('水印圖片('.$this->srcImg.')格式不對,只支持PNG、JPEG、GIF。');
returnarray('data'=>'','info'=>'水印圖片('.$this->srcImg.')格式不對,只支持PNG、JPEG、GIF。','status'=>0);
}
}
privatefunctionwaterpos() { //水印位置算法
switch($this->pos) {
case0: //隨機位置
$this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
$this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
break1;
case1: //上左
$this->x = 0;
$this->y = 0;
break1;
case2: //上中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y = 0;
break1;
case3: //上右
$this->x =$this->srcImg_info[0]-$this->waterImg_info[0];
$this->y = 0;
break1;
case4: //中左
$this->x = 0;
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
break1;
case5: //中中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
break1;
case6: //中右
$this->x =$this->srcImg_info[0]-$this->waterImg_info[0];
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
break1;
case7: //下左
$this->x = 0;
$this->y =$this->srcImg_info[1]-$this->waterImg_info[1];
break1;
case8: //下中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y =$this->srcImg_info[1]-$this->waterImg_info[1];
break1;
case9: //下中偏上100px
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y =$this->srcImg_info[1]-$this->waterImg_info[1] - 100;
break1;
default: //下右
$this->x =$this->srcImg_info[0]-$this->waterImg_info[0];
$this->y =$this->srcImg_info[1]-$this->waterImg_info[1];
break1;
}
}
/**
* 水印文字圖片位置,根據需求調整
*/
privatefunctionwaterposStr() {
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y =$this->srcImg_info[1]-$this->waterImg_info[1] - 3;
}
privatefunctionwaterimg($type='') {
if($this->srcImg_info[0] <=$this->waterImg_info[0] ||$this->srcImg_info[1] <=$this->waterImg_info[1]){
//die('水印比原圖大!');
returnarray('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);
}
privatefunctionwaterstr() {
$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
*/
publicfunctiondrawRectangle()
{
//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);//填充資源,填充的坐標(魔棒),顏色
}
functionoutput() {
$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]) {
case3:
$res_output= imagepng($this->im,$this->output_img);
break1;
case2:
$res_output= imagejpeg($this->im,$this->output_img);
break1;
case1:
$res_output= imagegif($this->im,$this->output_img);
break1;
default:
// die('添加水印失敗!');
returnarray('data'=>'','info'=>'添加水印失敗!','status'=>0);
break;
}
imagedestroy($this->im);
imagedestroy($this->water_im);
returnarray('data'=>$res_output,'info'=>'添加水印成功!','status'=>1);
}
}
使用方式:
代碼如下 復制代碼$file='58368dddc8c51_22';//需要加水印的圖片
$file_ext='.jpeg';//擴展名
$imgFileName='./'.$file.$file_ext;//需要加水印圖片路徑
$obj=newWaterMask($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();