PHP加水印代码 支持文字和图片水印_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?>

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。
