查看: 695|回复: 0

[PHP实例] PHP水印类,支持添加图片、文字、填充颜色区域的实现

发表于 2017-11-22 08:00:13

*自己整理的一个水印类*

支持添加图片、文字、填充颜色区域

  1. <?php
  2. /**
  3. * 图片加水印类,支持文字水印、透明度设置、自定义水印位置等。
  4. * 使用示例:
  5. * $obj = new WaterMask($imgFileName); //实例化对象
  6. * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印
  7. * $obj->$transparent = 45; //水印透明度
  8. * $obj->$waterStr = 'icp.niufee.com'; //水印文字
  9. * $obj->$fontSize = 18; //文字字体大小
  10. * $obj->$fontColor = array(255,255,255); //水印文字颜色(RGB)
  11. * $obj->$fontFile = 'AHGBold.ttf'; //字体文件
  12. * ……
  13. * $obj->output(); //输出水印图片文件覆盖到输入的图片文件
  14. * @modify liuzp111
  15. */
  16. class WaterMask{
  17. public $waterTypeImage = false; //水印类型:启用图片水印
  18. public $waterTypeStr = false; //水印类型:启用文字水印
  19. public $pos = 0; //水印位置
  20. public $transparent = 45; //水印透明度(0---100)数值越大越不透明
  21. public $waterStr = 'icp.niufee.com'; //水印文字
  22. public $fontSize = 14; //文字字体大小
  23. public $fontColor = array(0,0,0); //水印文字颜色(RGB) 默认黑色
  24. public $fontFile = './font/simfang.ttf'; //字体文件
  25. public $waterImg = 'logo.png'; //水印图片
  26. private $srcImg = ''; //需要添加水印的图片
  27. private $im = ''; //图片句柄
  28. private $water_im = ''; //水印图片句柄
  29. private $srcImg_info = ''; //图片信息
  30. private $waterImg_info = ''; //水印图片信息
  31. private $str_w = ''; //水印文字宽度
  32. private $str_h = ''; //水印文字高度
  33. private $x = ''; //水印X坐标
  34. private $y = ''; //水印y坐标
  35. public $output_img = ''; //存储输出图片到哪里
  36. public $is_draw_rectangle = false; //是否绘制矩形区域 (暂不支持自定义位置)
  37. //public $rectange_color = ''; //绘制矩形区域的颜色
  38. private $result_array = array(); //结果数组
  39. public function __construct($img) { //析构函数
  40. //$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
  41. if(file_exists($img)){
  42. $this->srcImg = $img;
  43. }else{
  44. return array('data'=>'','info'=>'源文件不存在!','status'=>0);
  45. }
  46. }
  47. private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。
  48. $this->srcImg_info = getimagesize($this->srcImg);
  49. switch ($this->srcImg_info[2]) {
  50. case 3:
  51. $this->im = imagecreatefrompng($this->srcImg);
  52. break 1;
  53. case 2:
  54. $this->im = imagecreatefromjpeg($this->srcImg);
  55. break 1;
  56. case 1:
  57. $this->im = imagecreatefromgif($this->srcImg);
  58. break 1;
  59. default:
  60. //die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  61. return array('data'=>'','info'=>'原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0);
  62. }
  63. }
  64. private function waterimginfo() { //获取水印图片的信息,并载入图片。
  65. $this->waterImg_info = getimagesize($this->waterImg);
  66. switch ($this->waterImg_info[2]) {
  67. case 3:
  68. $this->water_im = imagecreatefrompng($this->waterImg);
  69. break 1;
  70. case 2:
  71. $this->water_im = imagecreatefromjpeg($this->waterImg);
  72. break 1;
  73. case 1:
  74. $this->water_im = imagecreatefromgif($this->waterImg);
  75. break 1;
  76. default:
  77. //die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  78. return array('data'=>'','info'=>'水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0);
  79. }
  80. }
  81. private function waterpos() { //水印位置算法
  82. switch ($this->pos) {
  83. case 0: //随机位置
  84. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  85. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  86. break 1;
  87. case 1: //上左
  88. $this->x = 0;
  89. $this->y = 0;
  90. break 1;
  91. case 2: //上中
  92. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  93. $this->y = 0;
  94. break 1;
  95. case 3: //上右
  96. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  97. $this->y = 0;
  98. break 1;
  99. case 4: //中左
  100. $this->x = 0;
  101. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  102. break 1;
  103. case 5: //中中
  104. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  105. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  106. break 1;
  107. case 6: //中右
  108. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  109. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  110. break 1;
  111. case 7: //下左
  112. $this->x = 0;
  113. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  114. break 1;
  115. case 8: //下中
  116. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  117. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  118. break 1;
  119. case 9: //下中偏上100px
  120. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  121. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 100;
  122. break 1;
  123. default: //下右
  124. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  125. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  126. break 1;
  127. }
  128. }
  129. /**
  130. * 水印文字图片位置,根据需求调整
  131. */
  132. private function waterposStr() {
  133. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  134. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 3;
  135. }
  136. private function waterimg($type='') {
  137. if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
  138. //die('水印比原图大!');
  139. return array('data'=>'','info'=>'水印比原图大!','status'=>0);
  140. }
  141. if($type == 'waterstr'){
  142. $this->waterposStr();
  143. }else{
  144. $this->waterpos();
  145. }
  146. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  147. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  148. $pct = $this->transparent;
  149. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  150. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  151. }
  152. private function waterstr() {
  153. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  154. $w = abs($rect[2]-$rect[6]);
  155. $h = abs($rect[3]-$rect[7]);
  156. $fontHeight = $this->fontSize;
  157. $this->water_im = imagecreatetruecolor($w, $h);
  158. imagealphablending($this->water_im,false);
  159. imagesavealpha($this->water_im,true);
  160. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  161. imagefill($this->water_im,0,0,$white_alpha);
  162. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  163. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  164. $this->waterImg_info = array(0=>$w,1=>$h);
  165. $this->waterimg($type='waterstr');
  166. }
  167. /**
  168. * 绘制矩形区
  169. * bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
  170. * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
  171. * @author liuzp111
  172. */
  173. public function drawRectangle()
  174. {
  175. //imagefill($im,0,0,$gray);//填充资源,填充的坐标(类似PS魔棒),颜色
  176. /*
  177. * 1--------------画长方形--------------
  178. * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
  179. * 参数: 画布资源, 左上角x坐标,左上y坐标,右下x坐标,右下y坐标,颜色
  180. */
  181. $color = imagecolorallocate($this->im,255,255,255);//创建矩形边框颜色和填充颜色
  182. //=========================================================================
  183. //绘制矩形区域并填充
  184. // 参数说明:
  185. //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
  186. // im:为将图像载入为图像资源
  187. // $x1:表示矩形左上角的X坐标
  188. // $y1:表示矩形左上角的Y坐标
  189. // $x2:表示矩形右下角的X坐标
  190. // $y2:表示矩形右下角的Y坐标
  191. // $color:为填充的RGB颜色
  192. //
  193. imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color);
  194. //不要使用下方的函数填充,下方填充函数为魔棒填充,容易导致填充不完整
  195. //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充资源,填充的坐标(魔棒),颜色
  196. }
  197. function output() {
  198. $this->imginfo();
  199. //是否创建矩形区域
  200. if($this->is_draw_rectangle){
  201. $this->drawRectangle();
  202. }
  203. if ($this->waterTypeStr ) {
  204. $this->waterstr();
  205. }
  206. if($this->waterTypeImage )
  207. {
  208. $this->waterimginfo();
  209. $this->waterimg();
  210. }
  211. switch ($this->srcImg_info[2]) {
  212. case 3:
  213. $res_output = imagepng($this->im,$this->output_img);
  214. break 1;
  215. case 2:
  216. $res_output = imagejpeg($this->im,$this->output_img);
  217. break 1;
  218. case 1:
  219. $res_output = imagegif($this->im,$this->output_img);
  220. break 1;
  221. default:
  222. // die('添加水印失败!');
  223. return array('data'=>'','info'=>'添加水印失败!','status'=>0);
  224. break;
  225. }
  226. imagedestroy($this->im);
  227. imagedestroy($this->water_im);
  228. return array('data'=>$res_output,'info'=>'添加水印成功!','status'=>1);
  229. }
  230. }
复制代码

使用方式:

  1. $file = '58368dddc8c51_22';//需要加水印的图片
  2. $file_ext = '.jpeg';//扩展名
  3. $imgFileName = './'.$file.$file_ext;//需要加水印图片路径
  4. $obj = new WaterMask($imgFileName); //实例化对象
  5. $obj->waterTypeStr = true; //开启文字水印
  6. $obj->waterTypeImage = true; //开启图片水印
  7. $obj->pos = 9; //定义水印图片位置
  8. $obj->waterImg = './water.png'; //水印图片
  9. $obj->transparent = 100; //水印透明度
  10. $obj->waterStr = '保险经纪人:刘测试 电话:02052552'; //水印文字
  11. $obj->fontSize = 9; //文字字体大小
  12. $obj->fontColor = array(0,0,0); //水印文字颜色(RGB)
  13. $obj->fontFile = './font/msyh.ttc'; //字体文件,这里是微软雅黑
  14. $obj->is_draw_rectangle = TRUE; //开启绘制矩形区域
  15. $obj ->output_img = './'.$file.'_n'.$file_ext;//输出的图片路径
  16. $obj->output();
复制代码

以上这篇PHP水印类,支持添加图片、文字、填充颜色区域的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条