ImageWatermark.php


  1. <?php
  2. /***********************************************************
  3. 类名:ImageWatermark
  4. 功能:用于生成图片或文字水印
  5. WDPHP素材源码 http://www.wdphp.com
  6. ************************************************************
  7. 合成水印:
  8. 1、图像水印appendImageMark(暂不可旋转)
  9. 2、文字水印appendTextMark(汉字水印需要设置汉字字体)(可旋转)
  10. 输出水印图像:write($filename=null)
  11. 1、输出到文件:指定$filename参数为输出的文件名。
  12. 2、输出到浏览器:不指定输出文件名,则输出到浏览器.
  13. 指定水印位置:
  14. 1、指定位置类型$markPosType:(default-0)
  15. 1-top left 2-top center 3-top right
  16. 4-middle left 5-middle center 6-middle right
  17. 7-bottom left 8-bottom center 9-bottom right
  18. 0-random
  19. 2、设置具体位置setMarkPos($x,$y),若指定具体位置,则上面的位置类型无效。
  20. ************************************************************
  21. */
  22. class ImageWatermark {
  23. public $markPosType = 0; //水印位置,缺省为随机位置输出水印
  24. public $fontFile = 'arial.ttf'; //字体文件名
  25. public $color = '#CCCCCC'; //水印字体的颜色
  26. public $fontSize = 12; //水印字体大小
  27. public $angle = 0; //水印文字旋转的角度
  28. private $markPos = array();
  29. private $markImageFile = null, $destImageFile = null;
  30. private $mark_res = null, $mark_width = 0, $mark_height = 0, $mark_type = null;
  31. private $dest_res = null, $dest_width = 0, $dest_height = 0, $dest_type = null;
  32. //用目标图片作为构造函数的参数
  33. public function __construct($destImage) {
  34. if (!file_exists($destImage)) return false;
  35. $this->destImageFile = $destImage;
  36. //获取图片大小、类型
  37. $imageInfo = getimagesize($this->destImageFile);
  38. $this->dest_width = $imageInfo[0];
  39. $this->dest_height = $imageInfo[1];
  40. $this->dest_type = $imageInfo[2];
  41. //得到图片资源句柄
  42. $this->dest_res = $this->getImageResource($this->destImageFile, $this->dest_type);
  43. }
  44. public function __destruct() {
  45. imagedestroy($this->dest_res);
  46. }
  47. //添加文字水印
  48. public function appendTextMark($markText) {
  49. if ($markText == null) return false;
  50. //计算水印文本的大小
  51. $box = imagettfbbox($this->fontSize, $this->angle, $this->fontFile, $markText);
  52. $this->mark_width = $box[2] - $box[6];
  53. $this->mark_height = $box[3] - $box[7];
  54. //计算水印位置
  55. $pos = ($this->markPos != null) ? $this->markPos : $this->getMarkPosition($this->markPosType);
  56. $pos[1]+= $this->mark_height;
  57. //将文字打印到图片上
  58. $RGB = $this->colorHexRgb($this->color);
  59. $imageColor = imagecolorallocate($this->dest_res, $RGB[0], $RGB[1], $RGB[2]);
  60. imagettftext($this->dest_res, $this->fontSize, $this->angle, $pos[0], $pos[1], $imageColor, $this->fontFile, $markText);
  61. }
  62. //添加图片水印
  63. public function appendImageMark($markImage) {
  64. if (!file_exists($markImage)) return false;
  65. $this->markImageFile = $markImage;
  66. //获取水印图片大小、类型
  67. $imageInfo = getimagesize($this->markImageFile);
  68. $this->mark_width = $imageInfo[0];
  69. $this->mark_height = $imageInfo[1];
  70. $this->mark_type = $imageInfo[2];
  71. //得到图片资源句柄
  72. $this->mark_res = $this->getImageResource($this->markImageFile, $this->mark_type);
  73. //计算水印位置
  74. $pos = ($this->markPos != null) ? $this->markPos : $this->getMarkPosition($this->markPosType);
  75. //设置图像混色模式
  76. imagealphablending($this->dest_res, true);
  77. //复制叠加图像
  78. imagecopy($this->dest_res, $this->mark_res, $pos[0], $pos[1], 0, 0, $this->mark_width, $this->mark_height);
  79. imagedestroy($this->mark_res);
  80. }
  81. //将叠加水印后的图片写入指定文件,若不定文件名,则输出到浏览器
  82. public function write($filename = null) {
  83. $this->writeImage($this->dest_res, $filename, $this->dest_type);
  84. }
  85. //设置水印x,y坐标
  86. public function setMarkPos($x, $y) {
  87. $this->markPos[0] = $x;
  88. $this->markPos[1] = $y;
  89. }
  90. //将十六进制的颜色值分解成RGB形式
  91. private function colorHexRgb($color) {
  92. $color = preg_replace('/#/', '', $color);
  93. $R = hexdec($color[0] . $color[1]);
  94. $G = hexdec($color[2] . $color[3]);
  95. $B = hexdec($color[4] . $color[5]);
  96. return array(
  97. $R,
  98. $G,
  99. $B
  100. );
  101. }
  102. //计算水印位置
  103. private function getMarkPosition($type = 0) {
  104. switch ($type) {
  105. case 0:
  106. $x = rand(0, $this->dest_width - $this->mark_width);
  107. $y = rand(0, $this->dest_height - $this->mark_height);
  108. break; //random
  109. case 1:
  110. $x = 0;
  111. $y = 0;
  112. break; //topleft
  113. case 2:
  114. $x = ($this->dest_width - $this->mark_width) / 2;
  115. $y = 0;
  116. break; //topcenter
  117. case 3:
  118. $x = $this->dest_width - $this->mark_width;
  119. $y = 0;
  120. break; // topright
  121. case 4:
  122. $x = 0;
  123. $y = ($this->dest_height - $this->mark_height) / 2;
  124. break; //middleleft
  125. case 5:
  126. $x = ($this->dest_width - $this->mark_width) / 2;
  127. $y = ($this->dest_height - $this->mark_height) / 2;
  128. break; //middlecenter
  129. case 6:
  130. $x = $this->dest_width - $this->mark_width;
  131. $y = ($this->dest_height - $this->mark_height) / 2;
  132. break; //middleright
  133. case 7:
  134. $x = 0;
  135. $y = $this->dest_height - $this->mark_height;
  136. break; //bottomleft
  137. case 8:
  138. $x = ($this->dest_width - $this->mark_width) / 2;
  139. $y = $this->dest_height - $this->mark_height;
  140. break; //bottomcenter
  141. case 9:
  142. $x = $this->dest_width - $this->mark_width;
  143. $y = $this->dest_height - $this->mark_height;
  144. break; //bottomright
  145. default:
  146. $x = rand(0, $this->dest_width - $this->mark_width);
  147. $y = rand(0, $this->dest_height - $this->mark_height);
  148. break; //random
  149. }
  150. return array(
  151. $x,
  152. $y
  153. );
  154. }
  155. //从一个图像文件中取得图片资源标识符
  156. private function getImageResource($filename, $type = 0) {
  157. switch ($type) {
  158. case 1:
  159. return imagecreatefromgif($filename);
  160. break;
  161. case 2:
  162. return imagecreatefromjpeg($filename);
  163. break;
  164. case 3:
  165. return imagecreatefrompng($filename);
  166. break;
  167. // 以后可添加其它格式
  168. default:
  169. return null;
  170. }
  171. }
  172. //将图像写入文件或输出到浏览器
  173. private function writeImage($ImageRes, $filename = null, $type = 0) {
  174. switch ($type) {
  175. case 1:
  176. imagegif($ImageRes, $filename);
  177. break;
  178. case 2:
  179. imagejpeg($ImageRes, $filename);
  180. break;
  181. case 3:
  182. imagepng($ImageRes, $filename);
  183. break;
  184. default:
  185. return null;
  186. }
  187. return true;
  188. }
  189. }
  190. //使用示例
  191. $markimg = new ImageWatermark('c_si.jpg');
  192. //$markimg->setMarkPos(100,200);//如何设置setMarkPos,则markPosType无效。
  193. $markimg->markPosType = 5;
  194. $markimg->appendImageMark('mark.png');
  195. $markimg->fontFile = 'STCAIYUN.TTF';
  196. $markimg->color = '#FFFFFF';
  197. $markimg->fontSize = 24;
  198. $markimg->angle = 45; //设置角度时,注意水印可能旋转出目标图片之外。
  199. $markimg->appendTextMark('汉字水印');
  200. $markimg->write();
  201. $markimg = null;
  202. ?>

