搜尋
首頁後端開發php教程php圖片水印類,php添加中文水印代碼

  1. Header("Content-type: image/png"); /*通知瀏覽器,要輸出圖片*/
  2. $im = imagecreate(400 , 300); /*定義影像的大小*/
  3. $gray = ImageColorAllocate($im , 235 , 235 , 235);
  4. $pink = ImageColorAllocate($im, 2555 , 2555, 255, );
  5. $fontfile = "simkai.ttf";
  6. /* $fontfile 字體的路徑,視操作系統而定,可以是simhei.ttf(黑體) , SIMKAI.TTF(楷體) , SIMFANG.TTF (仿宋) ,SIMSUN.TTC(宋體&新宋體) 等GD 支持的中文字體*/
  7. $str = iconv('GB2312','UTF-8','中文水印'); /*將gb2312 的字元集轉換成UTF-8 的字元*/
  8. ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str);
  9. /* 加入中文浮水印*/
  10. Imagepng($im);
  11. ImageDestroy($im);
  12. ?>
複製程式碼

例2,php圖片浮水印程式碼。

  1. // ************************ **************** //
  2. // 功能: 新增文字
  3. // 參數: $img 圖片檔名
  4. // $new_img 儲存圖片檔案名稱,如果為空表示不另存圖片
  5. // $text 字串內容
  6. // text_size 字串大小
  7. // text_angle 字型字串輸出角度
  8. // text_x 字串輸出x座標
  9. // text_y 字串輸出y 座標
  10. // $text_font 字體檔案名稱
  11. // $r,$g,$b 字串顏色RGB值
  12. // **** ************************************ //
  13. function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){
  14. $text=iconv("gb2312","UTF-8",$text);
  15. Header("Content-type: image/gif");
  16. $im = @imagecreatefromstring(file_get_contents($img)) or die ("開啟圖片失敗!");
  17. $color = ImageColorAllocate( $im, $r,$g,$b);
  18. //ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text):
  19. //本函數將TTF (TrueType Fonts) 字體文字寫入圖片。
  20. //參數: size 為字形的尺寸;
  21. // angle 為字體的角度,順時針計算,0 度為水平(從左到右),90 度則為由下到上的文字;
  22. // x,y 二參數為文字的座標值(原點為左上角);
  23. // col 為字的顏色;
  24. // fontfile 為字體檔案名稱;
  25. / / text 是字串內容。
  26. ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);
  27. if ($new_img==""):
  28. ImageGif($im ); // 不儲存圖片,只顯示
  29. else:
  30. ImageGif($im,$new_img); // 儲存圖片,但不顯示
  31. endif;
  32. ImageDestroy($im); / /結束圖形,釋放記憶體空間
  33. }
  34. ?>
複製程式碼

