PHP image automatically add watermark code, PHP image watermark function code

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. * For the top of the left, 2 for the top center, 3 for the top right;
  7. In 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 (!empty ($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 (!empty ($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 *2.5), 0, "./cour.ttf", $watertext);//Gets 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 (!empty ($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. //--------
  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, "", "http://blog.csdn.net/longware/", 5, "#ff0000");
  168. Image watermark
  169. $waterimage = "./hanweb_shuiyin.gif";//Watermark Picture Path
  170. Imagewatermark ($uploadfile, 0, $waterimage);
  171. echo "";
  172. }
  173. Else
  174. {
  175. echo "fail
    ";
  176. }
  177. }
  178. ?>
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.