forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 26
/
class.php
325 lines (313 loc) · 10.6 KB
/
class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
class WaterMark{
private $im_src; // 背景图文件
private $im_src_width; // 背景图宽度
private $im_src_height; // 背景图高度
private $src_im; // 由背景图创建的新图
private $im_water; // 水印图文件
private $im_water_width; // 水印图宽度
private $im_water_height; // 水印图高度
private $water_im; // 由水印图创建的新图
private $font; // 字体库
private $font_text; // 文本
private $font_size; // 字体大小
private $font_color; // 字体颜色
private $type; //类型
private $mime;
function setImSrc($img,$width = 0){
$this->im_src = $img;
//读取背景图片文件
$srcInfo = @getimagesize($this->im_src);
$this->im_src_width = $srcInfo[0];
$this->im_src_height = $srcInfo[1];
$this->type = $srcInfo[2];
$this->mime = $srcInfo['mime'];
//取得背景图片
$this->src_im = $this->getType($this->im_src, $srcInfo[2]);
if($width){
$this->resize($width);
}
}
function setImWater($img){
$this->im_water = $img;
//读取水印图片文件
$waterInfo = @getimagesize($this->im_water);
$this->im_water_width = $waterInfo[0];
$this->im_water_height = $waterInfo[1];
//取得水印图片
$this->water_im = $this->getType($this->im_water, $waterInfo[2]);
}
function setFont($font, $text, $size, $color){
$this->font = $font;
$this->font_text = $text;
$this->font_size = $size;
//水印文字颜色('255,255,255')
$this->font_color = $color;
}
/**
* 根据文件或URL创建一个新图象
* @param $img
* @param $type
* @return resource
*/
function getType($img, $type)
{
switch($type){
case 1:
$im = imagecreatefromgif($img);
break;
case 2:
$im = imagecreatefromjpeg($img);
break;
case 3:
$im = imagecreatefrompng($img);
imagesavealpha($im, true);
break;
default:break;
}
return $im;
}
/**
* 根据位置及水印宽高,获取打印的x/y坐标
* @param $pos
* @param $w
* @param $h
*/
function getPos($pos, $w, $h)
{
switch($pos){
case 0://随机
$posX = rand(0, ($this->im_src_width - $w));
$posY = rand(0, ($this->im_src_height - $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ceil($this->im_src_width - $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $this->im_src_width - $w;
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ceil($this->im_src_height - $h) / 2;
break;
case 5://5为中部居中
$posX = ceil($this->im_src_width - $w) / 2;
$posY = ceil($this->im_src_height - $h) / 2;
break;
case 6://6为中部居右
$posX = $this->im_src_width - $w;
$posY = ceil($this->im_src_height - $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $this->im_src_height - $h;
break;
case 8://8为底端居中
$posX = ceil($this->im_src_width - $w) / 2;
$posY = $this->im_src_height - $h;
break;
case 9://9为底端居右
$posX = $this->im_src_width - $w;
$posY = $this->im_src_height - $h;
break;
default://随机
$posX = rand(0,($this->im_src_width - $w));
$posY = rand(0,($this->im_src_height - $h));
break;
}
return array($posX, $posY);
}
/**
* 校验尺寸
* @param $w
* @param $h
* @return boolean
*/
function check_range($w, $h){
if( ($this->im_src_width < $w) || ($this->im_src_height < $h) ){
return false;
}
return true;
}
/**
* 打水印操作
* @param $is_image 是1否0水印图片
* @param $image_pos 水印图片位置(0~9)
* @param $is_text 是1否0水印文字
* @param $text_pos 水印文字位置(0~9)
*/
function mark($is_image=0, $image_pos=0, $is_text=0, $text_pos=0, $mic_x=0, $mic_y=0, $alpha=0, $save=FALSE){
// 水印图片情况
if ($is_image)
{
// 校验图片大小,太小无法加水印直接返回
if (!$this->check_range($this->im_water_width, $this->im_water_height)){
$this->show();
$this->clean();
return;
}
$posArr = $this->getPos($image_pos, $this->im_water_width, $this->im_water_height);
$posX = $posArr[0];
$posY = $posArr[1];
// 拷贝水印到目标文件
if($alpha){
$this->imagecopymerge_alpha($this->src_im, $this->water_im, $posX, $posY, 0, 0, $this->im_water_width, $this->im_water_height, $alpha);
}else{
imagecopy($this->src_im, $this->water_im, $posX, $posY, 0, 0, $this->im_water_width, $this->im_water_height);
}
}
// 水印文字情况
if ($is_text){
//取得此字体的文本的范围
$temp = imagettfbbox($this->font_size, 0, $this->font, $this->font_text);
$w = $temp[2] - $temp[0];
$h = $temp[1] - $temp[7];
unset($temp);
// 校验图片大小,太小无法加水印直接返回
if (!$this->check_range($w, $h)){
$this->show();
$this->clean();
return;
}
$posArr = $this->getPos($text_pos, $w, $h);
$posX = $posArr[0]+$mic_x;
$posY = $posArr[1]+$mic_y;
// 打印文本
$rgb = explode(',', $this->font_color);
if (count($rgb) != 3){
$rgb = $this->hex2rgb($this->font_color);
file_put_contents('log.txt',$this->font_color);
$color = imagecolorallocate($this->src_im, $rgb['r'], $rgb['g'], $rgb['b']);
}else{
$color = imagecolorallocate($this->src_im, $rgb[0], $rgb[1], $rgb[2]);
}
imagettftext($this->src_im, $this->font_size, 0, $posX, $posY, $color, $this->font, $this->font_text);
}
// 输出
$this->show();
if($save){
$this->save($save);
}
// 清理
$this->clean();
}
/**
* 输出图像
*/
function show() {
ob_clean();
header("Content-type: {$this->mime}; charset=UTF-8");
switch ($this->type){
case 1:
imagegif($this->src_im);
break;
case 2:
imagejpeg($this->src_im);
break;
default :
imagepng($this->src_im);
}
}
/**
* 存储图像
*/
function save($file){
switch ($this->type){
case 1:
imagegif($this->src_im,$file);
break;
case 2:
imagejpeg($this->src_im,$file);
break;
default :
imagepng($this->src_im,$file);
}
}
/**
* 清理
*/
function clean(){
imagedestroy($this->water_im);
imagedestroy($this->src_im);
}
/**
* 将十六进制颜色转为RGB
* @param type $hexColor
* @return array
*/
function hex2rgb($hexColor) {
$color = substr($hexColor,1);
if (strlen($color) > 3) {
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
} else {
$color = $hexColor;
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
'r' => hexdec($r),
'g' => hexdec($g),
'b' => hexdec($b)
);
}
return $rgb;
}
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$opacity=$pct;
// getting the watermark width
$w = imagesx($src_im);
// getting the watermark height
$h = imagesy($src_im);
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying that section of the background to the cut
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// inverting the opacity
$opacity = 100 - $opacity;
// placing the watermark now
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);
}
/**
* 重设图片大小
*
* @param type $maxwidth
*/
function resize($maxwidth=600){
$im = $this->src_im;
$width = imagesx($im);
$height = imagesy($im);
if($maxwidth && $width > $maxwidth){
$ratio = $maxwidth/$width;
$this->im_src_width = $width * $ratio;
$this->im_src_height = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($this->im_src_width , $this->im_src_height);
if(3 === $this->type){
imagealphablending($newim, false);
imagesavealpha($newim, true);
}
imagecopyresampled($newim, $im, 0, 0, 0, 0, $this->im_src_width , $this->im_src_height, $width, $height);
}else{
$newim = imagecreate($this->im_src_width , $this->im_src_height);
if(3 === $this->type){
imagealphablending($newim, false);
imagesavealpha($newim, true);
}
imagecopyresized($newim, $im, 0, 0, 0, 0, $this->im_src_width , $this->im_src_height, $width, $height);
}
$this->src_im = $newim;
}
}
}
?>