例3,php圖片浮水印,支援php文字浮水印效果。

  1. /*
  2. * 功能:PHP圖片水印(水印支援圖片或文字)
  3. * 參數:
  4. * $ $ groundImage 背景圖片,即需要加浮水印的圖片,暫只支援GIF,JPG,PNG格式;
  5. * $waterPos 浮水印位置,有10種狀態,0為隨機位置;
  6. * 1為頂端居左,2為頂端居中,3為頂端居右;
  7. * 4為中部居左,5為中部居中,6為中部居右;
  8. * 7為底端居左,8為底端居中,9為底端居右;
  9. * $waterImage 圖片浮水印,即作為浮水印的圖片,暫只支援GIF,JPG,PNG格式;
  10. * $waterText 文字水印,即把文字當作水印,支援ASCII碼,不支援中文;
  11. * $textFont 文字大小,值為1、2、3、4或5,預設為5;
  12. * $textColor 文字顏色,值為十六進位顏色值,預設為#FF0000(紅);
  13. *
  14. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  15. * $waterImage 和$waterText 最好不要同時使用,選其中一個即可,優先使用$waterImage。
  16. * 當$waterImage有效時,參數$waterString、$stringFont、$stringColor皆不生效。
  17. * 加浮水印後的圖片的檔案名稱和 $groundImage 一樣。
  18. * 作者:longware @ 2004-11-3 14:15:13
  19. */
  20. function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$waterText=”",$ textFont=5,$textColor=”#FF0000″)
  21. {
  22. $isWaterImage = FALSE;
  23. $formatMsg = 「暫不支援此檔案格式,請用圖片處理軟體將圖片轉換為GIF、JPG 、PNG格式。”;
  24. //讀取浮水印檔案
  25. if(!emptyempty($waterImage) && file_exists($waterImage))
  26. {
  27. $isWaterImage = TRUE;
  28. $water_info = getidize($water_info =$9000 waterImage);
  29. $water_w = $water_info[0];//取得浮水印圖片的寬度
  30. $water_h = $water_info[1];//取得浮水印圖片的高
  31. switch($water_info[2 ])//取得浮水印圖片的格式
  32. {
  33. case 1:$water_im = imagecreatefromgif($waterImage);break;
  34. case 2:$water_im = imagecreatefromjpeg($waterImage);break; case 3:$water_im = imagecreatefrompng($waterImage);break;
  35. default:die($formatMsg);
  36. }
  37. }
  38. //讀取背景圖片
  39. if(!emptyempty( $groundImage) && file_exists($groundImage))
  40. {
  41. $ground_info = getimagesize($groundImage);
  42. $ground_w = $ground_info[0];//取得背景圖片的寬度
  43. $ground_w = $ground_info[0];//取得背景圖片的寬度
  44. $ground_h = $ground_info[1];//取得背景圖片的高
  45. switch($ground_info[2])//取得背景圖片的格式
  46. {
  47. case 1:$ground_im = imagecreatefromgif($groundImage) ;break;
  48. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  49. case 3:$ground_im = imagecreatefrompng($groundImage);;
  50. breakdefault:die($formatMsg); >}
  51. }
  52. else
  53. {
  54. die(」需要加浮水印的圖片不存在! ”);
  55. }
  56. //水印位置
  57. if($isWaterImage)//圖片水印
  58. {
  59. $w = $water_w;
  60. $h = $water_h;
  61. $w = $water_w;
  62. $h = $water_h;
  63. $label = “圖片的”;
  64. }
  65. else//文字浮水印
  66. {
  67. $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf” ,$waterText);//取得使用TrueType 字型的文字的範圍
  68. $w = $temp[2] - $temp[6];
  69. $h = $temp[3] - $temp[7] ;
  70. unset($temp);
  71. $label = “文字區域”;
  72. }
  73. if( ($ground_w {
  74. echo 「需要加浮水印的圖片的長度或寬度比水印」.$label.」還小,無法產生浮水印! 」;
  75. return;
  76. }
  77. switch($waterPos)
  78. {
  79. case 0://隨機
  80. $posX = rand(0,($ground_w - $w)) ;
  81. $posY = rand(0,($ground_h - $h));
  82. break;
  83. case 1://1為頂端居左
  84. $posX = 0;
  85. $posY = 0;
  86. break;
  87. case 2://2為頂端居中
  88. $posX = ($ground_w - $w) / 2;
  89. $posY = 0;
  90. break;
  91. case 3://3為頂端居右
  92. $posX = $ground_w - $w;
  93. $posY = 0;
  94. break;
  95. case 4://4為中部居左
  96. $posX = 0;
  97. $posY = ($ground_h - $h) / 2;
  98. break;
  99. case 5://5為中部居中
  100. $posX = ($ground_w - $w) / 2;
  101. $posY = ($ground_h - $h) / 2;
  102. break;
  103. case 6://6為中部居右
  104. $posX = $ground_w - $ w;
  105. $posY = ($ground_h - $h) / 2;
  106. break;
  107. case 7://7為底端居左
  108. $posX = 0;
  109. $posY = $ground_h - $h;
  110. break;
  111. case 8://8為底端居中
  112. $posX = ($ground_w - $w) / 2;
  113. $posY = $ground_h - $ h;
  114. break;
  115. case 9://9為底端居右邊
  116. $posX = $ground_w - $w;
  117. $posY = $ground_h - $h;
  118. break;
  119. default://隨機
  120. $posX = rand(0,($ground_w - $w));
  121. $posY = rand(0,($ground_h - $h));
  122. break ;
  123. }
  124. //設定影像的混色模式
  125. imagealphablending($ground_im, true);
  126. if($isWaterImage)//圖片浮水印
  127. {
  128. imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_image) , $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷貝水印到目標檔案
  129. }
  130. else//文字浮水印
  131. {
  132. if( ! emptyempty($textColor) && (strlen($textColor)==7) )
  133. {
  134. $R = hexdec(substr($textColor,1,2));
  135. $G = hexdec(substr( $textColor,3,2));
  136. $B = hexdec(substr($textColor,5));
  137. }
  138. else
  139. { die(」水印文字顏色格式不正確!」);
  140. }
  141. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  142. }
  143. //產生浮水印後的圖片
  144. @unlink($groundImage);
  145. switch($ground_info[2])//取得背景圖片的格式
  146. {
  147. case 1:imagegif($ground_im, $groundImage);break;
  148. 案例2:imagejpeg($ground_im,$groundImage);break;
  149. 案例3:imagepng($ground_im,$groundImage);break;
  150. 預設:die($errorMsg) ;
  151. }
  152. //釋放記憶體
  153. if(isset($water_info)) unset($water_info);
  154. if(isset($water_im)) imagedestroy($water_im);
  155. 未設定($ground_info);
  156. imagedestroy($ground_im);
  157. }
  158. //—————————————————————————— ————
  159. $id=$_REQUEST['id' ];
  160. $num = count($_FILES['userfile']['name']);
  161. print_r($_FILES['userfile ']);
  162. print_r($_FILES['使用者檔案']['名稱']);
  163. 回顯$num;
  164. 回顯「
    」;
  165. if(isset( $id)){
  166. for($i=0;$iif(isset($_FILES) && !emptyempty($_FILES[' userfile']) && $_FILES ['userfile']['size']>0)
  167. {
  168. $uploadfile = “./”.time().”_”.$_FILES['userfile' ][姓名][$i] ;
  169. 回顯「
    」;
  170. echo $uploadfile;
  171. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
  172. {
  173. echo “確定
    ”;
  174. //文字浮水印
  175. //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”# cccccc“);
  176. //圖片浮水印
  177. $waterImage=”logo_ok1.gif”;//水印圖片路徑
  178. imageWaterMark($uploadfile,9,$waterImage);
  179. echo “php圖片水印類,php添加中文水印代碼“;
  180. }
  181. else
  182. {
  183. echo “失敗
    ”;
  184. }
  185. }
  186. }
  187. }
  188. ? >
  189. ;
  190. for($a=0;$aecho “檔案: ;
    ”;
  191. }
  192. ? >
