Php multi-image Upload watermark code

Source: Internet
Author: User
Php multi-image Upload watermark code

  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 Currently. use image processing software to convert images to GIF, JPG, and PNG formats. ";
  6. // Read the watermark file
  7. If (! Empty ($ waterImage) & file_exists ($ waterImage ))
  8. {
  9. $ IsWaterImage = TRUE;
  10. $ Water_info = getimagesize ($ waterImage); // you can obtain an array.
  11. $ Water_w = $ water_info [0]; // Obtain the watermark image width.
  12. $ Water_h = $ water_info [1]; // Get the watermark image height
  13. Switch ($ water_info [2]) // Obtain the watermark image format
  14. {
  15. Case 1: $ water_im = imagecreatefromgif ($ waterImage); break; // converts an image into a PHP identifiable encoding process
  16. Case 2: $ water_im = imagecreatefromjpeg ($ waterImage); break; // converts an image into a PHP recognizable encoding process
  17. Case 3: $ water_im = imagecreatefrompng ($ waterImage); break; // converts an image into a PHP identifiable encoding process
  18. Default: die ($ formatMsg );
  19. }
  20. }
  21. // Read the background image
  22. If (! Empty ($ groundImage) & file_exists ($ groundImage ))
  23. {
  24. $ Ground_info = getimagesize ($ groundImage );
  25. $ Ground_w = $ ground_info [0]; // Obtain the width of the background image.
  26. $ Ground_h = $ ground_info [1]; // get the height of the background image
  27. Switch ($ ground_info [2]) // Obtain the background image format
  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 ("the image to be watermarked does not exist! ");
  38. }
  39. // Watermark Position
  40. If ($ isWaterImage) // image watermark
  41. {
  42. $ W = $ water_w;
  43. $ H = $ water_h;
  44. $ Label = "image ";
  45. }
  46. Else // text watermark
  47. {
  48. $ Temp = imagettfbbox (ceil ($ textFont * 2.5), 0, "c:/windows/fonts/stcaiyun. ttf ", $ waterText); // get the text range using the TrueType font
  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 to be watermark is smaller than that of the watermark". $ label. ". 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 is the top left
  66. $ PosX = 0;
  67. $ PosY = 0;
  68. Break;
  69. Case 2: // 2 center the top
  70. $ PosX = ($ ground_w-$ w)/2;
  71. $ PosY = 0;
  72. Break;
  73. Case 3: // 3: top right
  74. $ PosX = $ ground_w-$ w;
  75. $ PosY = 0;
  76. Break;
  77. Case 4: // 4 is left in the middle
  78. $ PosX = 0;
  79. $ PosY = ($ ground_h-$ h)/2;
  80. Break;
  81. Case 5: // 5 center in the middle
  82. $ PosX = ($ ground_w-$ w)/2;
  83. $ PosY = ($ ground_h-$ h)/2;
  84. Break;
  85. Case 6: // 6 is the center and right
  86. $ PosX = $ ground_w-$ w;
  87. $ PosY = ($ ground_h-$ h)/2;
  88. Break;
  89. Case 7: // 7 is left at the bottom
  90. $ PosX = 0;
  91. $ PosY = $ ground_h-$ h;
  92. Break;
  93. Case 8: // 8 is centered at the bottom
  94. $ PosX = ($ ground_w-$ w)/2;
  95. $ PosY = $ ground_h-$ h;
  96. Break;
  97. Case 9: // 9: 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. // Set the mixed color mode of the image
  107. Imagealphablending ($ ground_im, true );
  108. If ($ isWaterImage) // image watermark
  109. {
  110. Imagecopy ($ ground_im, $ water_im, $ posX, $ posY, 0, 0, $ water_w, $ water_h); // Copy the watermark to the target file
  111. }
  112. Else // text watermark
  113. {
  114. If (! Empty ($ textColor) & (strlen ($ textColor) = 7 ))
  115. {
  116. $ R = hexdec (substr ($ textColor, 1, 2 ));
  117. $ G = hexdec (substr ($ textColor, 3, 2 ));
  118. $ B = hexdec (substr ($ textColor, 5 ));
  119. }
  120. Else
  121. {
  122. Die ("the watermark text color format is incorrect! ");
  123. }
  124. Imagestring ($ ground_im, $ textFont, $ posX, $ posY, $ waterText, imagecolorallocate ($ ground_im, $ R, $ G, $ B ));
  125. }
  126. // The image after the watermark is generated
  127. @ Unlink ($ groundImage );
  128. Switch ($ ground_info [2]) // Obtain the background image format
  129. {
  130. Case 1: imagegif ($ ground_im, $ groundImage); break; // create a gif image
  131. Case 2: imagejpeg ($ ground_im, $ groundImage); break; // create an image in jpeg format
  132. Case 3: imagepng ($ ground_im, $ groundImage); break; // Create a png image
  133. Default: die ($ errorMsg );
  134. }
  135. // Release the 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. $ Upfile = "./img/". ($ I + 1). ". png"; // replace the path with your
  145. If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'] [$ I], $ upfile )){
  146. ImageWaterMark ($ upfile, 9, "./shuiyin.png", "Made By Chenduan", 5, "# FF0000 ");
  147. /*
  148. * Function: image watermark (watermarks support images or text)
  149. * ImageWaterMark ($ groundImage, $ waterPos = 0, $ waterImage = "", $ waterText = "", $ textFont = 5, $ textColor = "# FF0000 ")
  150. * Parameters:
  151. * $ GroundImage: specifies the background image to be Watermark. Currently, only GIF, JPG, and PNG formats are supported;
  152. * $ WaterPos watermark position, which has 10 statuses. 0 indicates a random position;
  153. * 1 is the top left, 2 is the top center, and 3 is the top right;
  154. * 4: center left, 5: center, and 6: center right;
  155. * 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
  156. * $ WaterImage: specifies the image Watermark. Currently, only the GIF, JPG, and PNG formats are supported;
  157. * $ WaterText text watermark: uses text as a watermark. it supports ASCII codes and does not support Chinese characters;
  158. * $ TextFont text size. The value is 1, 2, 3, 4, or 5. the default value is 5;
  159. * $ TextColor text color. The value is a hexadecimal color value. The default value is # FF0000 (red );
  160. */
  161. Echo "";
  162. Echo "". ($ I + 1). "Image operation successful
    ";
  163. }
  164. Else {
  165. Echo "no." ($ I + 1). "the image cannot be uploaded.
    ";
  166. }
  167. }
  168. ?>

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.