PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例_第1頁(yè)
PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例_第2頁(yè)
PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例_第3頁(yè)
PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例_第4頁(yè)
PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例PHP實(shí)現(xiàn)的給圖片添加水印功能,可添加文字水印或圖片水印,使用文字水印時(shí)需要提供字體文件,使用圖片水印時(shí)需要提供水印圖片,水印圖片不能比要添加水印的圖片大,請(qǐng)使用背景透明的水印圖片。

該水印類支持自定義水印位置、自定義水印大小和水印的透明度,字體水印可自定義顏色等,功能已相應(yīng)完善。

完整源代碼如下(注解中已給出使用示例):

*圖片加水印類,支持文字水印、透明度設(shè)置、自定義水印位置等。

*使用示例:

*$obj=newWaterMask($imgFileName);//實(shí)例化對(duì)象

*$obj-$waterType=1;//類型:0為文字水印、1為圖片水印

*$obj-$transparent=45;//水印透明度

*$obj-$waterStr='';//水印文字

*$obj-$fontSize=18;//文字字體大小

*$obj-$fontColor=array(255,255,255);//水印文字顏色(RGB)

*$obj-$fontFile='AHGBold.ttf';//字體文件

*……

*$obj-output();//輸出水印圖片文件覆蓋到輸入的圖片文件

classWaterMask{

public$waterType=0;//水印類型:0為文字水印、1為圖片水印

public$pos=0;//水印位置

public$transparent=45;//水印透明度

public$waterStr='';//水印文字

public$fontSize=18;//文字字體大小

public$fontColor=array(255,255,255);//水印文字顏色(RGB)

public$fontFile='AHGBold.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)

function__construct($img){//析構(gòu)函數(shù)

$this-srcImg=file_exists($img)$img:die('"'.$img.'"源文件不存在!');

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.')格式不對(duì),只支持PNG、JPEG、GIF。');

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.')格式不對(duì),只支持PNG、JPEG、GIF。');

privatefunctionwaterpos(){//水印位置算法

switch($this-pos){

case0://隨機(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]);

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;

default://下右

$this-x=$this-srcImg_info[0]-$this-waterImg_info[0];

$this-y=$this-srcImg_info[1]-$this-waterImg_info[1];

break1;

privatefunctionwaterimg(){

if($this-srcImg_info[0]=$this-waterImg_info[0]||$this-srcImg_info[1]=$this-waterImg_info[1]){

die('水印比原圖大!');

$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=

$this-waterimg();

functionoutput(){

$this-imginfo();

if($this-waterType==0){

$this-waterstr();

}else{

$this-waterimginfo();

$this-waterimg();

switch($this-srcImg_info[2]){

case3:

imagepng($this-im,$this-srcImg);

break1;

case2:

imagejpeg($this-im,$t

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論