PHP Image Watermark Class Code, PHP images add text watermark and image watermark

Source: Internet
Author: User
Tags imagecopy
  1. /**

  2. * Add watermark class, support the text image watermark Transparency settings, watermark picture background transparent.
  3. * Use: (PHP text watermark effect)
  4. * $obj = new Watermask ($imgFileName); Instantiating an Object
  5. * $obj $waterType = 1; Type: 0 for text watermark, 1 for picture 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 files
  11. * $obj->output (); Output watermark image file overlay into the input picture file
  12. */
  13. Class watermask{
  14. public $waterType = 1; Watermark Type: 0 for text watermark, 1 for picture watermark
  15. Public $pos = 0; Watermark Location
  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 files
  21. Public $waterImg = ' logo.png '; Watermark Picture
  22. Private $srcImg = '; Picture to add a watermark
  23. Private $im = '; Picture handle
  24. Private $water _im = "; Watermark Picture Handle
  25. Private $SRCIMG _info = "; Picture information
  26. Private $WATERIMG _info = "; Watermark Picture Information
  27. Private $str _w = "; Watermark Text Width
  28. Private $str _h = "; Watermark Text Height
  29. Private $x = '; Watermark X Coordinate
  30. Private $y = '; Watermark y-coordinate

  31. function __construct ($img) {//destructor

  32. $this->srcimg = file_exists ($img)? $img: Die (' ". $img. '" Source file does not exist!) ');
  33. }
  34. Private Function Imginfo () {//Get information about the picture that needs to be added to the watermark and load the picture.
  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 picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. ');
  48. }
  49. }
  50. Private Function Waterimginfo () {//Get the information of the watermark picture and load the picture.
  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 picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF 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://Kaminaka
  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://Middle Left
  85. $this->x = 0;
  86. $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
  87. Break 1;
  88. Case 5://Middle
  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://Middle 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://Lower Left
  97. $this->x = 0;
  98. $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
  99. Break 1;
  100. Case 8://Down
  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://Down 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 (' The watermark is bigger than the original! ');
  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,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 (' Add watermark failed! ');
  156. Break
  157. }
  158. Imagedestroy ($this->im);
  159. Imagedestroy ($this->water_im);
  160. }
  161. }
  162. ?>

Copy Code
  • 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.