Php image watermark function code (supports pictures and text)

Source: Internet
Author: User
Php image watermark function code (supports pictures and text)

  1. /*

  2. * Function: php image watermark (watermarks support images or text)
  3. * Parameters:
  4. * $ GroundImage: specifies the background image to be Watermark. Currently, only GIF, JPG, and PNG formats are supported;
  5. * $ WaterPos watermark position, which has 10 statuses. 0 indicates a random position;
  6. * 1 is the top left, 2 is the top center, and 3 is the top right;
  7. * 4: center left, 5: center, and 6: center right;
  8. * 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
  9. * $ WaterImage: specifies the image Watermark. Currently, only the GIF, JPG, and PNG formats are supported;
  10. * $ WaterText text watermark: uses text as a watermark. it supports ASCII codes and does not support Chinese characters;
  11. * $ TextFont text size. The value is 1, 2, 3, 4, or 5. the default value is 5;
  12. * $ TextColor text color. The value is a hexadecimal color value. The default value is # FF0000 (red );
  13. * Http://bbs.it-home.org
  14. * Note: Support GD 2.0, Support FreeType, GIF Read, GIF Create, JPG, and PNG
  15. * $ WaterImage and $ waterText should not be used at the same time. select one of them and use $ waterImage first.
  16. * When $ waterImage is valid, $ waterString, $ stringFont, and $ stringColor are invalid.
  17. * The name of the watermark image is the same as that of $ groundImage.
  18. * Author: longware @ 2004-11-3 14:15:13
  19. */
  20. Function imageWaterMark ($ groundImage, $ waterPos = 0, $ waterImage = "", $ waterText = "", $ textFont = 5, $ textColor = "# FF0000 ")
  21. {
  22. $ IsWaterImage = FALSE;
  23. $ FormatMsg = "this file format is not supported Currently. use image processing software to convert images to GIF, JPG, and PNG formats. ";
  24. // Read the watermark file
  25. If (! Empty ($ waterImage) & file_exists ($ waterImage ))
  26. {
  27. $ IsWaterImage = TRUE;
  28. $ Water_info = getimagesize ($ waterImage );
  29. $ Water_w = $ water_info [0]; // Obtain the watermark image width.
  30. $ Water_h = $ water_info [1]; // Get the watermark image height
  31. Switch ($ water_info [2]) // Obtain the watermark image format
  32. {
  33. Case 1: $ water_im = imagecreatefromgif ($ waterImage); break;
  34. Case 2: $ water_im = imagecreatefromjpeg ($ waterImage); break;
  35. Case 3: $ water_im = imagecreatefrompng ($ waterImage); break;
  36. Default: die ($ formatMsg );
  37. }
  38. }
  39. // Read the background image
  40. If (! Empty ($ groundImage) & file_exists ($ groundImage ))
  41. {
  42. $ Ground_info = getimagesize ($ groundImage );
  43. $ Ground_w = $ ground_info [0]; // Obtain the width of the background image.
  44. $ Ground_h = $ ground_info [1]; // get the height of the background image
  45. Switch ($ ground_info [2]) // Obtain the background image format
  46. {
  47. Case 1: $ ground_im = imagecreatefromgif ($ groundImage); break;
  48. Case 2: $ ground_im = imagecreatefromjpeg ($ groundImage); break;
  49. Case 3: $ ground_im = imagecreatefrompng ($ groundImage); break;
  50. Default: die ($ formatMsg );
  51. }
  52. }
  53. Else
  54. {
  55. Die ("the image to be watermarked does not exist! ");
  56. }
  57. // Watermark Position
  58. If ($ isWaterImage) // image watermark
  59. {
  60. $ W = $ water_w;
  61. $ H = $ water_h;
  62. $ Label = "image ";
  63. }
  64. Else // text watermark
  65. {
  66. $ Temp = imagettfbbox (ceil ($ textFont * 2.5), 0, "./cour. ttf", $ waterText); // get the text range using the TrueType font
  67. $ W = $ temp [2]-$ temp [6];
  68. $ H = $ temp [3]-$ temp [7];
  69. Unset ($ temp );
  70. $ Label = "text area ";
  71. }
  72. If ($ ground_w <$ w) | ($ ground_h <$ h ))
  73. {
  74. Echo "the length or width of the image to be watermark is smaller than that of the watermark". $ label. ". The Watermark cannot be generated! ";
  75. Return;
  76. }
  77. Switch ($ waterPos)
  78. {
  79. Case 0: // random
  80. $ PosX = rand (0, ($ ground_w-$ w ));
  81. $ PosY = rand (0, ($ ground_h-$ h ));
  82. Break;
  83. Case 1: // 1 is the top left
  84. $ PosX = 0;
  85. $ PosY = 0;
  86. Break;
  87. Case 2: // 2 center the top
  88. $ PosX = ($ ground_w-$ w)/2;
  89. $ PosY = 0;
  90. Break;
  91. Case 3: // 3: top right
  92. $ PosX = $ ground_w-$ w;
  93. $ PosY = 0;
  94. Break;
  95. Case 4: // 4 is left in the middle
  96. $ PosX = 0;
  97. $ PosY = ($ ground_h-$ h)/2;
  98. Break;
  99. Case 5: // 5 center in the middle
  100. $ PosX = ($ ground_w-$ w)/2;
  101. $ PosY = ($ ground_h-$ h)/2;
  102. Break;
  103. Case 6: // 6 is the center and right
  104. $ PosX = $ ground_w-$ w;
  105. $ PosY = ($ ground_h-$ h)/2;
  106. Break;
  107. Case 7: // 7 is left at the bottom
  108. $ PosX = 0;
  109. $ PosY = $ ground_h-$ h;
  110. Break;
  111. Case 8: // 8 is centered at the bottom
  112. $ PosX = ($ ground_w-$ w)/2;
  113. $ PosY = $ ground_h-$ h;
  114. Break;
  115. Case 9: // 9: right
  116. $ PosX = $ ground_w-$ w;
  117. $ PosY = $ ground_h-$ h;
  118. Break;
  119. Default: // random
  120. $ PosX = rand (0, ($ ground_w-$ w ));
  121. $ PosY = rand (0, ($ ground_h-$ h ));
  122. Break;
  123. }
  124. // Set the mixed color mode of the image
  125. Imagealphablending ($ ground_im, true );
  126. If ($ isWaterImage) // image watermark
  127. {
  128. Imagecopy ($ ground_im, $ water_im, $ posX, $ posY, 0, 0, $ water_w, $ water_h); // Copy the watermark to the target file
  129. }
  130. Else // text watermark
  131. {
  132. If (! Empty ($ textColor) & (strlen ($ textColor) = 7 ))
  133. {
  134. $ R = hexdec (substr ($ textColor, 1, 2 ));
  135. $ G = hexdec (substr ($ textColor, 3, 2 ));
  136. $ B = hexdec (substr ($ textColor, 5 ));
  137. }
  138. Else
  139. {
  140. Die ("the watermark text color format is incorrect! ");
  141. }
  142. Imagestring ($ ground_im, $ textFont, $ posX, $ posY, $ waterText, imagecolorallocate ($ ground_im, $ R, $ G, $ B ));
  143. }
  144. // The image after the watermark is generated
  145. @ Unlink ($ groundImage );
  146. Switch ($ ground_info [2]) // Obtain the background image format
  147. {
  148. Case 1: imagegif ($ ground_im, $ groundImage); break;
  149. Case 2: imagejpeg ($ ground_im, $ groundImage); break;
  150. Case 3: imagepng ($ ground_im, $ groundImage); break;
  151. Default: die ($ errorMsg );
  152. }
  153. // Release the memory
  154. If (isset ($ water_info) unset ($ water_info );
  155. If (isset ($ water_im) imagedestroy ($ water_im );
  156. Unset ($ ground_info );
  157. Imagedestroy ($ ground_im );
  158. }

  159. //------

  160. If (isset ($ _ FILES )&&! Empty ($ _ FILES ['userfile']) & $ _ FILES ['userfile'] ['size']> 0)
  161. {
  162. $ Uploadfile = "./". time (). "_". $ _ FILES ['userfile'] ['name'];
  163. If (copy ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile ))
  164. {
  165. Echo "OK
    ";
  166. // Text watermark
  167. ImageWaterMark ($ uploadfile, 0, "", "your water text", 9, "# EBEBEB ");
  168. // Image watermark
  169. // $ WaterImage = "./images/watermark.gif"; // watermark image path
  170. // ImageWaterMark ($ uploadfile, 9, $ waterImage );
  171. Echo "";
  172. }
  173. Else
  174. {
  175. Echo "Fail
    ";
  176. }
  177. }
  178. ?>

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.