PHP image Watermark class, PHP add Watermark function code to the picture

Source: Internet
Author: User
  1. /*

  2. * Function: PHP image watermark (watermark supports picture or text)
  3. Parameters
  4. * $groundimage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
  5. * $waterPos watermark position, there are 10 kinds of states, 0 is a random position;
  6. * 1 for the top left, 2 for the top center, 3 for the top right;
  7. * 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
  8. * 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
  9. * $waterImage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
  10. * $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
  11. * $textFont Text size, value 1, 2, 3, 4 or 5, default is 5;
  12. * $textColor text color, value is hexadecimal color value, default is #ff0000 (red);
  13. *
  14. * Note: Support GD 2.0,support FreeType, gif Read, GIF Create, JPG, PNG
  15. * $waterImage and $waterText best not to use at the same time, choose one of them, priority to use the $waterImage.
  16. * When the $waterimage is valid, the parameters $waterstring, $stringFont, $stringColor are not effective.
  17. * The watermark image has the same file name as the $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 = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, PNG format. ”;
  24. Read Watermark File
  25. if (!emptyempty ($waterImage) && file_exists ($waterImage))
  26. {
  27. $isWaterImage = TRUE;
  28. $water _info = getimagesize ($waterImage);
  29. $water _w = $water _info[0];//To obtain the width of the watermark picture
  30. $water _h = $water _info[1];//To obtain a high watermark image
  31. Switch ($water _info[2])//Get the format of the watermark picture
  32. {
  33. Case 1: $water _im = Imagecreatefromgif ($waterImage);
  34. Case 2: $water _im = Imagecreatefromjpeg ($waterImage);
  35. Case 3: $water _im = Imagecreatefrompng ($waterImage);
  36. Default:die ($FORMATMSG);
  37. }
  38. }
  39. Read background picture
  40. if (!emptyempty ($groundImage) && file_exists ($groundImage))
  41. {
  42. $ground _info = getimagesize ($groundImage);
  43. $ground _w = $ground _info[0];//Get the width of the background image
  44. $ground _h = $ground _info[1];//get a high background picture
  45. Switch ($ground _info[2])//Get the format of the background picture
  46. {
  47. Case 1: $ground _im = Imagecreatefromgif ($groundImage);
  48. Case 2: $ground _im = Imagecreatefromjpeg ($groundImage);
  49. Case 3: $ground _im = Imagecreatefrompng ($groundImage);
  50. Default:die ($FORMATMSG);
  51. }
  52. }
  53. Else
  54. {
  55. Die ("The picture that needs watermark does not exist!") ”);
  56. }
  57. Watermark Location
  58. if ($isWaterImage)//Picture watermark
  59. {
  60. $w = $water _w;
  61. $h = $water _h;
  62. $label = "Picture";
  63. }
  64. else//text watermark
  65. {
  66. $temp = Imagettfbbox (Ceil ($textFont), 0, "./cour.ttf", $waterText);//Get the range of text that uses TrueType fonts
  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 picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ”;
  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 for top left
  84. $posX = 0;
  85. $posY = 0;
  86. Break
  87. Case 2://2 centered on the top
  88. $posX = ($ground _w-$w)/2;
  89. $posY = 0;
  90. Break
  91. Case 3://3 for top right
  92. $posX = $ground _w-$w;
  93. $posY = 0;
  94. Break
  95. Case 4://4 for the middle left
  96. $posX = 0;
  97. $posY = ($ground _h-$h)/2;
  98. Break
  99. Case 5://5 centered in the middle
  100. $posX = ($ground _w-$w)/2;
  101. $posY = ($ground _h-$h)/2;
  102. Break
  103. Case 6://6 for the middle right
  104. $posX = $ground _w-$w;
  105. $posY = ($ground _h-$h)/2;
  106. Break
  107. Case 7://7 for Bottom left
  108. $posX = 0;
  109. $posY = $ground _h-$h;
  110. Break
  111. Case 8://8 centered at bottom
  112. $posX = ($ground _w-$w)/2;
  113. $posY = $ground _h-$h;
  114. Break
  115. Case 9://9 for bottom 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. To set the color blending mode of an image
  125. Imagealphablending ($ground _im, true);
  126. if ($isWaterImage)//Picture watermark
  127. {
  128. Imagecopy ($ground _im, $water _im, $posX, $posY, 0, 0, $water _w, $water _h);//copy watermark to target file
  129. }
  130. else//text watermark
  131. {
  132. if (!emptyempty ($textColor) && (strlen ($textColor) ==7))
  133. {
  134. $R = Hexdec (substr ($textColor,));
  135. $G = Hexdec (substr ($textColor, 3,2));
  136. $B = Hexdec (substr ($textColor, 5));
  137. }
  138. Else
  139. {
  140. Die ("Watermark text color format is not correct! ”);
  141. }
  142. Imagestring ($ground _im, $textFont, $posX, $posY, $waterText, Imagecolorallocate ($ground _im, $R, $G, $B));
  143. }
  144. Create a picture after a watermark
  145. @unlink ($groundImage);
  146. Switch ($ground _info[2])//Get the format of the background picture
  147. {
  148. Case 1:imagegif ($ground _im, $groundImage);
  149. Case 2:imagejpeg ($ground _im, $groundImage);
  150. Case 3:imagepng ($ground _im, $groundImage);
  151. Default:die ($ERRORMSG);
  152. }
  153. Freeing 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. -bbs.it-home.org
  160. $id =$_request[' id '];
  161. $num = count ($_files[' userfile ' [' name ']);
  162. Print_r ($_files[' userfile ');
  163. Print_r ($_files[' userfile ' [' name ']);
  164. Echo $num;
  165. echo "
    ”;
  166. if (Isset ($id)) {
  167. for ($i =0; $i < $id; $i + +) {
  168. if (Isset ($_files) &&!emptyempty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
  169. {
  170. $uploadfile = "./". Time (). " _ ". $_files[' UserFile '][name][$i];
  171. echo "
    ”;
  172. Echo $uploadfile;
  173. if (copy ($_files[' userfile ' [' tmp_name '] [$i], $uploadfile))
  174. {
  175. echo "OK
    ”;
  176. Text watermark
  177. Imagewatermark ($uploadfile, 5, "", "HTTP://www.lvye.info", 5, "#cccccc");
  178. Image watermark
  179. $waterImage = "logo_ok1.gif";//Watermark Picture Path
  180. Imagewatermark ($uploadfile, 9, $waterImage);
  181. echo "";
  182. }
  183. Else
  184. {
  185. echo "Fail
    ”;
  186. }
  187. }
  188. }
  189. }
  190. ?>
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.