1 <?php
2 /**
3 * 水印类
4 * @author zhaoyingnan 2015/07/16
5 **/
6 include_once('extend.php');
7 class Watermark_extend extends Extend
8 {
9 private $_isString;//水印类型是否为字符串
10 private $_imageInfo;//图片的信息array('dirname' => '路径','basename' => '文件名.扩展名','extension' => '扩展名','filename' => '文件名','type' => 类型,'height' => 高,'width' =>宽)
11 private $_iAngle = 0;//角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
12 private $_iPct = 15;//图片水印透明度,值越小透明度越高
13 private $_srcImage;
14 private $_resImage;//resource of type (gd)
15 private $_resWaterImage;//resource of type (gd)
16 private $_sString = 'Fuck You';//水印文字
17 private $_iStringSize = 20;//字体大小
18 private $_trueType;//字体库
19 private $_waterImg;//水印图片
20
21 /**
22 * 设置水印参数
23 * @author zhaoyingnan 2015/07/16
24 * @param string $image 图片路径
25 * @param bool $isString
26 * @return array('error'=>int,'msg'=>string) error大于0为成功,msg为描述
27 **/
28 public function setParam($image, $isString)
29 {
30 if(!file_exists($image))return array('error'=>-1,'msg'=>'文件不存在');
31 $this->_trueType = __ROOTPATH__.'Kreepshow.ttf';//字体库位置
32 $this->_waterImg = __ROOTPATH__.'img/logo/logo_16.png';//水印图片位置
33 $this->_srcImage = $image;
34 $this->_isString = $isString ? true : false;//是否为文字水印
35 $this->_imageInfo = pathinfo($this->_srcImage);//图片信息
36 list($this->_imageInfo['width'], $this->_imageInfo['height'], $this->_imageInfo['type']) = @getimagesize($this->_srcImage);//图片信息
37 //dump($this->_imageInfo);
38 switch($this->_imageInfo['type'])
39 {
40 case IMAGETYPE_PNG://3
41 $this->_resImage = @imagecreatefrompng($this->_srcImage);
42 break;
43 case IMAGETYPE_JPEG://2
44 $this->_resImage = @imagecreatefromjpeg($this->_srcImage);
45 break;
46 case IMAGETYPE_GIF://1
47 $this->_resImage = @imagecreatefromgif($this->_srcImage);
48 break;
49 default:
50 return array('error'=>-2,'msg'=>'文件类型不正确');
51 }
52 //dump($this->_resImage);
53 return array('error'=>1,'msg'=>'ok');
54 }
55
56 /**
57 * 计算水印的坐标
58 * @author zhaoyingnan 2015/07/17
59 * @param int $iWidth 水印的宽度
60 * @param int $iHeight 水印的高度
61 * @return array array(int $x,int $y)
62 **/
63 public function calculatePosition($iWidth, $iHeight)
64 {
65 if($this->_imageInfo['width'] < $iWidth || $this->_imageInfo['height'] < $iHeight)
66 return null;
67 //return array(($this->_imageInfo['width']-$iWidth)/2, ($this->_imageInfo['height']-$iHeight)/2);
68 if($this->_isString)
69 return array($this->_imageInfo['width']/2-$iWidth, $this->_imageInfo['height']/2);
70 else
71 return array(($this->_imageInfo['width']-$iWidth)/2, ($this->_imageInfo['height']-$iHeight)/2);
72 }
73
74 /**
75 * 执行添加水印
76 * @author zhaoyingnan 2015/07/17
77 * @return array('error'=>int,'msg'=>string) error大于0为成功,msg为描述
78 **/
79 public function executeWater()
80 {
81 if($this->_isString)
82 {
83 //水印为文字
84 $black = @imagecolorallocate($this->_resImage, 0, 0, 0);#颜色
85 /*
86 array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )
87 size 像素单位的字体大小。
88 angle text 将被度量的角度大小。
89 fontfile TrueType 字体文件的文件名(可以是 URL)。根据 PHP 所使用的 GD 库版本,可能尝试搜索那些不是以 '/' 开头的文件名并加上 '.ttf' 的后缀并搜索库定义的字体路径
90 text 要度量的字符串。
91 imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
92 0 左下角 X 位置
93 1 左下角 Y 位置
94 2 右下角 X 位置
95 3 右下角 Y 位置
96 4 右上角 X 位置
97 5 右上角 Y 位置
98 6 左上角 X 位置
99 7 左上角 Y 位置
100 */
101 //list(,$rightbottomX,$rightbottomY,,,$lefttopX,$lefttopY) = imagettfbbox($this->_iStringSize, 0, __ROOTPATH__.'movieola.ttf', $this->_sString);
102 list(,$rightbottomX,$rightbottomY,,,$lefttopX,$lefttopY) = @imagettfbbox($this->_iStringSize, $this->_iAngle, $this->_trueType, $this->_sString);
103
104 //计算水印的宽度,长度
105 $iWidth = $rightbottomX - $lefttopX;
106 $iHeight = $rightbottomY - $lefttopY;
107
108 list($x, $y) = $this->calculatePosition($iWidth, $iHeight);
109
110 //调试
111 //echo '图片宽度'.$this->_imageInfo['width'].'<br/>';
112 //echo '图片高度'.$this->_imageInfo['height'].'<br/>';
113 //echo '水印宽度'.$iWidth.'<br/>';
114 //echo '水印高度'.$iHeight.'<br/>';
115 //echo '坐标X'.$x.'<br/>';
116 //echo '坐标Y'.$y.'<br/>';
117
118 if(!$x||!$y)return array('error'=>-3,'msg'=>'图片大小小于水印的大小,无法添加水印');
119 /*
120 imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
121 image 由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
122 size 字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。
123 angle 角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
124 x 由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0
125 Y 坐标。它设定了字体基线的位置,不是字符的最底端。
126 color 颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 imagecolorallocate()。
127 fontfile 是想要使用的 TrueType 字体的路径。
128 text UTF-8 编码的文本字符串
129 */
130 //imagettftext($this->_resImage, $this->_iStringSize, 0, $x, $y, $black, __ROOTPATH__.'movieola.ttf', $this->_sString);
131 @imagettftext($this->_resImage, $this->_iStringSize, $this->_iAngle, $x, $y, $black, $this->_trueType, $this->_sString);
132 }
133 else
134 {
135 //图片水印
136 $this->_resWaterImage = @imagecreatefrompng($this->_waterImg);
137 /*
138 bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
139 将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
140
141 bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
142 将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。
143 当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
144 */
145
146 list($x, $y) = $this->calculatePosition(16, 16);//已经知道了水印图片的高度和宽度,不用获取了
147 if(!$x||!$y)return array('error'=>-3,'msg'=>'图片大小小于水印的大小,无法添加水印');
148 $bool = @imagecopy($this->_resImage, $this->_resWaterImage, $x, $y, 0, 0, 16, 16);
149 //$bool = @imagecopymerge($this->_resImage, $this->_resWaterImage, $x, $y, 0, 0, 512, 512, $this->_iPct);
150 if(!$bool)return array('error'=>-4,'msg'=>'添加水印失败');
151 }
152
153 switch($this->_imageInfo['type'])
154 {
155 case IMAGETYPE_PNG://3
156 $this->_resImage = @imagepng($this->_resImage, $this->_srcImage);
157 break;
158 case IMAGETYPE_JPEG://2
159 $this->_resImage = @imagejpeg($this->_resImage, $this->_srcImage);
160 break;
161 case IMAGETYPE_GIF://1
162 $this->_resImage = @imagegif($this->_resImage, $this->_srcImage);
163 break;
164 default:
165 return array('error'=>-2,'msg'=>'文件类型不正确');
166 }
167 @imagedestroy($this->_resImage);
168 return array('error'=>1,'msg'=>'ok');
169 }
170 }
171 ?>