当前位置:   article > 正文

PHP的生成图片或文字水印的类_php 水印、

php 水印、
  1. <?php
  2. /***********************************************************
  3. 类名:ImageWatermark
  4. 功能:用于生成图片或文字水印
  5. ************************************************************
  6. 合成水印:
  7. 1、图像水印appendImageMark(暂不可旋转)
  8. 2、文字水印appendTextMark(汉字水印需要设置汉字字体)(可旋转)
  9. 输出水印图像:write($filename=null)
  10. 1、输出到文件:指定$filename参数为输出的文件名。
  11. 2、输出到浏览器:不指定输出文件名,则输出到浏览器.
  12. 指定水印位置:
  13. 1、指定位置类型$markPosType:(default-0)
  14. 1-top left 2-top center 3-top right
  15. 4-middle left 5-middle center 6-middle right
  16. 7-bottom left 8-bottom center 9-bottom right
  17. 0-random
  18. 2、设置具体位置setMarkPos($x,$y),若指定具体位置,则上面的位置类型无效。
  19. ************************************************************
  20. */
  21. class ImageWatermark{
  22. public $markPosType = 0; //水印位置,缺省为随机位置输出水印
  23. public $fontFile = 'arial.ttf'; //字体文件名
  24. public $color = '#CCCCCC'; //水印字体的颜色
  25. public $fontSize = 12; //水印字体大小
  26. public $angle = 0; //水印文字旋转的角度
  27. private $markPos = array();
  28. private $markImageFile = null, $destImageFile = null;
  29. private $mark_res = null, $mark_width = 0, $mark_height = 0, $mark_type = null;
  30. private $dest_res = null, $dest_width = 0, $dest_height = 0, $dest_type = null;
  31. //用目标图片作为构造函数的参数
  32. public function __construct($destImage){
  33. if(!file_exists($destImage)) return false;
  34. $this->destImageFile=$destImage;
  35. //获取图片大小、类型
  36. $imageInfo = getimagesize($this->destImageFile);
  37. $this->dest_width = $imageInfo[0];$this->dest_height = $imageInfo[1];$this->dest_type = $imageInfo[2];
  38. //得到图片资源句柄
  39. $this->dest_res = $this->getImageResource($this->destImageFile,$this->dest_type);
  40. }
  41. public function __destruct(){
  42. imagedestroy($this->dest_res);
  43. }
  44. //添加文字水印
  45. public function appendTextMark($markText){
  46. if($markText==null) return false;
  47. //计算水印文本的大小
  48. $box = imagettfbbox($this->fontSize,$this->angle,$this->fontFile,$markText);
  49. $this->mark_width = $box[2]-$box[6];
  50. $this->mark_height = $box[3]-$box[7];
  51. //计算水印位置
  52. $pos = ($this->markPos!=null)?$this->markPos:$this->getMarkPosition($this->markPosType);
  53. $pos[1]+=$this->mark_height;
  54. //将文字打印到图片上
  55. $RGB=$this->colorHexRgb($this->color);
  56. $imageColor=imagecolorallocate($this->dest_res,$RGB[0],$RGB[1],$RGB[2]);
  57. imagettftext($this->dest_res,$this->fontSize,$this->angle,$pos[0],$pos[1],$imageColor,$this->fontFile,$markText);
  58. }
  59. //添加图片水印
  60. public function appendImageMark($markImage){
  61. if(!file_exists($markImage)) return false;
  62. $this->markImageFile=$markImage;
  63. //获取水印图片大小、类型
  64. $imageInfo = getimagesize($this->markImageFile);
  65. $this->mark_width = $imageInfo[0];$this->mark_height = $imageInfo[1];$this->mark_type = $imageInfo[2];
  66. //得到图片资源句柄
  67. $this->mark_res = $this->getImageResource($this->markImageFile,$this->mark_type);
  68. //计算水印位置
  69. $pos = ($this->markPos!=null)?$this->markPos:$this->getMarkPosition($this->markPosType);
  70. //设置图像混色模式
  71. imagealphablending($this->dest_res, true);
  72. //复制叠加图像
  73. imagecopy($this->dest_res,$this->mark_res,$pos[0],$pos[1],0,0,$this->mark_width,$this->mark_height);
  74. imagedestroy($this->mark_res);
  75. }
  76. //将叠加水印后的图片写入指定文件,若不定文件名,则输出到浏览器
  77. public function write($filename=null){
  78. $this->writeImage($this->dest_res,$filename,$this->dest_type);
  79. }
  80. //设置水印x,y坐标
  81. public function setMarkPos($x,$y){
  82. $this->markPos[0]=$x; $this->markPos[1]=$y;
  83. }
  84. //将十六进制的颜色值分解成RGB形式
  85. private function colorHexRgb($color){
  86. $color = preg_replace('/#/','',$color);
  87. $R=hexdec($color[0].$color[1]);
  88. $G=hexdec($color[2].$color[3]);
  89. $B=hexdec($color[4].$color[5]);
  90. return array($R,$G,$B);
  91. }
  92. //计算水印位置
  93. private function getMarkPosition($type=0){
  94. switch($type){
  95. case 0: $x = rand(0,$this->dest_width-$this->mark_width);
  96. $y = rand(0,$this->dest_height-$this->mark_height);
  97. break;//random
  98. case 1: $x = 0;
  99. $y = 0;
  100. break;//topleft
  101. case 2: $x = ($this->dest_width-$this->mark_width)/2;
  102. $y = 0;
  103. break; //topcenter
  104. case 3: $x = $this->dest_width-$this->mark_width;
  105. $y = 0;
  106. break;// topright
  107. case 4: $x = 0;
  108. $y = ($this->dest_height-$this->mark_height)/2;
  109. break;//middleleft
  110. case 5: $x = ($this->dest_width-$this->mark_width)/2;
  111. $y = ($this->dest_height-$this->mark_height)/2;
  112. break;//middlecenter
  113. case 6: $x = $this->dest_width-$this->mark_width;
  114. $y = ($this->dest_height-$this->mark_height)/2;
  115. break;//middleright
  116. case 7: $x = 0; $y = $this->dest_height-$this->mark_height;
  117. break;//bottomleft
  118. case 8: $x = ($this->dest_width-$this->mark_width)/2;
  119. $y = $this->dest_height-$this->mark_height;
  120. break;//bottomcenter
  121. case 9: $x = $this->dest_width-$this->mark_width;
  122. $y = $this->dest_height-$this->mark_height;
  123. break;//bottomright
  124. default:$x = rand(0,$this->dest_width-$this->mark_width);
  125. $y = rand(0,$this->dest_height-$this->mark_height);
  126. break;//random
  127. }
  128. return array($x,$y);
  129. }
  130. //从一个图像文件中取得图片资源标识符
  131. private function getImageResource($filename,$type=0){
  132. switch($type){
  133. case 1:return imagecreatefromgif($filename);break;
  134. case 2:return imagecreatefromjpeg($filename);break;
  135. case 3:return imagecreatefrompng($filename);break;
  136. // 以后可添加其它格式
  137. default:return null;
  138. }
  139. }
  140. //将图像写入文件或输出到浏览器
  141. private function writeImage($ImageRes,$filename=null,$type=0){
  142. switch($type) {
  143. case 1:imagegif($ImageRes,$filename);break;
  144. case 2:imagejpeg($ImageRes,$filename);break;
  145. case 3:imagepng($ImageRes,$filename);break;
  146. default:return null;
  147. }
  148. return true;
  149. }
  150. }
  151. //使用示例
  152. $markimg = new ImageWatermark('c_si.jpg');
  153. //$markimg->setMarkPos(100,200);//如何设置setMarkPos,则markPosType无效。
  154. $markimg->markPosType=5;
  155. $markimg->appendImageMark('mark.png');
  156. $markimg->fontFile='STCAIYUN.TTF';
  157. $markimg->color='#FFFFFF';
  158. $markimg->fontSize=24;
  159. $markimg->angle=45;//设置角度时,注意水印可能旋转出目标图片之外。
  160. $markimg->appendTextMark('汉字水印');
  161. $markimg->write();
  162. $markimg=null;
  163. ?>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/325984
推荐阅读
相关标签
  

闽ICP备14008679号