亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁(yè) 后端開發(fā) php教程 給圖片添加水印的PHP類

給圖片添加水印的PHP類

Jul 25, 2016 am 08:42 AM

支持文字水印、圖片水印支持水印的位置隨機(jī)或固定(九宮格)水印透明度設(shè)置(圖片水印和文字水印都支持)文字 水印的字體、顏色、大小設(shè)置圖片水印的背景透明
  1. /**
  2. * 加水印類,支持文字圖片水印的透明度設(shè)置、水印圖片背景透明。
  3. * 日期:2011-09-27
  4. * 作者:www.itwhy.org
  5. * 使用:
  6. * $obj = new WaterMask($imgFileName); //實(shí)例化對(duì)象
  7. * $obj->$waterType = 1; //類型:0為文字水印、1為圖片水印
  8. * $obj->$transparent = 45; //水印透明度
  9. * $obj->$waterStr = 'www.itwhy.org'; //水印文字
  10. * $obj->$fontSize = 16; //文字字體大小
  11. * $obj->$fontColor = array(255,0255); //水印文字顏色(RGB)
  12. * $obj->$fontFile = = 'AHGBold.ttf'; //字體文件
  13. * $obj->output(); //輸出水印圖片文件覆蓋到輸入的圖片文件
  14. */
  15. class WaterMask{
  16. public $waterType = 1; //水印類型:0為文字水印、1為圖片水印
  17. public $pos = 0; //水印位置
  18. public $transparent = 45; //水印透明度
  19. public $waterStr = 'www.itwhy.org'; //水印文字
  20. public $fontSize = 16; //文字字體大小
  21. public $fontColor = array(255,0,255); //水印文字顏色(RGB)
  22. public $fontFile = 'AHGBold.ttf'; //字體文件
  23. public $waterImg = 'logo.png'; //水印圖片
  24. private $srcImg = ''; //需要添加水印的圖片
  25. private $im = ''; //圖片句柄
  26. private $water_im = ''; //水印圖片句柄
  27. private $srcImg_info = ''; //圖片信息
  28. private $waterImg_info = ''; //水印圖片信息
  29. private $str_w = ''; //水印文字寬度
  30. private $str_h = ''; //水印文字高度
  31. private $x = ''; //水印X坐標(biāo)
  32. private $y = ''; //水印y坐標(biāo)
  33. function __construct($img) { //析構(gòu)函數(shù)
  34. $this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
  35. }
  36. private function imginfo() { //獲取需要添加水印的圖片的信息,并載入圖片。
  37. $this->srcImg_info = getimagesize($this->srcImg);
  38. switch ($this->srcImg_info[2]) {
  39. case 3:
  40. $this->im = imagecreatefrompng($this->srcImg);
  41. break 1;
  42. case 2:
  43. $this->im = imagecreatefromjpeg($this->srcImg);
  44. break 1;
  45. case 1:
  46. $this->im = imagecreatefromgif($this->srcImg);
  47. break 1;
  48. default:
  49. die('原圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。');
  50. }
  51. }
  52. private function waterimginfo() { //獲取水印圖片的信息,并載入圖片。
  53. $this->waterImg_info = getimagesize($this->waterImg);
  54. switch ($this->waterImg_info[2]) {
  55. case 3:
  56. $this->water_im = imagecreatefrompng($this->waterImg);
  57. break 1;
  58. case 2:
  59. $this->water_im = imagecreatefromjpeg($this->waterImg);
  60. break 1;
  61. case 1:
  62. $this->water_im = imagecreatefromgif($this->waterImg);
  63. break 1;
  64. default:
  65. die('水印圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。');
  66. }
  67. }
  68. private function waterpos() { //水印位置算法
  69. switch ($this->pos) {
  70. case 0: //隨機(jī)位置
  71. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  72. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  73. break 1;
  74. case 1: //上左
  75. $this->x = 0;
  76. $this->y = 0;
  77. break 1;
  78. case 2: //上中
  79. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  80. $this->y = 0;
  81. break 1;
  82. case 3: //上右
  83. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  84. $this->y = 0;
  85. break 1;
  86. case 4: //中左
  87. $this->x = 0;
  88. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  89. break 1;
  90. case 5: //中中
  91. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  92. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  93. break 1;
  94. case 6: //中右
  95. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  96. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  97. break 1;
  98. case 7: //下左
  99. $this->x = 0;
  100. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  101. break 1;
  102. case 8: //下中
  103. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  104. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  105. break 1;
  106. default: //下右
  107. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  108. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  109. break 1;
  110. }
  111. }
  112. private function waterimg() {
  113. if ($this->srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){
  114. die('水印比原圖大!');
  115. }
  116. $this->waterpos();
  117. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  118. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  119. $pct = $this->transparent;
  120. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  121. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  122. }
  123. private function waterstr() {
  124. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  125. $w = abs($rect[2]-$rect[6]);
  126. $h = abs($rect[3]-$rect[7]);
  127. $fontHeight = $this->fontSize;
  128. $this->water_im = imagecreatetruecolor($w, $h);
  129. imagealphablending($this->water_im,false);
  130. imagesavealpha($this->water_im,true);
  131. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  132. imagefill($this->water_im,0,0,$white_alpha);
  133. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  134. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  135. $this->waterImg_info = array(0=>$w,1=>$h);
  136. $this->waterimg();
  137. }
  138. function output() {
  139. $this->imginfo();
  140. if ($this->waterType == 0) {
  141. $this->waterstr();
  142. }else {
  143. $this->waterimginfo();
  144. $this->waterimg();
  145. }
  146. switch ($this->srcImg_info[2]) {
  147. case 3:
  148. imagepng($this->im,$this->srcImg);
  149. break 1;
  150. case 2:
  151. imagejpeg($this->im,$this->srcImg);
  152. break 1;
  153. case 1:
  154. imagegif($this->im,$this->srcImg);
  155. break 1;
  156. default:
  157. die('添加水印失敗!');
  158. break;
  159. }
  160. imagedestroy($this->im);
  161. imagedestroy($this->water_im);
  162. }
  163. }
  164. ?>