PHP的生成图片或文字水印的类的更多相关文章

  1. 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  2. pdo文字水印类,验证码类,缩略图类,logo类

    文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...

  3. java常用开发工具类之 图片水印,文字水印,缩放,补白工具类

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  4. asp .net 为图片添加文字水印(内包含有加图片水印的方法) .

    在项目中先创建一个Imag_writer 类库 在该类库下分别创建两个枚举类型WaterMarkType (水印的类型).WaterMarkPosition (水印的位置).代码如下: using S ...

  5. 图片水印工具类java

    关于jar包的导入我就不多说了,我会把引入的jar包显示出来,大家自行Google package com.net.util; import java.awt.AlphaComposite; impo ...

  6. java创建透明背景的PNG图片加自定义文字水印

    人在码上走,需求天天有.这不,今天前端让我返回一个带自定义水印的背景图片.一通google,有现成的代码,但是基本是直接在源图上添加水印,生成出来的文字样式也没有控制好,看来又只有自己造轮子了. 过程 ...

  7. Asp.net 上传图片添加半透明图片或者文字水印的方法

    主要用到System.Drawing 命名空间下的相关类,如Brush.Image.Bitmap.Graphics等等类 Image类可以从图片文件创建Image的实例,Bitmap可以从文件也可以从 ...

  8. 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

    1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image ...

  9. Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)

    想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...