複製程式碼

代碼4 增加中文浮水印

  1. /*-----
  2. **描述:這是用於給指定圖片加上底部浮水印(不佔用圖片顯示區域)的自訂類,需建立物件呼叫
  3. **建立:2007-10-09
  4. **更新:2007-10-09
  5. **說明:1、需要gd庫支持,需要iconv支援(php5已經包含不用載入)
  6. 2、只適合三種類型的圖片,jpg/jpeg/gif/png,其它類型不處理
  7. 3、注意圖片所在目錄的屬性必須可寫
  8. 4.呼叫範例:
  9. $objImg = new MyWaterDownChinese();
  10. $objImg->Path = "images/";
  11. $objImg->FileName = "1.jpg";
  12. $ objImg->Text = "HAHAKONGJIANHTTP://HI.BAIDU.COM/LYSONCN";
  13. $objImg->Font = "./font/simhei.ttf";
  14. $objImg->Run();
  15. **成員函數:
  16. --------------*/
  17. class MyWaterDownChinese{
  18. var $Path = "./"; //圖片所在目錄相對於呼叫此類別的頁面的相對路徑
  19. var $FileName = ""; //圖片的名字,如“1.jpg”
  20. var $Text = ""; //圖片要加上的水印文字,支援中文
  21. var $TextColor = "#ffffff"; //文字的顏色,gif圖片時,字體顏色只能為黑色
  22. var $TextBgColor = "#000000"; //文字的背景條的顏色
  23. var $Font = "c://windows//fonts//simhei.ttf"; //字體的存放目錄,相對路徑
  24. var $OverFlag = true; //是否要覆蓋原圖,預設為覆蓋,不覆蓋時,自動在原圖檔名後"_water_down",如“1.jpg”=> "1_water_down.jpg"
  25. var $BaseWidth = 200; //圖片的寬度至少要>=200 ,才會加上水印文字。
  26. //--------------
  27. //功能:類別的建構子(php5.0以上的形式)
  28. //參數:無
  29. / /返回:無
  30. function __construct(){;}
  31. //------------------------
  32. //功能:類別的析構函數(php5.0以上的形式)
  33. //參數:無
  34. //回傳:無
  35. function __destruct(){;}
  36. //------ ----------------
  37. //功能:物件運作函數,為圖片加上浮水印
  38. //參數:無
  39. //回傳:無
  40. function Run()
  41. {
  42. if($this->FileName == "" || $this->Text == "")
  43. return;
  44. //偵測是否安裝GD庫
  45. if(false == function_exists("gd_info"))
  46. {
  47. echo "系統沒有安裝GD庫,不能給圖片加浮水印.";
  48. return;
  49. }
  50. //設定輸入、輸出圖片路徑名稱
  51. $arr_in_name = explode(".",$this->FileName);
  52. //
  53. $inImg = $this->Path.$this->FileName ;
  54. $outImg = $inImg;
  55. $tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; //暫時處理的圖片,很重要
  56. if(!$this->OverFlag)
  57. $outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1];
  58. //偵測圖片是否存在
  59. if(!file_exists($inImg))
  60. return ;
  61. //取得圖片的屬性
  62. $groundImageType = @getimagesize($inImg);
  63. $imgWidth = $groundImageType[0]; 🎜>$imgHeight = $groundImageType[1];
  64. $imgType = $groundImageType[2];
  65. if($imgWidth BaseWidth) //小於基本寬度,不處理
  66. return;
  67. //圖片不是jpg/jpeg/gif/png時,不處理
  68. switch($imgType)
  69. {
  70. case 1:
  71. $image = imagecreatefromgif($inImg);
  72. $this->TextBgColor = "#ffffff"; //gif圖片字體只能為黑,所以背景顏色就設定為白色
  73. break;
  74. case 2:
  75. $image = imagecreatefromjpeg($inImg );
  76. break;
  77. case 3:
  78. $image = imagecreatefrompng($inImg);
  79. break;
  80. default:
  81. return;
  82. break;
  83. default:
  84. return;
  85. break;
  86. }
  87. }
  88. break; 🎜>//建立顏色
  89. $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)), hexdec(substr($this->TextColor,5,2))); //文字顏色
  90. //產生一個空的圖片,它的高度在底部增加水印高度
  91. $newHeight = $imgHeight 20;
  92. $objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight);
  93. $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,1,2)),hexde(astrp( ->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); //背景顏色
  94. //填滿圖片的背景顏色
  95. @imagefill ($objTmpImg, 0,0,$colorBg);
  96. //把原圖copy到臨時圖片中
  97. @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
  98. //建立要寫入的水印文字物件
  99. $objText = $this->createText($this->Text);
  100. //計算要寫入的水印文字的位置
  101. $x = 5;
  102. $y = $newHeight-5;
  103. //寫入文字浮水印
  104. @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font, $objText);
  105. //產生新的圖片,臨時圖片
  106. switch($imgType)
  107. {
  108. case 1:
  109. imagegif($objTmpImg,$tmpImg); ;
  110. case 2:
  111. imagejpeg($objTmpImg,$tmpImg);
  112. break;
  113. case 3:
  114. imagepng($objTmpImg,$tmpImg);
  115. imagepng($objTmpImg,$tmpImg); ; default:
  116. return;
  117. break;
  118. }
  119. //釋放資源
  120. @imagedestroy($objTmpImg);
  121. @imagedestroy($image);
  122. //重新命名檔案
  123. if($this->OverFlag)
  124. {
  125. //覆蓋原圖
  126. @unlink($inImg);
  127. @rename($tmpImg,$outImg);
  128. }
  129. else
  130. {
  131. //不覆蓋原圖
  132. @rename($tmpImg,$outImg);
  133. }
  134. }
  135. //------ ----
  136. //功能:建立浮水印文字物件
  137. //參數:無
  138. //回傳:建立的浮水印文字物件
  139. function createText($instring)
  140. {
  141. $outstring="";
  142. $max=strlen($instring);
  143. for($i=0;$i{
  144. $h=ord( $instring[$i]);
  145. if($h>=160 && $i{
  146. $outstring .= "".base_convert(bin2hex(iconv(" gb2312","ucs-2",substr($instring,$i,2))),16,10).";";
  147. $i ;
  148. }
  149. else
  150. {
  151. $outstring .= $instring[$i];
  152. }
  153. }
  154. return $outstring;
  155. }
  156. }//class
?>
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP:服務器端腳本語言的簡介PHP:服務器端腳本語言的簡介Apr 16, 2025 am 12:18 AM

PHP是一種服務器端腳本語言,用於動態網頁開發和服務器端應用程序。 1.PHP是一種解釋型語言,無需編譯,適合快速開發。 2.PHP代碼嵌入HTML中,易於網頁開發。 3.PHP處理服務器端邏輯,生成HTML輸出,支持用戶交互和數據處理。 4.PHP可與數據庫交互,處理表單提交,執行服務器端任務。

PHP和網絡:探索其長期影響PHP和網絡:探索其長期影響Apr 16, 2025 am 12:17 AM

PHP在過去幾十年中塑造了網絡,並將繼續在Web開發中扮演重要角色。 1)PHP起源於1994年,因其易用性和與MySQL的無縫集成成為開發者首選。 2)其核心功能包括生成動態內容和與數據庫的集成,使得網站能夠實時更新和個性化展示。 3)PHP的廣泛應用和生態系統推動了其長期影響,但也面臨版本更新和安全性挑戰。 4)近年來的性能改進,如PHP7的發布,使其能與現代語言競爭。 5)未來,PHP需應對容器化、微服務等新挑戰,但其靈活性和活躍社區使其具備適應能力。

