php圖片自動加入浮水印程式碼,php圖片浮水印函數程式碼

WBOY
發布: 2016-07-25 08:51:53
原創
972 人瀏覽過
  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(!empty($waterimage) && file_exists($waterimage))
  26. {
  27. $iswaterimage = true;
  28. $water_info = getimagesize($iswaterimage = true;
  29. $water_info = getimagesize($ waterimage);
  30. $water_w = $water_info[0];//取得浮水印圖片的寬度
  31. $water_h = $water_info[1];//取得水印圖片的高
  32. switch($water_info[2 ])//取得浮水印圖片的格式
  33. {
  34. case 1:$water_im = imagecreatefromgif($waterimage);break;
  35. case 2:$water_im = imagecreatefromjpeg($waterimage);break;break
  36. case 3:$water_im = imagecreatefrompng($waterimage);break;
  37. default:die($formatmsg);
  38. }
  39. }
  40. //讀取背景圖片
  41. if(!empty( $groundimage) && file_exists($groundimage))
  42. {
  43. $ground_info = getimagesize($groundimage);
  44. $ground_w = $ground_info[0];//取得背景圖片的寬度
  45. $ground_w = $ground_info[0];//取得背景圖片的寬度
  46. $ground_h = $ground_info[1];//取得背景圖片的高
  47. switch($ground_info[2])//取得背景圖片的格式
  48. {
  49. case 1:$ground_im = imagecreatefromgif($groundimage) ;break;
  50. case 2:$ground_im = imagecreatefromjpeg($groundimage);break;
  51. case 3:$ground_im = imagecreatefrompng($groundimage);break;
  52. default:die($formatmsg) }
  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*2.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_im , $water_im, $posx, $posy, 0, 0, $water_w,$water_h);//拷貝水印到目標檔案
  129. }
  130. else//文字浮水印
  131. {
  132. if( ! empty($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. case 2:imagejpeg($ground_im,$groundimage);break;
  149. case 3:imagepng($ground_im,$groundimage);break;
  150. default: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. if(isset($_files) && !empty($_files['userfile ']) && $_files['userfile']['size']>0)
  160. {
  161. $uploadfile = "./".time()."_".$_files['userfile'][ '名稱'];
  162. if (copy($_files['userfile']['tmp_name'], $uploadfile))
  163. {
  164. echo "ok
    ";
  165. //文字水印
  166. imagewatermark($uploadfile,0,"","http://blog.csdn.net/longware/",5,"#ff0000");
  167. //圖片浮水印
  168. //$ waterimage="./hanweb_shuiyin.gif";//水印圖片路徑
  169. //imagewatermark($uploadfile,0,$waterimage);
  170. echo "php圖片自動加入浮水印程式碼,php圖片浮水印函數程式碼";
  171. }
  172. else
  173. {
  174. echo "fail
    ";
  175. }
  176. }
  177. ?>
  178. 檔案:
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!