随机推荐

  1. warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

    解决方法:是所有项目的这个"代码生成"属性设置保持一致. 项目——属性——配置属性——C/C++——代码生成:他有/MT,/MTd,/Md,/MDd四个选项,你必须让所有使用的库都 ...

  2. wpf listbox touch 整个窗口移动

    工作中遇到遇到,在有listbox中的地方,touch listbox的时候  可以把整个窗体都移动了,解决方案如下: /// <summary> /// prevent the rubb ...

  3. wc,sort,uniq,awk,grep

    wc awk, sort, uniq grep

  4. JZOJ2368 【SDOI2011】黑白棋

    题目 题目大意 在一个1*n的棋盘上,有黑棋和白棋交错分布,每次,一个人可以移动自己的ddd颗旗子. 问先手必胜的方案数. 思考历程 在一开始,我就有点要放弃的念头. 因为这题是一道博弈问题. 我是非 ...

  5. Python学习之enumerate

         enumerate还可以接收第二个参数,用于指定索引起始值   2.     注意open返回文件对象,可迭代,而os.open返回的是文件指针,int类型, <wiz_tmp_tag ...

  6. (转载)JavaScript世界万物诞生记

    一. 无中生有 起初,什么都没有.造物主说:没有东西本身也是一种东西啊,于是就有了null: 现在我们要造点儿东西出来.但是没有原料怎么办?有一个声音说:不是有null嘛?另一个声音说:可是null代 ...

  7. 19-10-23-L-Mor

    ZJ一下: 挺好,T2打表差点出规律(最近拿PFGYL硬卡提升自己几乎没有的打表水平) T1竟然……是个××题 T3的Floyd写死了. T1 简单思考会发现……直接全异或起来就AC 话说T1真叫最大 ...

  8. 软件-UlitraEdit:UIitraEdit

    ylbtech-软件-UlitraEdit:UIitraEdit UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足够强大),内 ...

  9. SPSS分析技术:CMH检验(分层卡方检验);辛普森悖论,数据分析的谬误

    SPSS分析技术:CMH检验(分层卡方检验):辛普森悖论,数据分析的谬误 只涉及两个分类变量的卡方检验有些时候是很局限的,因为混杂因素总是存在,如果不考虑混杂因素,得出的分析结论很可能是谬误的,这就是 ...

  10. .h头文件 .lib动态链接库文件 .dll 动态链接库

    (1).h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib 不是.dll 若生成了DLL ,则肯定也生成 LIB文件 如果要完成源代码的编译和链接,有头文件和 ...