搜尋
首頁後端開發php教程PHP加水印代码 支持文字和图片水印_PHP教程

PHP加水印代码 支持文字和图片水印_PHP教程

Jul 13, 2016 am 10:32 AM
php程式碼圖片支援水印類別背景設定透明度

   PHP加图片水印、文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置、水印图片背景透明。自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有类使用方法。

  001

  002class WaterMask{

  003 public $waterType = 1; //水印类型:0为文字水印、1为图片水印

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

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

  006 public $waterStr = 'www.codefans.net'; //水印文字

  007 public $fontSize = 16; //文字字体大小

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

  009 public $fontFile = 'AHGBold.ttf';//字体文件

  010 public $waterImg = 'logo.png';//水印图片

  011 private $srcImg = '';//需要添加水印的图片

  012 private $im = '';//图片句柄

  013 private $water_im = '';//水印图片句柄

  014 private $srcImg_info = '';//图片信息

  015 private $waterImg_info = '';//水印图片信息

  016 private $str_w = '';//水印文字宽度

  017 private $str_h = '';//水印文字高度

  018 private $x = '';//水印X坐标

  019 private $y = '';//水印y坐标

  020 function __construct($img) {

  021 $this->srcImg = file_exists($img) $img : die('"'.$img.'" 对不起,水印文件不存在!');

  022 }

  023 private function imginfo() {//获取水印图片信息,并加载。

  024 $this->srcImg_info = getimagesize($this->srcImg);

  025 switch ($this->srcImg_info[2]) {

  026 case 3:

  027 $this->im = imagecreatefrompng($this->srcImg);

  028 break 1;

  029 case 2:

  030 $this->im = imagecreatefromjpeg($this->srcImg);

  031 break 1;

  032 case 1:

  033 $this->im = imagecreatefromgif($this->srcImg);

  034 break 1;

  035 default:

  036 die('水印图片('.$this->srcImg.')水印图片格式不对,请选择PNG、JPEG、GIF格式的图片。');

  037 }

  038 }

  039 private function waterimginfo() {//获取水印图片并载入。

  040 $this->waterImg_info = getimagesize($this->waterImg);

  041 switch ($this->waterImg_info[2]) {

  042 case 3:

  043 $this->water_im = imagecreatefrompng($this->waterImg);

  044 break 1;

  045 case 2:

  046 $this->water_im = imagecreatefromjpeg($this->waterImg);

  047 break 1;

  048 case 1:

  049 $this->water_im = imagecreatefromgif($this->waterImg);

  050 break 1;

  051 default:

  052 die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');

  053 }

  054 }

  055 private function waterpos() {//水印位置算法

  056 switch ($this->pos) {

  057 case 0: //随机位置

  058 $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);

  059 $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);

  060 break 1;

  061 case 1: //上左

  062 $this->x = 0;

  063 $this->y = 0;

  064 break 1;

  065 case 2: //上中

  066 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

  067 $this->y = 0;

  068 break 1;

  069 case 3: //上右

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

  071 $this->y = 0;

  072 break 1;

  073 case 4: //中左

  074 $this->x = 0;

  075 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  076 break 1;

  077 case 5: //中中

  078 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

  079 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  080 break 1;

  081 case 6: //中右

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

  083 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  084 break 1;

  085 case 7: //下左

  086 $this->x = 0;

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

  088 break 1;

  089 case 8: //下中

  090 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

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

  092 break 1;

  093 default: //下右

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

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

  096 break 1;

  097 }

  098 }

  099 private function waterimg() {

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

  101 die('水印比原图大!');

  102 }

  103 $this->waterpos();

  104 $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);

  105 imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);

  106 $pct = $this->transparent;

  107 imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);

  108 imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);

  109 }

  110 private function waterstr() {

  111 $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);

  112 $w = abs($rect[2]-$rect[6]);

  113 $h = abs($rect[3]-$rect[7]);

  114 $fontHeight = $this->fontSize;

  115 $this->water_im = imagecreatetruecolor($w, $h);

  116 imagealphablending($this->water_im,false);

  117 imagesavealpha($this->water_im,true);

  118 $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);

  119 imagefill($this->water_im,0,0,$white_alpha);

  120 $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);

  121 imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);

  122 $this->waterImg_info = array(0=>$w,1=>$h);

  123 $this->waterimg();

  124 }

  125 function output() {

  126 $this->imginfo();

  127 if ($this->waterType == 0) {

  128 $this->waterstr();

  129 }else {

  130 $this->waterimginfo();

  131 $this->waterimg();

  132 }

  133 switch ($this->srcImg_info[2]) {

  134 case 3:

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

  136 break 1;

  137 case 2:

  138 imagejpeg($this->im,$this->srcImg);

  139 break 1;

  140 case 1:

  141 imagegif($this->im,$this->srcImg);

  142 break 1;

  143 default:

  144 die('原因未知:水印添加失败!');

  145 break;

  146 }

  147 imagedestroy($this->im);

  148 imagedestroy($this->water_im);

  149 }

  150}

  151?>

  PHP生成水印类用法:

  view sourceprint?01

  02$obj = new WaterMask($imgFileName);

  03$obj->$waterType = 1;//水印类型:0为文字水印、1为图片水印

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

  05$obj->$waterStr = 'www.codefans.net';//水印文字

  06$obj->$fontSize = 16;//字体大小

  07$obj->$fontColor = array(255,0,255);//水印文字颜色(RGB值)

  08$obj->$fontFile = = 'AHGBold.ttf'; //字体名称

  09$obj->output(); //输出水印图片

  10?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/755612.htmlTechArticlePHP加图片水印、文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置、水印图片背景透明。自己写的一个类,因为自己开发的一...
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能