PHP Multi-image upload code to add watermarks

Source: Internet
Author: User
  1. PHP watermark function
  2. function Imagewatermark ($groundImage, $waterPos =0, $waterImage = "", $waterText = "", $textFont =5, $textColor = "#FF0000")
  3. {
  4. $isWaterImage = FALSE;
  5. $FORMATMSG = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, PNG format. ";
  6. Read 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];//To obtain the width of the watermark picture
  12. $water _h = $water _info[1];//To obtain a high watermark image
  13. Switch ($water _info[2])//Get the format of the watermark picture
  14. {
  15. Case 1: $water _im = Imagecreatefromgif ($waterImage); Convert a picture to PHP's recognizable encoding process
  16. Case 2: $water _im = Imagecreatefromjpeg ($waterImage); Convert a picture to PHP's recognizable encoding process
  17. Case 3: $water _im = Imagecreatefrompng ($waterImage); Convert a picture to PHP's recognizable encoding process
  18. Default:die ($FORMATMSG);
  19. }
  20. }
  21. Read background picture
  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 a high background picture
  27. Switch ($ground _info[2])//Get the format of the background picture
  28. {
  29. Case 1: $ground _im = Imagecreatefromgif ($groundImage);
  30. Case 2: $ground _im = Imagecreatefromjpeg ($groundImage);
  31. Case 3: $ground _im = Imagecreatefrompng ($groundImage);
  32. Default:die ($FORMATMSG);
  33. }
  34. }
  35. Else
  36. {
  37. Die ("The picture that needs watermark does not exist!") ");
  38. }
  39. Watermark Location
  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);//Gets the range of text that uses 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 picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ";
  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 for top left
  66. $posX = 0;
  67. $posY = 0;
  68. Break
  69. Case 2://2 centered on the top
  70. $posX = ($ground _w-$w)/2;
  71. $posY = 0;
  72. Break
  73. Case 3://3 for top right
  74. $posX = $ground _w-$w;
  75. $posY = 0;
  76. Break
  77. Case 4://4 for the middle left
  78. $posX = 0;
  79. $posY = ($ground _h-$h)/2;
  80. Break
  81. Case 5://5 centered in the middle
  82. $posX = ($ground _w-$w)/2;
  83. $posY = ($ground _h-$h)/2;
  84. Break
  85. Case 6://6 for the middle right
  86. $posX = $ground _w-$w;
  87. $posY = ($ground _h-$h)/2;
  88. Break
  89. Case 7://7 for Bottom left
  90. $posX = 0;
  91. $posY = $ground _h-$h;
  92. Break
  93. Case 8://8 centered at bottom
  94. $posX = ($ground _w-$w)/2;
  95. $posY = $ground _h-$h;
  96. Break
  97. Case 9://9 for 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));
  104. Break
  105. }
  106. To set the color blending mode of an image
  107. Imagealphablending ($ground _im, true);
  108. if ($isWaterImage)//Picture watermark
  109. {
  110. Imagecopy ($ground _im, $water _im, $posX, $posY, 0, 0, $water _w, $water _h);//copy watermark to target file
  111. }
  112. else//text watermark
  113. {
  114. if (!empty ($textColor) && (strlen ($textColor) ==7))
  115. {
  116. $R = Hexdec (substr ($textColor,));
  117. $G = Hexdec (substr ($textColor, 3,2));
  118. $B = Hexdec (substr ($textColor, 5));
  119. }
  120. Else
  121. {
  122. Die ("Watermark text color format is not correct! ");
  123. }
  124. Imagestring ($ground _im, $textFont, $posX, $posY, $waterText, Imagecolorallocate ($ground _im, $R, $G, $B));
  125. }
  126. Create a picture after a watermark
  127. @unlink ($groundImage);
  128. Switch ($ground _info[2])//Get the format of the background picture
  129. {
  130. Case 1:imagegif ($ground _im, $groundImage); Create a picture in GIF format
  131. Case 2:imagejpeg ($ground _im, $groundImage); Create a picture in JPEG format
  132. Case 3:imagepng ($ground _im, $groundImage); Create a picture in PNG format
  133. Default:die ($ERRORMSG);
  134. }
  135. Freeing memory
  136. if (Isset ($water _info)) unset ($water _info);
  137. if (Isset ($water _im)) Imagedestroy ($water _im);
  138. unset ($ground _info);
  139. Imagedestroy ($ground _im);
  140. }
  141. ?>
  142. PHP Image upload code:
  143. for ($i =0; $i
  144. {
  145. $upfile = "./img/". ($i + 1). ". PNG ";//Here's your path.
  146. if (Move_uploaded_file ($_files[' userfile '] [' tmp_name '] [$i], $upfile)) {
  147. Imagewatermark ($upfile, 9, "./shuiyin.png", "Made by Chenduan", 5, "#FF0000");
  148. /*
  149. * Function: Image watermark (watermark supports picture or text)
  150. * Imagewatermark ($groundImage, $waterPos =0, $waterImage = "", $waterText = "", $textFont =5, $textColor = "#FF0000")
  151. Parameters
  152. * $groundImage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
  153. * $waterPos watermark position, there are 10 kinds of states, 0 is a random position;
  154. * 1 for the top left, 2 for the top center, 3 for the top right;
  155. * 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
  156. * 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
  157. * $waterImage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
  158. * $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
  159. * $textFont Text size, value 1, 2, 3, 4 or 5, default is 5;
  160. * $textColor text color, value is hexadecimal color value, default is #ff0000 (red);
  161. */
  162. echo "";
  163. echo "First". ($i + 1). " Picture Operation succeeded
    ";
  164. }
  165. else{
  166. echo "First". ($i + 1). " Picture can't be uploaded.
    ";
  167. }
  168. }
  169. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.