復(fù)制代碼

PHP


本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
PHP變量范圍解釋了 PHP變量范圍解釋了 Jul 17, 2025 am 04:16 AM

PHP變量作用域常見問(wèn)題及解決方法包括:1.函數(shù)內(nèi)部無(wú)法訪問(wèn)全局變量,需使用global關(guān)鍵字或參數(shù)傳入;2.靜態(tài)變量用static聲明,只初始化一次并在多次調(diào)用間保持值;3.超全局變量如$_GET、$_POST可在任何作用域直接使用,但需注意安全過(guò)濾;4.匿名函數(shù)需通過(guò)use關(guān)鍵字引入父作用域變量,修改外部變量則需傳遞引用。掌握這些規(guī)則有助于避免錯(cuò)誤并提升代碼穩(wěn)定性。

如何在PHP中牢固地處理文件上傳? 如何在PHP中牢固地處理文件上傳? Jul 08, 2025 am 02:37 AM

要安全處理PHP文件上傳需驗(yàn)證來(lái)源與類型、控制文件名與路徑、設(shè)置服務(wù)器限制并二次處理媒體文件。1.驗(yàn)證上傳來(lái)源通過(guò)token防止CSRF并通過(guò)finfo_file檢測(cè)真實(shí)MIME類型使用白名單控制;2.重命名文件為隨機(jī)字符串并根據(jù)檢測(cè)類型決定擴(kuò)展名存儲(chǔ)至非Web目錄;3.PHP配置限制上傳大小及臨時(shí)目錄Nginx/Apache禁止訪問(wèn)上傳目錄;4.GD庫(kù)重新保存圖片清除潛在惡意數(shù)據(jù)。

在PHP中評(píng)論代碼 在PHP中評(píng)論代碼 Jul 18, 2025 am 04:57 AM

PHP注釋代碼常用方法有三種:1.單行注釋用//或#屏蔽一行代碼,推薦使用//;2.多行注釋用/.../包裹代碼塊,不可嵌套但可跨行;3.組合技巧注釋如用/if(){}/控制邏輯塊,或配合編輯器快捷鍵提升效率,使用時(shí)需注意閉合符號(hào)和避免嵌套。

發(fā)電機(jī)如何在PHP中工作? 發(fā)電機(jī)如何在PHP中工作? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

撰寫PHP評(píng)論的提示 撰寫PHP評(píng)論的提示 Jul 18, 2025 am 04:51 AM

寫好PHP注釋的關(guān)鍵在于明確目的與規(guī)范,注釋應(yīng)解釋“為什么”而非“做了什么”,避免冗余或過(guò)于簡(jiǎn)單。1.使用統(tǒng)一格式,如docblock(/*/)用于類、方法說(shuō)明,提升可讀性與工具兼容性;2.強(qiáng)調(diào)邏輯背后的原因,如說(shuō)明為何需手動(dòng)輸出JS跳轉(zhuǎn);3.在復(fù)雜代碼前添加總覽性說(shuō)明,分步驟描述流程,幫助理解整體思路;4.合理使用TODO和FIXME標(biāo)記待辦事項(xiàng)與問(wèn)題,便于后續(xù)追蹤與協(xié)作。好的注釋能降低溝通成本,提升代碼維護(hù)效率。

如何通過(guò)php中的索引訪問(wèn)字符串中的字符 如何通過(guò)php中的索引訪問(wèn)字符串中的字符 Jul 12, 2025 am 03:15 AM

在PHP中獲取字符串特定索引字符可用方括號(hào)或花括號(hào),但推薦方括號(hào);索引從0開始,超出范圍訪問(wèn)返回空值,不可賦值;處理多字節(jié)字符需用mb_substr。例如:$str="hello";echo$str[0];輸出h;而中文等字符需用mb_substr($str,1,1)獲取正確結(jié)果;實(shí)際應(yīng)用中循環(huán)訪問(wèn)前應(yīng)檢查字符串長(zhǎng)度,動(dòng)態(tài)字符串需驗(yàn)證有效性,多語(yǔ)言項(xiàng)目建議統(tǒng)一使用多字節(jié)安全函數(shù)。

快速PHP安裝教程 快速PHP安裝教程 Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

學(xué)習(xí)PHP:初學(xué)者指南 學(xué)習(xí)PHP:初學(xué)者指南 Jul 18, 2025 am 04:54 AM

易于效率,啟動(dòng)啟動(dòng)tingupalocalserverenverenvirestoolslikexamppandacodeeditorlikevscode.1)installxamppforapache,mysql,andphp.2)uscodeeditorforsyntaxssupport.3)

See all articles