為什麼要使用PHP?解釋的優點和好處為什麼要使用PHP?解釋的優點和好處Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

揭穿神話:PHP真的是一種死語嗎?揭穿神話:PHP真的是一種死語嗎?Apr 16, 2025 am 12:15 AM

PHP沒有死。 1)PHP社區積極解決性能和安全問題,PHP7.x提升了性能。 2)PHP適合現代Web開發,廣泛用於大型網站。 3)PHP易學且服務器表現出色,但類型系統不如靜態語言嚴格。 4)PHP在內容管理和電商領域仍重要,生態系統不斷進化。 5)通過OPcache和APC等優化性能,使用OOP和設計模式提升代碼質量。

PHP與Python辯論:哪個更好?PHP與Python辯論:哪個更好?Apr 16, 2025 am 12:03 AM

PHP和Python各有優劣,選擇取決於項目需求。 1)PHP適合Web開發,易學,社區資源豐富,但語法不夠現代,性能和安全性需注意。 2)Python適用於數據科學和機器學習,語法簡潔,易學,但執行速度和內存管理有瓶頸。

PHP的目的:構建動態網站PHP的目的:構建動態網站Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP:處理數據庫和服務器端邏輯PHP:處理數據庫和服務器端邏輯Apr 15, 2025 am 12:15 AM

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

您如何防止PHP中的SQL注入? (準備的陳述,PDO)您如何防止PHP中的SQL注入? (準備的陳述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Safe Exam Browser

Safe Exam Browser

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

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器