php多圖片上傳添加浮水印的程式碼

WBOY
發布: 2016-07-25 08:51:49
原創
910 人瀏覽過
  1. //php水印函數
  2. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText=" ",$textFont=5,$textColor="#FF0000")
  3. {
  4. $isWaterImage = FALSE;
  5. $formatMsg = "暫不支援此檔案格式,請用圖片處理軟體將圖片轉換為GIF、JPG、PNG格式。";
  6. //讀取浮水印檔案
  7. if(!empty($waterImage) && file_exists($waterImage))
  8. {
  9. $isWaterImage = TR$;
  10. $water_info = getimagesize( waterImage); //得到的是一個陣列
  11. $water_w = $water_info[0];//取得浮水印圖片的寬度
  12. $water_h = $water_info[1];//取得浮水印圖片的高
  13. switch($water_info[2])//取得浮水印圖片的格式
  14. {
  15. case 1:$water_im = imagecreatefromgif($waterImage);break; //將圖片轉換為PHP可辨識的編碼過程
  16. case 2:$water_im = imagecreatefromjpeg($waterImage);break; //將圖片轉換為PHP可辨識的編碼過程
  17. case 3:$water_im = imagecreatefrompng($waterImage);break; //將圖片轉換為PHP可辨識的編碼過程
  18. default:die($formatMsg);
  19. }
  20. }
  21. //讀取背景圖片
  22. if(!empty($groundImage) && file_exists($ groundImage))
  23. {
  24. $ground_info = getimagesize($groundImage);
  25. $ground_w = $ground_info[0];//取得背景圖片的寬度
  26. $ground_h = $ground_info[1]; //取得背景圖片的高
  27. switch($ground_info[2])//取得背景圖片的格式
  28. {
  29. case 1:$ground_im = imagecreatefromgif($groundImage);;
  30. breakcase 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  31. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  32. default:die($formatMsg);
  33. }
  34. default:die($formatMsg);
  35. }
  36. }
  37. } 🎜>else
  38. {
  39. die("需要加浮水印的圖片不存在! ");
  40. }
  41. //水印位置
  42. if($isWaterImage)//圖片水印
  43. {
  44. $w = $water_w;
  45. $h = $water_h;
  46. $label = "圖片的";
  47. }
  48. else//文字浮水印
  49. {
  50. $temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts /stcaiyun.ttf",$waterText);//取得使用TrueType 字體的文字的範圍
  51. $w = $temp[2] - $temp[6];
  52. $h = $temp[3] - $temp[7];
  53. unset($temp);
  54. $label = "文字區域";
  55. }
  56. if( ($ground_w{
  57. echo "需要加浮水印的圖片的長度或寬度比水印".$label."還小,無法產生浮水印! ";
  58. return;
  59. }
  60. switch($waterPos)
  61. {
  62. case 0://隨機
  63. $posX = rand(0,($ground_w - $w)) ;
  64. $posY = rand(0,($ground_h - $h));
  65. break;
  66. case 1://1為頂端居左
  67. $posX = 0;
  68. $posY = 0;
  69. break;
  70. case 2://2為頂端居中
  71. $posX = ($ground_w - $w) / 2;
  72. $posY = 0;
  73. break;
  74. case 3://3為頂端居右
  75. $posX = $ground_w - $w;
  76. $posY = 0;
  77. break;
  78. case 4://4為中部居左
  79. $posX = 0;
  80. $posY = ($ground_h - $h) / 2;
  81. break;
  82. case 5://5為中部居中
  83. $posX = ($ground_w - $w) / 2;
  84. $posY = ($ground_h - $h) / 2;
  85. break;
  86. case 6://6為中部居右
  87. $posX = $ground_w - $ w;
  88. $posY = ($ground_h - $h) / 2;
  89. break;
  90. case 7://7為底端居左
  91. $posX = 0;
  92. $posY = $ground_h - $h;
  93. break;
  94. case 8://8為底端居中
  95. $posX = ($ground_w - $w) / 2;
  96. $posY = $ground_h - $ h;
  97. break;
  98. case 9://9為底端居右邊
  99. $posX = $ground_w - $w;
  100. $posY = $ground_h - $h;
  101. break;
  102. default://隨機
  103. $posX = rand(0,($ground_w - $w));
  104. $posY = rand(0,($ground_h - $h));
  105. break ;
  106. }
  107. //設定影像的混色模式
  108. imagealphablending($ground_im, true);
  109. if($isWaterImage)//圖片浮水印
  110. {
  111. 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);//拷貝水印到目標檔案
  112. }
  113. else//文字浮水印
  114. {
  115. if( ! empty($textColor) && (strlen($textColor)==7) )
  116. {
  117. $R = hexdec(substr($textColor,1,2));
  118. $G = hexdec(substr( $textColor,3,2));
  119. $B = hexdec(substr($textColor,5));
  120. }
  121. else { die("水印文字顏色格式不正確!");
  122. }
  123. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  124. }
  125. //產生浮水印後的圖片
  126. @unlink($groundImage);
  127. switch($ground_info[2])//取得背景圖片的格式
  128. {
  129. case 1:imagegif($ground_im, $groundImage);break; //建立gif格式的圖片
  130. case 2:imagejpeg($ground_im,$groundImage);break; //建立jpeg格式的圖片
  131. case 3:imagepng($ground_im,$groundImage );break; //建立png格式的圖片
  132. default:die($errorMsg);
  133. }
  134. //釋放記憶體
  135. if(isset($water_info)) unset($water_info);
  136. if(isset($water_im)) imagedestroy($water_im);
  137. unset($ground_info);
  138. imagedestroy($ground_im);
  139. }
  140. ?>
  141. php圖片上傳程式碼:
  142. for ($i=0;$i{
  143. $upfile="./img/".($i 1).".png";//此處路徑換成你的
  144. if(move_uploaded_file($_FILES['userfile']['tmp_name'][ $i],$upfile)){
  145. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");
  146. /*
  147. *功能:圖片浮水印(浮水印支援圖片或文字)
  148. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  149. * 參數:
  150. * $groundImage 背景圖片,即需要加浮水印的圖片,暫只支援GIF,JPG,PNG格式;
  151. * $waterPos 水印位置,有10種狀態,0為隨機位置;
  152. * 1為頂端居左,2為頂端居中,3為頂端居右;
  153. * 4為中部居左,5為中部居中,6為中部居右;
  154. * 7為底端居左,5為中部居中,6為中部居右;
  155. * 7為底端居左,8為底端居中,9為底端居右;
  156. * $waterImage 圖片水印,即作為浮水印的圖片,暫只支援GIF,JPG,PNG格式;
  157. * $waterText 文字水印,即把文字作為水印,支援ASCII碼,不支援中文;
  158. * $textFont 文字大小,值為1、2、3、4或5,預設為5;
  159. * $textColor 文字顏色,值為十六進位顏色值,預設為#FF0000(紅色);
  160. */
  161. echo "php多圖片上傳添加浮水印的程式碼";
  162. echo "第".($i 1)."張圖片操作成功
    ";
  163. }
  164. else{
  165. echo "第".($i 1)."張圖片上傳不了
    " ;
  166. }
  167. }
?>
複製程式碼


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