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

PHP 图片水印类的更多相关文章

  1. 不错.net图片水印类

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...

  2. C#图片水印类

    这个是学习用的呃,主要看一下水印在修改图片中距左边的宽度和高度是杂弄的就哦客了. using System; using System.Collections.Generic; using Syste ...

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

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

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

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

  5. php使用GD库实现图片水印和缩略图——封装成类

    学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...

  6. PHP的图片处理类(缩放、加图片水印和剪裁)

    <!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...

  7. 图片水印工具类java

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

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

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

  9. PHP水印类

    <?php /** * 水印类 * @author zhaoyingnan 2015/07/16 **/ include_once('extend.php'); class Watermark_ ...

随机推荐

  1. hdu 5747 Aaronson

    T :  1 n m:  10  2 题解:20 * 0  +  21* 1  +  22* 2 = 10 输出:3  <--  0+1+2=3 AC 代码: #include<stdio ...

  2. java gc的工作原理、如何优化GC的性能、如何和GC进行有效的交互

    java gc的工作原理.如何优化GC的性能.如何和GC进行有效的交互 一个优秀的Java 程序员必须了解GC 的工作原理.如何优化GC的性能.如何和GC进行有效的交互,因为有一些应用程序对性能要求较 ...

  3. matlab怎么定义一个数组

    A=[];n=input('n=');%数组的长度for i=1:n fprintf('a%.0f=',i); x=input('');%分别输入各个数的值 A=[A,x];endA就可以得到长度为n ...

  4. 畅通工程再续(MST)

    畅通工程再续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. Android--UI之EditText

    前言 上一篇博客介绍了Android的TextView控件,这篇博客来说一下EditText控件.EditText为一个文本控件,提供了文本输入的功能,而且继承自TextView,可以理解为可以输入的 ...

  6. Android控件之圆形Button

    bg_circle.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns: ...

  7. 反正切函数求圆周率 atan

    #define PI atan(1.0)*4 原理:tan ∏/4=1; atan2: 返回给定的 X 及 Y 坐标值的反正切值.反正切的角度值等于 X 轴正方向与通过原点和给定坐标点 (Y坐标, X ...

  8. C语言 单引号和双引号的区别

    最近的C语言课在教字符串,貌似N多同学搞不清楚单引号和双引号的区别,有人还以为在C语言里用哪个都可以...其实C语言中的单引号和双引号含义是一点也不一样滴... 1.含义不同. 用单引号引起的一个字符 ...

  9. Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  10. hadoop(一):深度剖析hdfs原理

    在配置hbase集群将 hdfs 挂接到其它镜像盘时,有不少困惑的地方,结合以前的资料再次学习;  大数据底层技术的三大基石起源于Google在2006年之前的三篇论文GFS.Map-Reduce. ...