PHP image watermark class supports watermark transparency and background transparency

WBOY
Release: 2016-07-25 08:51:49
Original
1398 people have browsed it
  1. /**

  2. * Add watermark class, support transparency setting of text and picture watermarks, and transparent background of watermark pictures.
  3. * Date: 2011-09-27
  4. * Author: bbs.it-home.org
  5. * Usage:
  6. * $obj = new WaterMask($imgFileName); //Instantiate the object
  7. * $obj->$waterType = 1; //Type: 0 is text watermark, 1 is picture watermark
  8. * $obj->$transparent = 45; //Watermark transparency
  9. * $obj->$waterStr = 'bbs.it-home.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 file
  13. * $obj->output(); //The output watermark image file overwrites the input image file
  14. */
  15. class WaterMask{
  16. public $waterType = 1; //Watermark type: 0 is text watermark, 1 is picture watermark
  17. public $pos = 0; //Watermark position
  18. public $transparent = 45; //Watermark transparency
  19. public $waterStr = 'bbs.it-home.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 file
  23. public $waterImg = 'logo.png'; //Watermark Picture
  24. private $srcImg = ''; //Picture that needs to be watermarked
  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 $img) ? $img : die('"'.$img.'" The source file does not exist!');
  32. }
  33. private function imginfo() { //Get the information of the image that needs to be watermarked and load the image .
  34. $this->srcImg_info = getimagesize($this->srcImg);
  35. switch ($this->srcImg_info[2]) {
  36. case 3:
  37. $this->im = imagecreatefrompng($this-> ;srcImg);
  38. break 1;
  39. case 2:
  40. $this->im = imagecreatefromjpeg($this->srcImg);
  41. break 1;
  42. case 1:
  43. $this->im = imagecreatefromgif($this ->srcImg);
  44. break 1;
  45. default:
  46. die('The format of the original image ('.$this->srcImg.') is wrong, only PNG, JPEG, and GIF are supported.');
  47. }
  48. }
  49. private function waterimginfo() { //Get the information of the watermark image and load the image.
  50. $this->waterImg_info = getimagesize($this->waterImg);
  51. switch ($this->waterImg_info[2]) {
  52. case 3:
  53. $this->water_im = imagecreatefrompng($this-> ;waterImg);
  54. break 1;
  55. case 2:
  56. $this->water_im = imagecreatefromjpeg($this->waterImg);
  57. break 1;
  58. case 1:
  59. $this->water_im = imagecreatefromgif($this ->waterImg);
  60. break 1;
  61. default:
  62. die('The watermark image ('.$this->srcImg.') is in the wrong format, only supports PNG, JPEG, and GIF.');
  63. }
  64. }
  65. private function waterpos() { //水印位置算法
  66. switch ($this->pos) {
  67. case 0: //随机位置
  68. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  69. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  70. break 1;
  71. case 1: //上左
  72. $this->x = 0;
  73. $this->y = 0;
  74. break 1;
  75. case 2: //上中
  76. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  77. $this->y = 0;
  78. break 1;
  79. case 3: //上右
  80. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  81. $this->y = 0;
  82. break 1;
  83. case 4: //中左
  84. $this->x = 0;
  85. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  86. break 1;
  87. case 5: //中中
  88. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  89. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  90. break 1;
  91. case 6: //中右
  92. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  93. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  94. break 1;
  95. case 7: //下左
  96. $this->x = 0;
  97. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  98. break 1;
  99. case 8: //下中
  100. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  101. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  102. break 1;
  103. default: //下右
  104. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  105. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  106. break 1;
  107. }
  108. }
  109. private function waterimg() {
  110. if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
  111. die('水印比原图大!');
  112. }
  113. $this->waterpos();
  114. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  115. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  116. $pct = $this->transparent;
  117. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  118. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  119. }
  120. private function waterstr() {
  121. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  122. $w = abs($rect[2]-$rect[6]);
  123. $h = abs($rect[3]-$rect[7]);
  124. $fontHeight = $this->fontSize;
  125. $this->water_im = imagecreatetruecolor($w, $h);
  126. imagealphablending($this->water_im,false);
  127. imagesavealpha($this->water_im,true);
  128. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  129. imagefill($this->water_im,0,0,$white_alpha);
  130. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  131. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  132. $this->waterImg_info = array(0=>$w,1=>$h);
  133. $this->waterimg();
  134. }
  135. function output() {
  136. $this->imginfo();
  137. if ($this->waterType == 0) {
  138. $this->waterstr();
  139. }else {
  140. $this->waterimginfo();
  141. $this->waterimg();
  142. }
  143. switch ($this->srcImg_info[2]) {
  144. case 3:
  145. imagepng($this->im,$this->srcImg);
  146. break 1;
  147. case 2:
  148. imagejpeg($this->im,$this->srcImg);
  149. break 1;
  150. case 1:
  151. imagegif($this->im,$this->srcImg);
  152. break 1;
  153. default:
  154. die('添加水印失败!');
  155. break;
  156. }
  157. imagedestroy($this->im);
  158. imagedestroy($this->water_im);
  159. }
  160. }
  161. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template