php圖片浮水印類,php添加中文浮水印代碼

來源:互聯網
上載者:User
  1. Header("Content-type: image/png"); /*通知瀏覽器,要輸出映像*/
  2. $im = imagecreate(400 , 300); /*定義映像的大小*/
  3. $gray = ImageColorAllocate($im , 235 , 235 , 235);
  4. $pink = ImageColorAllocate($im, 255 , 128 , 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=”",$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 = getimagesize($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_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);break;
  50. default:die($formatMsg);
  51. }
  52. }
  53. else
  54. {
  55. die(”需要加浮水印的圖片不存在!”);
  56. }
  57. //浮水印位置
  58. if($isWaterImage)//圖片浮水印
  59. {
  60. $w = $water_w;
  61. $h = $water_h;
  62. $label = “圖片的”;
  63. }
  64. else//文字浮水印
  65. {
  66. $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字型的文本的範圍
  67. $w = $temp[2] - $temp[6];
  68. $h = $temp[3] - $temp[7];
  69. unset($temp);
  70. $label = “文字地區”;
  71. }
  72. if( ($ground_w<$w) || ($ground_h<$h) )
  73. {
  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_im, $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. {
  140. die(”浮水印文字顏色格式不正確!”);
  141. }
  142. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  143. }
  144. //產生浮水印後的圖片
  145. @unlink($groundImage);
  146. switch($ground_info[2])//取得背景圖片的格式
  147. {
  148. case 1:imagegif($ground_im,$groundImage);break;
  149. case 2:imagejpeg($ground_im,$groundImage);break;
  150. case 3:imagepng($ground_im,$groundImage);break;
  151. default:die($errorMsg);
  152. }
  153. //釋放記憶體
  154. if(isset($water_info)) unset($water_info);
  155. if(isset($water_im)) imagedestroy($water_im);
  156. unset($ground_info);
  157. imagedestroy($ground_im);
  158. }
  159. //—————————————————————————————
  160. $id=$_REQUEST['id'];
  161. $num = count($_FILES['userfile']['name']);
  162. print_r($_FILES['userfile']);
  163. print_r($_FILES['userfile']['name']);
  164. echo $num;
  165. echo “
    ”;
  166. if(isset($id)){
  167. for($i=0;$i<$id;$i++){
  168. if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
  169. {
  170. $uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];
  171. echo “
    ”;
  172. echo $uploadfile;
  173. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
  174. {
  175. echo “OK
    ”;
  176. //文字浮水印
  177. //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);
  178. //圖片浮水印
  179. $waterImage=”logo_ok1.gif”;//浮水印圖片路徑
  180. imageWaterMark($uploadfile,9,$waterImage);
  181. echo “”;
  182. }
  183. else
  184. {
  185. echo “Fail
    ”;
  186. }
  187. }
  188. }
  189. }
  190. ?>
複製代碼

代碼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];
  64. $imgHeight = $groundImageType[1];
  65. $imgType = $groundImageType[2];
  66. if($imgWidth < $this->BaseWidth) //小於基本寬度,不處理
  67. return;
  68. //圖片不是jpg/jpeg/gif/png時,不處理
  69. switch($imgType)
  70. {
  71. case 1:
  72. $image = imagecreatefromgif($inImg);
  73. $this->TextBgColor = "#ffffff"; //gif圖片字型只能為黑,所以背景顏色就設定為白色
  74. break;
  75. case 2:
  76. $image = imagecreatefromjpeg($inImg);
  77. break;
  78. case 3:
  79. $image = imagecreatefrompng($inImg);
  80. break;
  81. default:
  82. return;
  83. break;
  84. }
  85. //建立顏色
  86. $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor,5,2))); //文字顏色
  87. //產生一個空的圖片,它的高度在底部增加浮水印高度
  88. $newHeight = $imgHeight+20;
  89. $objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight);
  90. $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); //背景顏色
  91. //填充圖片的背景顏色
  92. @imagefill ($objTmpImg,0,0,$colorBg);
  93. //把原圖copy到臨時圖片中
  94. @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
  95. //建立要寫入的浮水印文字對象
  96. $objText = $this->createText($this->Text);
  97. //計算要寫入的浮水印文字的位置
  98. $x = 5;
  99. $y = $newHeight-5;
  100. //寫入文字浮水印
  101. @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText);
  102. //產生新的圖片,臨時圖片
  103. switch($imgType)
  104. {
  105. case 1:
  106. imagegif($objTmpImg,$tmpImg);
  107. break;
  108. case 2:
  109. imagejpeg($objTmpImg,$tmpImg);
  110. break;
  111. case 3:
  112. imagepng($objTmpImg,$tmpImg);
  113. break;
  114. default:
  115. return;
  116. break;
  117. }
  118. //釋放資源
  119. @imagedestroy($objTmpImg);
  120. @imagedestroy($image);
  121. //重新命名檔案
  122. if($this->OverFlag)
  123. {
  124. //覆蓋原圖
  125. @unlink($inImg);
  126. @rename($tmpImg,$outImg);
  127. }
  128. else
  129. {
  130. //不覆蓋原圖
  131. @rename($tmpImg,$outImg);
  132. }
  133. }
  134. //----------
  135. //功能:建立浮水印文字對象
  136. //參數:無
  137. //返回:建立的浮水印文字對象
  138. function createText($instring)
  139. {
  140. $outstring="";
  141. $max=strlen($instring);
  142. for($i=0;$i<$max;$i++)
  143. {
  144. $h=ord($instring[$i]);
  145. if($h>=160 && $i<$max-1)
  146. {
  147. $outstring .= "&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr($instring,$i,2))),16,10).";";
  148. $i++;
  149. }
  150. else
  151. {
  152. $outstring .= $instring[$i];
  153. }
  154. }
  155. return $outstring;
  156. }
  157. }//class
  158. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.