首頁  >  文章  >  後端開發  >  php圖片水印類,php為圖片添加水印函數程式碼

php圖片水印類,php為圖片添加水印函數程式碼

WBOY
WBOY原創
2016-07-25 08:51:52843瀏覽

}

?>;
  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 = getsize($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; 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. $label = “圖片的”;
  62. }
  63. else//文字浮水印
  64. {
  65. $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf” ,$waterText);//取得使用TrueType 字型的文字的範圍
  66. $w = $temp[2] - $temp[6];
  67. $h = $temp[3] - $temp[7] ;
  68. unset($temp);
  69. $label = “文字區域”;
  70. }
  71. if( ($ground_w {
  72. echo 「需要加浮水印的圖片的長度或寬度比水印」.$label.」還小,無法產生浮水印! 」;
  73. return;
  74. }
  75. switch($waterPos)
  76. {
  77. case 0://隨機
  78. $posX = rand(0,($ground_w - $w)) ;
  79. $posY = rand(0,($ground_h - $h));
  80. break;
  81. case 1://1為頂端居左
  82. $posX = 0;
  83. $posY = 0;
  84. break;
  85. case 2://2為頂端居中
  86. $posX = ($ground_w - $w) / 2;
  87. $posY = 0;
  88. break;
  89. case 3://3為頂端居右
  90. $posX = $ground_w - $w;
  91. $posY = 0;
  92. break;
  93. case 4://4為中部居左
  94. $posX = 0;
  95. $posY = ($ground_h - $h) / 2;
  96. break;
  97. case 5://5為中部居中
  98. $posX = ($ground_w - $w) / 2;
  99. $posY = ($ground_h - $h) / 2;
  100. break;
  101. case 6://6為中部居右
  102. $posX = $ground_w - $ w;
  103. $posY = ($ground_h - $h) / 2;
  104. break;
  105. case 7://7為底端居左
  106. $posX = 0;
  107. $posY = $ground_h - $h;
  108. break;
  109. case 8://8為底端居中
  110. $posX = ($ground_w - $w) / 2;
  111. $posY = $ground_h - $ h;
  112. break;
  113. case 9://9為底端居右邊
  114. $posX = $ground_w - $w;
  115. $posY = $ground_h - $h;
  116. break;
  117. default://隨機
  118. $posX = rand(0,($ground_w - $w));
  119. $posY = rand(0,($ground_h - $h));
  120. break ;
  121. }
  122. //設定影像的混色模式
  123. imagealphablending($ground_im, true);
  124. if($isWaterImage)//圖片浮水印
  125. {
  126. imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy($ground_imagecopy) , $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷貝水印到目標檔案
  127. }
  128. else//文字浮水印
  129. {
  130. if( ! emptyempty($textColor) && (strlen($textColor)==7) )
  131. {
  132. $R = hexdec(substr($textColor,1,2));
  133. $G = hexdec(substr( $textColor,3,2));
  134. $B = hexdec(substr($textColor,5));
  135. }
  136. else
  137. {
  138. die(」水印文字顏色格式不正確!」);
  139. }
  140. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. //產生浮水印後的圖片
  153. @unlink($groundImage);
  154. switch($ground_info[2])//取得背景圖片的格式
  155. {
  156. case 1:imagegif($ground_im, $groundImage);break;
  157. 案例2:imagejpeg($ground_im,$groundImage);break;
  158. 案例3:imagepng($ground_im,$groundImage);break;
  159. 預設:die($errorMsg) ;
  160. }
  161. //釋放記憶體
  162. if(isset($water_info)) unset($water_info);
  163. if(isset($water_im)) imagedestroy($water_im);
  164. 未設定($ground_info);
  165. imagedestroy($ground_im);
  166. }
  167. //- bbs.it-home.org
  168. $id=$_REQUEST['id'];
  169. $ num = count($_FILES['userfile']['name']);
  170. print_r($_FILES['userfile']);
  171. print_r($_FILES['使用者檔案']['名稱'] );
  172. 回顯$num;
  173. 回顯「
    」;
  174. if(isset($id)){
  175. for($i=0;$iif(isset($_FILES) && !emptyempty($_FILES[' userfile']) && $_FILES['userfile']['size']>0)
  176. {
  177. $uploadfile = “./”.time().”_”.$_FILES['userfile' ][姓名][$i];
  178. 回顯“
    ”;
  179. echo $uploadfile;
  180. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
  181. {
  182. echo “確定
    ”;
  183. //文字浮水印
  184. //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);
  185. //圖片浮水印
  186. $waterImage=”logo_ok1.gif” ;//浮水印圖片路徑
  187. imageWaterMark($uploadfile,9,$waterImage);
  188. echo “php圖片水印類,php為圖片添加水印函數程式碼“;
  189. }
  190. else
  191. {
  192. echo “失敗
    ”;
  193. }
  194. }
  195. }
  196. }
? >
; for($a=0;$a echo “檔案: ;
”;
複製程式碼

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