PHP class to add a watermark to a picture

Source: Internet
Author: User
Tags imagecopy
Support text watermark, image watermark support watermark location random or fixed (nine Gongge) Watermark Transparency settings (image watermark and text watermark are supported) text watermark font, color, size set picture watermark background Transparent
  1. /**
  2. * Add watermark class, support the text image watermark Transparency settings, watermark picture background transparent.
  3. * Date: 2011-09-27
  4. * Author: www.itwhy.org
  5. Use
  6. * $obj = new Watermask ($imgFileName); Instantiating an Object
  7. * $obj $waterType = 1; Type: 0 for text watermark, 1 for picture watermark
  8. * $obj $transparent = 45; Watermark Transparency
  9. * $obj $waterStr = ' www.itwhy.org '; Watermark Text
  10. * $obj $fontSize = 16; Text font size
  11. * $obj $fontColor = array (255,0255); Watermark text color (RGB)
  12. * $obj $fontFile = = ' Ahgbold.ttf '; Font files
  13. * $obj->output (); Output watermark image file overlay into the input picture file
  14. */
  15. Class watermask{
  16. public $waterType = 1; Watermark Type: 0 for text watermark, 1 for picture watermark
  17. Public $pos = 0; Watermark Location
  18. Public $transparent = 45; Watermark Transparency
  19. Public $waterStr = ' www.itwhy.org '; Watermark Text
  20. public $fontSize = 16; Text font size
  21. Public $fontColor = Array (255,0,255); Watermark text color (RGB)
  22. Public $fontFile = ' Ahgbold.ttf '; Font files
  23. Public $waterImg = ' logo.png '; Watermark Picture
  24. Private $srcImg = '; Picture to add a watermark
  25. Private $im = '; Picture handle
  26. Private $water _im = "; Watermark Picture Handle
  27. Private $SRCIMG _info = "; Picture information
  28. Private $WATERIMG _info = "; Watermark Picture Information
  29. Private $str _w = "; Watermark Text Width
  30. Private $str _h = "; Watermark Text Height
  31. Private $x = '; Watermark X Coordinate
  32. Private $y = '; Watermark y-coordinate
  33. function __construct ($img) {//destructor
  34. $this->srcimg = file_exists ($img)? $img: Die (' ". $img. '" Source file does not exist!) ');
  35. }
  36. Private Function Imginfo () {//Get information about the picture that needs to be added to the watermark and load the picture.
  37. $this->srcimg_info = getimagesize ($this->srcimg);
  38. Switch ($this->srcimg_info[2]) {
  39. Case 3:
  40. $this->im = imagecreatefrompng ($this->srcimg);
  41. Break 1;
  42. Case 2:
  43. $this->im = imagecreatefromjpeg ($this->srcimg);
  44. Break 1;
  45. Case 1:
  46. $this->im = imagecreatefromgif ($this->srcimg);
  47. Break 1;
  48. Default
  49. Die (' Original picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. ');
  50. }
  51. }
  52. Private Function Waterimginfo () {//Get the information of the watermark picture and load the picture.
  53. $this->waterimg_info = getimagesize ($this->waterimg);
  54. Switch ($this->waterimg_info[2]) {
  55. Case 3:
  56. $this->water_im = imagecreatefrompng ($this->waterimg);
  57. Break 1;
  58. Case 2:
  59. $this->water_im = imagecreatefromjpeg ($this->waterimg);
  60. Break 1;
  61. Case 1:
  62. $this->water_im = imagecreatefromgif ($this->waterimg);
  63. Break 1;
  64. Default
  65. Die (' Watermark picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. ');
  66. }
  67. }
  68. Private Function Waterpos () {//Watermark position algorithm
  69. Switch ($this->pos) {
  70. Case 0://Random position
  71. $this->x = rand (0, $this->srcimg_info[0]-$this->waterimg_info[0]);
  72. $this->y = rand (0, $this->srcimg_info[1]-$this->waterimg_info[1]);
  73. Break 1;
  74. Case 1://upper Left
  75. $this->x = 0;
  76. $this->y = 0;
  77. Break 1;
  78. Case 2://Kaminaka
  79. $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
  80. $this->y = 0;
  81. Break 1;
  82. Case 3://upper Right
  83. $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
  84. $this->y = 0;
  85. Break 1;
  86. Case 4://Middle Left
  87. $this->x = 0;
  88. $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
  89. Break 1;
  90. Case 5://Middle
  91. $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
  92. $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
  93. Break 1;
  94. Case 6://Middle Right
  95. $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
  96. $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
  97. Break 1;
  98. Case 7://Lower Left
  99. $this->x = 0;
  100. $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
  101. Break 1;
  102. Case 8://Down
  103. $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
  104. $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
  105. Break 1;
  106. Default://Down Right
  107. $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
  108. $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
  109. Break 1;
  110. }
  111. }
  112. Private Function waterimg () {
  113. if ($this->srcimg_info[0] <= $this->waterimg_info[0] | | $this->srcimg_info[1] <= $this->waterimg_ Info[1]) {
  114. Die (' The watermark is bigger than the original! ');
  115. }
  116. $this->waterpos ();
  117. $cut = Imagecreatetruecolor ($this->waterimg_info[0], $this->waterimg_info[1]);
  118. Imagecopy ($cut, $this->im,0,0, $this->x, $this->y, $this->waterimg_info[0], $this->waterimg_info[1]) ;
  119. $pct = $this->transparent;
  120. Imagecopy ($cut, $this->water_im,0,0,0,0, $this->waterimg_info[0], $this->waterimg_info[1]);
  121. Imagecopymerge ($this->im, $cut, $this->x, $this->y,0,0, $this->waterimg_info[0], $this->waterimg_ INFO[1], $pct);
  122. }
  123. Private Function Waterstr () {
  124. $rect = Imagettfbbox ($this->fontsize,0, $this->fontfile, $this->waterstr);
  125. $W = ABS ($rect [2]-$rect [6]);
  126. $h = ABS ($rect [3]-$rect [7]);
  127. $fontHeight = $this->fontsize;
  128. $this->water_im = Imagecreatetruecolor ($w, $h);
  129. Imagealphablending ($this->water_im,false);
  130. Imagesavealpha ($this->water_im,true);
  131. $white _alpha = Imagecolorallocatealpha ($this->water_im,255,255,255,127);
  132. Imagefill ($this->water_im,0,0, $white _alpha);
  133. $color = Imagecolorallocate ($this->water_im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor [2]);
  134. Imagettftext ($this->water_im, $this->fontsize,0,0, $this->fontsize, $color, $this->fontfile, $this WATERSTR);
  135. $this->waterimg_info = Array (0=> $w,1=> $h);
  136. $this->waterimg ();
  137. }
  138. function output () {
  139. $this->imginfo ();
  140. if ($this->watertype = = 0) {
  141. $this->waterstr ();
  142. }else {
  143. $this->waterimginfo ();
  144. $this->waterimg ();
  145. }
  146. Switch ($this->srcimg_info[2]) {
  147. Case 3:
  148. Imagepng ($this->im, $this->srcimg);
  149. Break 1;
  150. Case 2:
  151. Imagejpeg ($this->im, $this->srcimg);
  152. Break 1;
  153. Case 1:
  154. Imagegif ($this->im, $this->srcimg);
  155. Break 1;
  156. Default
  157. Die (' Add watermark failed! ');
  158. Break
  159. }
  160. Imagedestroy ($this->im);
  161. Imagedestroy ($this->water_im);
  162. }
  163. }
  164. ?>
Copy Code
Php
  • 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.