支持文字水印、图片水印支持水印的位置随机或固定(九宫格)水印透明度设置(图片水印和文字水印都支持)文字 水印的字体、颜色、大小设置图片水印的背景透明
  1. * 加水印类,支持文字图片水印的透明度设置、水印图片背景透明。
  2. * 日期:2011-09-27
  3. * 作者:www.itwhy.org
  4. * 使用:
  5. * $obj = new WaterMask($imgFileName); //实例化对象
  6. * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印
  7. * $obj->$transparent = 45; //水印透明度
  8. * $obj->$waterStr = 'www.itwhy.org'; //水印文字
  9. * $obj->$fontSize = 16; //文字字体大小
  10. * $obj->$fontColor = array(255,0255); //水印文字颜色(RGB)
  11. * $obj->$fontFile = = 'AHGBold.ttf'; //字体文件
  12. * $obj->output(); //输出水印图片文件覆盖到输入的图片文件
  13. */
  14. class WaterMask{
  15. public $waterType = 1; //水印类型:0为文字水印、1为图片水印
  16. public $pos = 0; //水印位置
  17. public $transparent = 45; //水印透明度
  18. public $waterStr = 'www.itwhy.org'; //水印文字
  19. public $fontSize = 16; //文字字体大小
  20. public $fontColor = array(255,0,255); //水印文字颜色(RGB)
  21. public $fontFile = 'AHGBold.ttf'; //字体文件
  22. public $waterImg = 'logo.png'; //水印图片
  23. private $srcImg = ''; //需要添加水印的图片
  24. private $im = ''; //图片句柄
  25. private $water_im = ''; //水印图片句柄
  26. private $srcImg_info = ''; //图片信息
  27. private $waterImg_info = ''; //水印图片信息
  28. private $str_w = ''; //水印文字宽度
  29. private $str_h = ''; //水印文字高度
  30. private $x = ''; //水印X坐标
  31. private $y = ''; //水印y坐标
  32. function __construct($img) { //析构函数
  33. $this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
  34. }
  35. private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。
  36. $this->srcImg_info = getimagesize($this->srcImg);
  37. switch ($this->srcImg_info[2]) {
  38. case 3:
  39. $this->im = imagecreatefrompng($this->srcImg);
  40. break 1;
  41. case 2:
  42. $this->im = imagecreatefromjpeg($this->srcImg);
  43. break 1;
  44. case 1:
  45. $this->im = imagecreatefromgif($this->srcImg);
  46. break 1;
  47. default:
  48. die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  49. }
  50. }
  51. private function waterimginfo() { //获取水印图片的信息,并载入图片。
  52. $this->waterImg_info = getimagesize($this->waterImg);
  53. switch ($this->waterImg_info[2]) {
  54. case 3:
  55. $this->water_im = imagecreatefrompng($this->waterImg);
  56. break 1;
  57. case 2:
  58. $this->water_im = imagecreatefromjpeg($this->waterImg);
  59. break 1;
  60. case 1:
  61. $this->water_im = imagecreatefromgif($this->waterImg);
  62. break 1;
  63. default:
  64. die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  65. }
  66. }
  67. private function waterpos() { //水印位置算法
  68. switch ($this->pos) {
  69. case 0: //随机位置
  70. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  71. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  72. break 1;
  73. case 1: //上左
  74. $this->x = 0;
  75. $this->y = 0;
  76. break 1;
  77. case 2: //上中
  78. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  79. $this->y = 0;
  80. break 1;
  81. case 3: //上右
  82. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  83. $this->y = 0;
  84. break 1;
  85. case 4: //中左
  86. $this->x = 0;
  87. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  88. break 1;
  89. case 5: //中中
  90. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  91. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  92. break 1;
  93. case 6: //中右
  94. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  95. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  96. break 1;
  97. case 7: //下左
  98. $this->x = 0;
  99. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  100. break 1;
  101. case 8: //下中
  102. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  103. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  104. break 1;
  105. default: //下右
  106. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  107. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  108. break 1;
  109. }
  110. }
  111. private function waterimg() {
  112. if ($this->srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){
  113. die('水印比原图大!');
  114. }
  115. $this->waterpos();
  116. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  117. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  118. $pct = $this->transparent;
  119. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  120. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  121. }
  122. private function waterstr() {
  123. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  124. $w = abs($rect[2]-$rect[6]);
  125. $h = abs($rect[3]-$rect[7]);
  126. $fontHeight = $this->fontSize;
  127. $this->water_im = imagecreatetruecolor($w, $h);
  128. imagealphablending($this->water_im,false);
  129. imagesavealpha($this->water_im,true);
  130. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  131. imagefill($this->water_im,0,0,$white_alpha);
  132. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  133. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  134. $this->waterImg_info = array(0=>$w,1=>$h);
  135. $this->waterimg();
  136. }
  137. function output() {
  138. $this->imginfo();
  139. if ($this->waterType == 0) {
  140. $this->waterstr();
  141. }else {
  142. $this->waterimginfo();
  143. $this->waterimg();
  144. }
  145. switch ($this->srcImg_info[2]) {
  146. case 3:
  147. imagepng($this->im,$this->srcImg);
  148. break 1;
  149. case 2:
  150. imagejpeg($this->im,$this->srcImg);
  151. break 1;
  152. case 1:
  153. imagegif($this->im,$this->srcImg);
  154. break 1;
  155. default:
  156. die('添加水印失败!');
  157. break;
  158. }
  159. imagedestroy($this->im);
  160. imagedestroy($this->water_im);
  161. }
  162. }
  163. ?>
复制代码

PHP


09-14 21:16