首頁 後端開發 php教程 php為圖片加中文浮水印的程式碼

php為圖片加中文浮水印的程式碼

Jul 25, 2016 am 09:08 AM

  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:

  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)) 或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:

  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 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;
  35. case 3:$water_im = imagecreatefrompng($waterImage);break;
  36. default:die($formatMsg);
  37. }
  38. }
  39. //讀取背景圖片
  40. if(!emptyempty( $groundImage) && file_exists($groundImage))
  41. {
  42. $ground_info = getimagesize($groundImage);
  43. $ground_w = $ground_info[0];//取得背景圖片的寬度
  44. $ground_w = $ground_info[0];//取得背景圖片的寬度
  45. $ground_h = $ground_info[1];//取得背景圖片的高
  46. switch($ground_info[2])//取得背景圖片的格式
  47. {
  48. case 1:$ground_im = imagecreatefromgif($groundImage) ;break;
  49. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  50. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  51. breakault:die($formatMsg);
  52. }
  53. }
  54. else
  55. {
  56. die(”需要加浮水印的圖片不存在!”);
  57. }
  58. //水印位置
  59. if($isWaterImage) //圖片浮水印
  60. {
  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. unset ($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['userfile']['name']);
  163. echo $num;
  164. echo “
    ”;
  165. if(isset($id )) {
  166. for($i=0;$iif(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES[' userfile' ]['大小']>0)
  167. {
  168. $uploadfile = “./”.time().”_”.$_FILES['userfile'][姓名][$i];
  169. echo “
    ”;
  170. echo $uploadfile;
  171. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
  172. {
  173. echo “OK
    ”;
  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為圖片加中文浮水印的程式碼”;
  180. }
  181. else
  182. {
  183. echo “失敗
    ”;
  184. }
  185. }
  186. }
  187. }
  188. ?>
  189. for($a=0; $aecho “檔案:
    ”;
  190. }
  191. ?>
複製程式碼

例4,增加中文水印:

  1. /*--------------

  2. **描述:這是用於為指定圖片加上底部浮水印(不佔用圖片顯示區域)的自訂類,需建立物件呼叫
  3. **說明:1、需要gd庫支持,需要iconv支持(php5已經包含不用載入)
  4. 2、只適合三種類型的圖片,jpg/jpeg/gif/png,其它類型不處理
  5. 3、注意圖片所在目錄的屬性必須可寫
  6. 4、調用範例:
  7. $objImg = new MyWaterDownChinese();
  8. $objImg->Path = "images/";
  9. $objImg->FileName = "1.jpg";
  10. $objImg->Text = "HAHAKONGJIANJ ]HTTP://HI.BAIDU.COM/LYSONCN[/url]";
  11. $objImg->Font = "./font/simhei.ttf";
  12. $objImg->Run();
  13. **成員函數:
  14. -------------*/
  15. class MyWaterDownChinese{
  16. var $Path = "./"; //圖片所在目錄相對於調用此類的頁面的相對路徑
  17. var $FileName = ""; //圖片的名字,如“1.jpg”
  18. var $Text = ""; //圖片要加上的水印文字,支持中文
  19. var $TextColor = "#ffffff"; //文字的顏色,gif圖片時,字體顏色只能為黑色
  20. var $TextBgColor = "#000000"; //文字的背景條的顏色
  21. var $Font = "c://windows//fonts//simhei.ttf"; //字體的存放目錄,相對路徑
  22. var $OverFlag = true; //是否要覆蓋原圖,預設為覆蓋,不覆蓋時,自動在原圖檔名後+"_water_down",如“1.jpg”=> "1_water_down.jpg"
  23. var $BaseWidth = 200; //圖片的寬度至少要>=200,才會加上浮水印文字。
  24. //----------------
  25. //功能:類別的建構子(php5.0以上的形式)
  26. //參數:無
  27. //回傳:無
  28. function __construct(){;}
  29. //---------------
  30. //函數:類別的析構函數(php5 .0以上的形式)
  31. //參數:無
  32. //回傳:無
  33. function __destruct(){;}
  34. //-------------
  35. //功能:物件運作函數,為圖片加上浮水印

  36. //參數:無
  37. //傳回:無
  38. function Run()
  39. {
  40. if($this->FileName == "" || $this->Text == "")
  41. return;
  42. //偵測是否安裝GD函式庫
  43. if(false == function_exists ("gd_info"))
  44. {
  45. echo "系統沒有安裝GD庫,不能給圖片加浮水印.";
  46. return;
  47. }
  48. //設定輸入、輸出圖片路徑名
  49. $arr_in_name = explode(".",$this->FileName);
  50. //
  51. $inImg = $this->Path.$this->FileName;
  52. $outImg = $inImg ;
  53. $tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; //暫時處理的圖片,很重要
  54. if(!$this->OverFlag )
  55. $outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1];
  56. //偵測圖片是否存在
  57. if(!file_exists($inImg) )
  58. return ;
  59. //取得圖片的屬性
  60. $groundImageType = @getimagesize($inImg);
  61. $imgWidth = $groundImageType[0];
  62. $imgHeight = $groundImageType[11 ];
  63. $imgType = $groundImageType[2];
  64. if($imgWidth BaseWidth) //小於基本寬度,不處理
  65. return;
  66. //圖片不是jpg/ jpeg/gif/png時,不處理
  67. switch($imgType)
  68. {
  69. case 1:
  70. $image = imagecreatefromgif($inImg);
  71. $this->TextBgColor = "# ffffff"; //gif圖片字體只能為黑,所以背景顏色就設定為白色
  72. break;
  73. case 2:
  74. $image = imagecreatefromjpeg($inImg);
  75. break;
  76. break;
  77. case 3:
  78. $image = imagecreatefrompng($inImg);
  79. break;
  80. default:
  81. return;
  82. break;
  83. }
  84. //建立顏色
  85. break;
  86. }
  87. //建立顏色 $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor ,5,2))); //文字顏色
  88. //產生一個空的圖片,它的高度在底部增加水印高度
  89. $newHeight = $imgHeight+20;
  90. $objTmpImg = @imagecreatetruecolor ($imgWidth,$newHeight);
  91. $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,22) ),hexdec(substr($this->TextBgColor,5,2))); //背景顏色
  92. //填滿圖片的背景顏色
  93. @imagefill ($objTmpImg,0,0,$colorBg);
  94. //把原圖copy到臨時圖片中
  95. @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
  96. //創建要寫入的水印文字物件
  97. $objText = $this->createText($this->Text);
  98. //計算要寫入的水印文字的位置
  99. $x = 5;
  100. $y = $newHeight-5;
  101. //寫入文字浮水印
  102. @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText);
  103. / /產生新的圖片,臨時圖片
  104. switch($imgType)
  105. {
  106. case 1:
  107. imagegif($objTmpImg,$tmpImg);
  108. break;
  109. case 2:
  110. imagejpeg($objTmpImg,$tmpImg);
  111. break;
  112. case 3:
  113. imagepng($objTmpImg,$tmpImg);
  114. break;
  115. d; 🎜>break;
  116. }
  117. //釋放資源
  118. @imagedestroy($objTmpImg);
  119. @imagedestroy($image);
  120. //重新命名檔案
  121. if($this ->OverFlag)
  122. {
  123. //覆蓋原圖
  124. @unlink($inImg);
  125. @rename($tmpImg,$outImg);
  126. }
  127. else
  128. {
  129. //不覆蓋原圖
  130. @rename($tmpImg,$outImg);
  131. }
  132. }
  133. //-------
  134. //功能:建立水印文字物件
  135. //參數:無
  136. //返回:建立的水印文字物件
  137. function createText($instring)
  138. {
  139. $outstring="";
  140. $ max=strlen($instring);
  141. for($i=0;$i{
  142. $h=ord($instring[$i]);
  143. if($h>=160 && $i{
  144. $outstring .= "".base_convert(bin2hex(iconv("gb2312","ucs-2",substr( $instring,$i,2))),16,10).";";
  145. $i++;
  146. }
  147. else
  148. {
  149. $outstring .= $instring[$i ];
  150. }
  151. }
  152. return $outstring;
  153. }
  154. }//class
  155. ?>
複製代碼



本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

11個最佳PHP URL縮短腳本(免費和高級) 11個最佳PHP URL縮短腳本(免費和高級) Mar 03, 2025 am 10:49 AM

11個最佳PHP URL縮短腳本(免費和高級)

Instagram API簡介 Instagram API簡介 Mar 02, 2025 am 09:32 AM

Instagram API簡介

在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash會話數據

構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

構建具有Laravel後端的React應用程序:第2部分,React

簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

簡化的HTTP響應在Laravel測試中模擬了

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

php中的捲曲:如何在REST API中使用PHP捲曲擴展

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12個最佳PHP聊天腳本

宣布 2025 年 PHP 形勢調查 宣布 2025 年 PHP 形勢調查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形勢調查

See all articles