PHP水印類,支持添加圖片、文字、填充顏色區(qū)域的實現(xiàn)
更新時間:2017年02月04日 09:01:51 投稿:jingxian
下面小編就為大家?guī)硪黄狿HP水印類,支持添加圖片、文字、填充顏色區(qū)域的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
*自己整理的一個水印類*
支持添加圖片、文字、填充顏色區(qū)域
<?php
/**
* 圖片加水印類,支持文字水印、透明度設(shè)置、自定義水印位置等。
* 使用示例:
* $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)數(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 = ''; //存儲輸出圖片到哪里
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.')格式不對,只支持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;
}
}
/**
* 水印文字圖片位置,根據(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--------------畫長方形--------------
* bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
* 參數(shù): 畫布資源, 左上角x坐標(biāo),左上y坐標(biāo),右下x坐標(biāo),右下y坐標(biāo),顏色
*/
$color = imagecolorallocate($this->im,255,255,255);//創(chuàng)建矩形邊框顏色和填充顏色
//=========================================================================
//繪制矩形區(qū)域并填充
// 參數(shù)說明:
//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';//擴展名 $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 = '保險經(jīng)紀(jì)人:劉測試 電話:02052552'; //水印文字 $obj->fontSize = 9; //文字字體大小 $obj->fontColor = array(0,0,0); //水印文字顏色(RGB) $obj->fontFile = './font/msyh.ttc'; //字體文件,這里是微軟雅黑 $obj->is_draw_rectangle = TRUE; //開啟繪制矩形區(qū)域 $obj ->output_img = './'.$file.'_n'.$file_ext;//輸出的圖片路徑 $obj->output();
以上這篇PHP水印類,支持添加圖片、文字、填充顏色區(qū)域的實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP實現(xiàn)Redis單據(jù)鎖以及防止并發(fā)重復(fù)寫入
本篇文章給大家分享了PHP實現(xiàn)Redis單據(jù)鎖以及如何防止并發(fā)重復(fù)寫入的方法,對此有需要的朋友參考學(xué)習(xí)下。2018-04-04
Zend Framework教程之Zend_Db_Table用法詳解
這篇文章主要介紹了Zend Framework教程之Zend_Db_Table用法,結(jié)合實例形式詳細(xì)分析了Zend_Db_Table的功能,使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-03-03
PHP SPL標(biāo)準(zhǔn)庫之SplFixedArray使用實例
這篇文章主要介紹了PHP SPL標(biāo)準(zhǔn)庫之SplFixedArray使用實例,SplFixedArray主要是處理數(shù)組相關(guān)的主要功能,它是固定長度的,比普通的數(shù)組處理更快,需要的朋友可以參考下2015-05-05

