PHP code to add watermark to multiple image uploads

WBOY
Release: 2016-07-25 08:51:49
Original
910 people have browsed it
  1. //php watermark function
  2. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000 ")
  3. {
  4. $isWaterImage = FALSE;
  5. $formatMsg = "This file format is not supported yet. Please use image processing software to convert the image to GIF, JPG, or PNG format.";
  6. //Read the watermark file
  7. if(!empty($waterImage) && file_exists($waterImage))
  8. {
  9. $isWaterImage = TRUE;
  10. $water_info = getimagesize($waterImage); //What you get is an array
  11. $water_w = $water_info[0];//Get the width of the watermark image
  12. $water_h = $water_info[1];//Get the height of the watermark image
  13. switch($water_info[2])//Get the format of the watermark image
  14. {
  15. case 1:$water_im = imagecreatefromgif($waterImage);break; //Convert the image into an encoding process that can be recognized by PHP
  16. case 2:$water_im = imagecreatefromjpeg($waterImage);break; //Convert the image into PHP-recognizable encoding process
  17. case 3:$water_im = imagecreatefrompng($waterImage);break; //Convert the image into PHP-recognizable encoding process
  18. default:die($formatMsg);
  19. }
  20. }
  21. //Read Get the background image
  22. if(!empty($groundImage) && file_exists($groundImage))
  23. {
  24. $ground_info = getimagesize($groundImage);
  25. $ground_w = $ground_info[0];//Get the width of the background image
  26. $ ground_h = $ground_info[1]; //Get the height of the background image
  27. switch($ground_info[2]) //Get the format of the background image
  28. {
  29. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  30. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  31. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  32. default:die($formatMsg);
  33. }
  34. }
  35. else
  36. {
  37. die("required The watermarked picture does not exist! ");
  38. }
  39. //Watermark position
  40. if($isWaterImage)//Picture watermark
  41. {
  42. $w = $water_w;
  43. $h = $water_h;
  44. $label = "Picture";
  45. }
  46. else/ /Text watermark
  47. {
  48. $temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts/stcaiyun.ttf",$waterText);//Get the range of text using TrueType fonts
  49. $ w = $temp[2] - $temp[6];
  50. $h = $temp[3] - $temp[7];
  51. unset($temp);
  52. $label = "text area";
  53. }
  54. if ( ($ground_w<$w) || ($ground_h<$h) )
  55. {
  56. echo "The length or width of the image that needs to be watermarked is smaller than the watermark ".$label.", and the watermark cannot be generated! ";
  57. return;
  58. }
  59. switch($waterPos)
  60. {
  61. case 0://random
  62. $posX = rand(0,($ground_w - $w));
  63. $posY = rand(0,($ground_h - $h));
  64. break;
  65. case 1://1 means top left
  66. $posX = 0;
  67. $posY = 0;
  68. break;
  69. case 2://2 means top center
  70. $posX = ($ ground_w - $w) / 2;
  71. $posY = 0;
  72. break;
  73. case 3://3 is the top right
  74. $posX = $ground_w - $w;
  75. $posY = 0;
  76. break;
  77. case 4 ://4 means center left
  78. $posX = 0;
  79. $posY = ($ground_h - $h) / 2;
  80. break;
  81. case 5://5 means center center
  82. $posX = ($ground_w - $w ) / 2;
  83. $posY = ($ground_h - $h) / 2;
  84. break;
  85. case 6://6 is the middle right
  86. $posX = $ground_w - $w;
  87. $posY = ($ground_h - $h) / 2;
  88. break;
  89. case 7://7 is bottom left
  90. $posX = 0;
  91. $posY = $ground_h - $h;
  92. break;
  93. case 8://8 is bottom center
  94. $posX = ($ground_w - $w) / 2;
  95. $posY = $ground_h - $h;
  96. break;
  97. case 9://9 is bottom right
  98. $posX = $ground_w - $w;
  99. $posY = $ground_h - $h;
  100. break;
  101. default://random
  102. $posX = rand(0,($ground_w - $w));
  103. $posY = rand(0,($ground_h - $h) ; posY, 0, 0, $water_w,$water_h);//Copy watermark to target file
  104. }
  105. else//Text watermark
  106. {
  107. if( !empty($textColor) && (strlen($textColor)==7) )
  108. {
  109. $R = hexdec(substr($textColor,1,2));
  110. $G = hexdec(substr($textColor,3,2));
  111. $B = hexdec(substr($textColor,5) );
  112. }
  113. else
  114. {
  115. die("The watermark text color format is incorrect!");
  116. }
  117. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  118. }
  119. //The image after generating the watermark
  120. @unlink($groundImage);
  121. switch($ground_info[2])//Get the format of the background image
  122. {
  123. case 1:imagegif($ground_im,$groundImage);break; //Create an image in gif format
  124. case 2 :imagejpeg($ground_im,$groundImage);break; //Create a picture in jpeg format
  125. case 3:imagepng($ground_im,$groundImage);break; //Create a picture in png format
  126. default:die($errorMsg);
  127. }
  128. //Release memory
  129. if(isset($water_info)) unset($water_info);
  130. if(isset($water_im)) imagedestroy($water_im);
  131. unset($ground_info);
  132. imagedestroy($ground_im) ;
  133. }
  134. ?>
  135. php image upload code:
  136. for ($i=0;$i {
  137. $upfile="./img/".($i+1).".png";//Change the path here to yours
  138. if(move_uploaded_file($_FILES['userfile']['tmp_name'] [$i],$upfile)){
  139. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");
  140. /*
  141. * Function: Image watermark ( Watermark supports pictures or text)
  142. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  143. * Parameters:
  144. * $ groundImage background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, and PNG formats;
  145. * $waterPos watermark position, there are 10 states, 0 is a random position;
  146. * 1 means top left, 2 means top center, 3 means top is on the right;
  147. * 4 is middle on the left, 5 is middle on the center, 6 is middle on the right;
  148. * 7 is bottom on the left, 8 is bottom on the center, 9 is bottom on the right;
  149. * $waterImage picture Watermark, that is, the image used as a watermark, currently only supports GIF, JPG, and PNG formats;
  150. * $waterText text watermark, that is, text is used as a watermark, supports ASCII code, does not support Chinese;
  151. * $textFont text size, value is 1 , 2, 3, 4 or 5, the default is 5;
  152. * $textColor text color, the value is a hexadecimal color value, the default is #FF0000 (red);
  153. */
  154. echo "";
  155. echo "No.".($i+1)."Picture operation successful
    ";
  156. }
  157. else{
  158. echo "No.". ($i+1)."Unable to upload picture
    ";
  159. }
  160. }
  161. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!