Php image watermark code, php image adding text watermark and image watermark-php Tutorial

Source: Internet
Author: User
Tags imagecopy
Php image watermark code, php image adding text watermark and image watermark

  1. /**

  2. * Watermarks: Supports the transparency settings of text image watermarks and the transparency of the watermark image background.
  3. * Usage: (php text watermark effect)
  4. * $ Obj = new WaterMask ($ imgFileName); // instantiate an object
  5. * $ Obj-> $ waterType = 1; // type: 0 indicates a text watermark, and 1 indicates an image watermark.
  6. * $ Obj-> $ transparent = 45; // watermark transparency
  7. * $ Obj-> $ waterStr = 'bbs .it-home.org '; // watermark text
  8. * $ Obj-> $ fontSize = 16; // text font size
  9. * $ Obj-> $ fontColor = array (255, 0255); // watermark text color (RGB)
  10. * $ Obj-> $ fontFile = 'ahgbold. ttf'; // font file
  11. * $ Obj-> output (); // The output watermark image file overwrites the input image file.
  12. */
  13. Class WaterMask {
  14. Public $ waterType = 1; // watermark type: 0 indicates a text watermark, and 1 indicates an image watermark.
  15. Public $ pos = 0; // Watermark Position
  16. Public $ transparent = 45; // watermark transparency
  17. Public $ waterStr = 'bbs .it-home.org '; // watermark text
  18. Public $ fontSize = 16; // text font size
  19. Public $ fontColor = array (255, 0, 255); // watermark text color (RGB)
  20. Public $ fontFile = 'ahgbold. ttf'; // font file
  21. Public $ waterImg = 'logo.png '; // watermark image
  22. Private $ srcImg = ''; // image to which a watermark is to be added
  23. Private $ im = ''; // Image handle
  24. Private $ water_im = ''; // watermark image handle
  25. Private $ srcImg_info = ''; // picture information
  26. Private $ waterImg_info = ''; // watermark image information
  27. Private $ str_w = ''; // watermark text width
  28. Private $ str_h = ''; // watermark text height
  29. Private $ x = ''; // watermark X coordinate
  30. Private $ y = ''; // y coordinate of the watermark

  31. Function _ construct ($ img) {// destructor

  32. $ This-> srcImg = file_exists ($ img )? $ Img: die ('"'. $ img. '" source file does not exist! ');
  33. }
  34. Private function imginfo () {// obtain the information of the image to which the watermark is to be added and load the image.
  35. $ This-> srcImg_info = getimagesize ($ this-> srcImg );
  36. Switch ($ this-> srcImg_info [2]) {
  37. Case 3:
  38. $ This-> im = imagecreatefrompng ($ this-> srcImg );
  39. Break 1;
  40. Case 2:
  41. $ This-> im = imagecreatefromjpeg ($ this-> srcImg );
  42. Break 1;
  43. Case 1:
  44. $ This-> im = imagecreatefromgif ($ this-> srcImg );
  45. Break 1;
  46. Default:
  47. Die ('original image ('. $ this-> srcImg.') format is incorrect. only PNG, JPEG, and GIF images are supported. ');
  48. }
  49. }
  50. Private function waterimginfo () {// Obtain the watermark image information and load the image.
  51. $ This-> waterImg_info = getimagesize ($ this-> waterImg );
  52. Switch ($ this-> waterImg_info [2]) {
  53. Case 3:
  54. $ This-> water_im = imagecreatefrompng ($ this-> waterImg );
  55. Break 1;
  56. Case 2:
  57. $ This-> water_im = imagecreatefromjpeg ($ this-> waterImg );
  58. Break 1;
  59. Case 1:
  60. $ This-> water_im = imagecreatefromgif ($ this-> waterImg );
  61. Break 1;
  62. Default:
  63. Die ('watermark image ('. $ this-> srcImg.') format is incorrect. only PNG, JPEG, and GIF images are supported. ');
  64. }
  65. }
  66. Private function waterpos () {// watermark position algorithm
  67. Switch ($ this-> pos ){
  68. Case 0: // random position
  69. $ This-> x = rand (0, $ this-> srcImg_info [0]-$ this-> waterImg_info [0]);
  70. $ This-> y = rand (0, $ this-> srcImg_info [1]-$ this-> waterImg_info [1]);
  71. Break 1;
  72. Case 1: // upper left
  73. $ This-> x = 0;
  74. $ This-> y = 0;
  75. Break 1;
  76. Case 2: // upper
  77. $ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
  78. $ This-> y = 0;
  79. Break 1;
  80. Case 3: // upper right
  81. $ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
  82. $ This-> y = 0;
  83. Break 1;
  84. Case 4: // center left
  85. $ This-> x = 0;
  86. $ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
  87. Break 1;
  88. Case 5: // Medium
  89. $ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
  90. $ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
  91. Break 1;
  92. Case 6: // right
  93. $ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
  94. $ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
  95. Break 1;
  96. Case 7: // bottom left
  97. $ This-> x = 0;
  98. $ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
  99. Break 1;
  100. Case 8: // bottom
  101. $ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
  102. $ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
  103. Break 1;
  104. Default: // bottom right
  105. $ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
  106. $ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
  107. Break 1;
  108. }
  109. }
  110. Private function waterimg (){
  111. If ($ this-> srcImg_info [0] <= $ this-> waterImg_info [0] | $ this-> srcImg_info [1] <= $ this-> waterImg_info [1]) {
  112. Die ('watermark is larger than the source image! ');
  113. }
  114. $ This-> waterpos ();
  115. $ Cut = imagecreatetruecolor ($ this-> waterImg_info [0], $ this-> waterImg_info [1]);
  116. Imagecopy ($ cut, $ this-> im, 0, 0, $ this-> x, $ this-> y, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
  117. $ Pct = $ this-> transparent;
  118. Imagecopy ($ cut, $ this-> water_im, 0, 0, 0, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
  119. Imagecopymerge ($ this-> im, $ cut, $ this-> x, $ this-> y, 0, 0, $ this-> waterImg_info [0], $ this-> waterImg_info [1], $ pct );
  120. }
  121. Private function waterstr (){
  122. $ Rect = imagettfbbox ($ this-> fontSize, 0, $ this-> fontFile, $ this-> waterStr );
  123. $ W = abs ($ rect [2]-$ rect [6]);
  124. $ H = abs ($ rect [3]-$ rect [7]);
  125. $ FontHeight = $ this-> fontSize;
  126. $ This-> water_im = imagecreatetruecolor ($ w, $ h );
  127. Imagealphablending ($ this-> water_im, false );
  128. Imagesavealpha ($ this-> water_im, true );
  129. $ White_alpha = imagecolorallocatealpha ($ this-> water_im, 255,255,255,127 );
  130. Imagefill ($ this-> water_im, 0, 0, $ white_alpha );
  131. $ Color = imagecolorallocate ($ this-> water_im, $ this-> fontColor [0], $ this-> fontColor [1], $ this-> fontColor [2]);
  132. Imagettftext ($ this-> water_im, $ this-> fontSize, 0, 0, $ this-> fontSize, $ color, $ this-> fontFile, $ this-> waterStr );
  133. $ This-> waterImg_info = array (0 => $ w, 1 => $ h );
  134. $ This-> waterimg ();
  135. }
  136. Function output (){
  137. $ This-> imginfo ();
  138. If ($ this-> waterType = 0 ){
  139. $ This-> waterstr ();
  140. } Else {
  141. $ This-> waterimginfo ();
  142. $ This-> waterimg ();
  143. }
  144. Switch ($ this-> srcImg_info [2]) {
  145. Case 3:
  146. Imagepng ($ this-> im, $ this-> srcImg );
  147. Break 1;
  148. Case 2:
  149. Imagejpeg ($ this-> im, $ this-> srcImg );
  150. Break 1;
  151. Case 1:
  152. Imagegif ($ this-> im, $ this-> srcImg );
  153. Break 1;
  154. Default:
  155. Die ('failed to add watermark! ');
  156. Break;
  157. }
  158. Imagedestroy ($ this-> im );
  159. Imagedestroy ($ this-> water_im );
  160. }
  161. }
  162. ?>

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.