版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中職老年服務(wù)與管理(養(yǎng)老服務(wù))試題及答案
- 2025年高職水產(chǎn)養(yǎng)殖學(xué)(水產(chǎn)動(dòng)物養(yǎng)殖)試題及答案
- 2025年高職(新能源汽車檢測(cè)與維修)維修技術(shù)試題及答案
- 2025年高職助產(chǎn)學(xué)(產(chǎn)科護(hù)理技術(shù))試題及答案
- 禁毒安全教育內(nèi)容課件
- 口腔醫(yī)學(xué)考研就業(yè)前景
- 2026年幼兒春節(jié)故事歡歡喜喜過(guò)大年
- 光伏技術(shù)交底全套
- 光伏培訓(xùn)教學(xué)課件
- 2024黑龍江省各級(jí)機(jī)關(guān)考試錄用公務(wù)員備考題庫(kù)及參考答案詳解
- TOC基本課程講義學(xué)員版-王仕斌
- T-GDWCA 0035-2018 HDMI 連接線標(biāo)準(zhǔn)規(guī)范
- 面板堆石壩面板滑模結(jié)構(gòu)設(shè)計(jì)
- 初中語(yǔ)文新課程標(biāo)準(zhǔn)與解讀課件
- 無(wú)人機(jī)裝調(diào)檢修工培訓(xùn)計(jì)劃及大綱
- 中建通風(fēng)與空調(diào)施工方案
- 高考語(yǔ)言運(yùn)用題型之長(zhǎng)短句變換 學(xué)案(含答案)
- 春よ、來(lái)い(春天來(lái)了)高木綾子演奏長(zhǎng)笛曲譜鋼琴伴奏
- ARJ21機(jī)型理論知識(shí)考試題庫(kù)(匯總版)
- 2023年婁底市建設(shè)系統(tǒng)事業(yè)單位招聘考試筆試模擬試題及答案解析
- GB/T 4623-2014環(huán)形混凝土電桿
評(píng)論
0/150
提交評(píng)論