-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTiledWatermark.class.php
405 lines (358 loc) · 14.1 KB
/
TiledWatermark.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* 文字图案水印平铺工具类
*
* @time 2017.02.16 23:26:33
* @author Joe
* @link https://tahenniu.com
*
*/
class TiledWatermark
{
private static $_instance;
private $use_size_array = true;
private $is_run = false;
private $horizontal = 0;
private $vertical = 0;
private $font_size = 12;
# 基础配置
private $all_config = array();
private function __construct()
{}
private function __clone()
{}
public static function getInstance()
{
if (!self::$_instance instanceof self)
{
self::$_instance = new self;
}
return self::$_instance;
}
/**
* mime类型 file ext
*
* @var array
*/
private static $mime_types = array(
2 => array('jpeg', '.jpg'),
3 => array('png', '.png')
);
/**
* size数组
*
* @var array
*
* 参数分别为:背景width范围,font-size,x以及y坐标位置偏移量
* 可根据实际使用情况调整以达到最佳显示效果
*/
private $size_array = array(
320 => array(12, 120, 55),
480 => array(13, 130, 60),
700 => array(14, 140, 80),
1200 => array(18, 180, 100),
1900 => array(26, 280, 120),
2500 => array(36, 430, 180),
3200 => array(52, 630, 260),
'max_size' => array(66, 730, 280)
);
/**
* 检查图片
*
*/
private function ckeckImage($image_path_type = 'bg')
{
# 检查图片
$this->image_type_name = $image_path_type =='bg' ? '背景' : 'logo';
$this->image_path = $image_path_type =='bg' ? $this->all_config['draw_bg'] : $this->all_config['logo_img'];
if(!$this->image_path || !is_file($this->image_path))
{
trigger_error($this->image_type_name .'图片不存在!"' . $image_path . '"', E_USER_ERROR);
}
# 图片数据,0 width, 1 height mime mime类型
$tmp_data = getimagesize($this->image_path);
# 检查图片尺寸
if(!isset($tmp_data[0]) || $tmp_data[0] < 1 || !isset($tmp_data[1]) || $tmp_data[1] < 1) {
trigger_error('路径:' . $this->image_path . '无效或已损毁的'.$this->image_type_name.'图片!');
}
# 检查图片类型
if(!isset($tmp_data[2]) || !isset(self::$mime_types[$tmp_data[2]][0])) {
trigger_error('路径:' . $this->image_path . '不被允许的'.$this->image_type_name.'图片类型!', E_USER_ERROR);
}
if ($this->image_type_name =='背景')
{
$this->tmp_data = getimagesize($this->all_config['draw_bg']);
list($this->img_w, $this->img_h) = $this->tmp_data;
}
else
{
$this->logo_tmp_data = getimagesize($this->all_config['logo_img']);
list($this->logo_w, $this->logo_h) = $this->logo_tmp_data;
}
}
/**
* 初始化
*/
private function initialize()
{
if(!$this->is_run)
{
$this->ckeckImage('bg');
# 是否已加载GD库
if(!function_exists('getimagesize'))
{
trigger_error(__CLASS__ . '运行依赖GD库,请检查扩展是否安装或加载!', E_USER_ERROR);
}
if($this->all_config['draw_type'] =='img')
{
# 检查logo文件
$this->ckeckImage('logo');
}
else
{
# 检查字体文件
if(!is_file($this->all_config['font_file'])) {
trigger_error('字体文件不存在!', E_USER_ERROR);
}
# 是否为水印设置了有效的RBG参数
if($this->all_config['text_rgb'] === null || strlen($this->all_config['text_rgb']) < 1)
{
# 设置水印默认RGB值
$this->all_config['text_rgb'] = 'x0b4';
}
# 是否为阴影设置了有效的RGB参数
if($this->all_config['shadow'])
{
if($this->all_config['shadow_rgb'] === null || strlen($this->all_config['shadow_rgb']) < 1)
{
# 默认阴影RGB
$this->all_config['shadow_rgb'] = '0x00';
}
}
# 检查水印文本
if(strlen($this->all_config['watermark_text']) < 1 || str_replace(' ','',$this->all_config['watermark_text']) =='')
{
trigger_error('无效的水印文字!', E_USER_ERROR);
}
}
# 可以开始绘制水印
$this->is_run = true;
}
}
/**
* RGB字符串转换为数组
*
* @param mixed $rgb_string
* @return array 0 red, 1 green, 2 blue.
*/
private function rgbStringToArray($rgb_string)
{
$rgb = array(
0 => null,
1 => null,
2 => null
);
# 单个值
if(strpos($rgb_string, ',') === false)
{
for($i = 0; $i < 3; $i++)
{
$rgb[$i] = $rgb_string;
}
}
# 多个值
else
{
$rgb_string = explode(',', $rgb_string);
if(count($rgb_string) == 3)
{
for($i = 0; $i < count($rgb_string); $i++)
{
$rgb[$i] = trim($rgb_string[$i]);
}
}
else
{
trigger_error('rgb参数不正确!', E_USER_ERROR);
}
}
return $rgb;
}
/**
* 计算标记水印
*
* @staticvar int $i
* @param array $points
* @param int $p
* @param int $max_x
* @return array
*/
private function markPoints($points = array(), $p = 20, $max_x = 0)
{
static $i = 0;
if(count($points) > 0) {
$i++;
$rand = rand(0, count($points) - 1);
if(isset($points[$rand]))
{
# 确保位置不超出范围
if(($points[$rand]['x'] < $max_x && $points[$rand]['y'] != $p && $points[$rand]['y'] < $max_x) || $i >= 100)
{
$points[$rand]['flag'] = true;
}
else
{
return $this->markPoints($points, $p, $max_x);
}
}
}
return $points;
}
/**
* 绘制水印
*
*/
public function okIsRun($config)
{
$this->all_config = $config;
$this->initialize();
# 使用尺寸数组
if(is_array($this->size_array) && count($this->size_array) > 0)
{
$img_max = $this->img_w > $this->img_h ? $this->img_w : $this->img_h;
$font_set = false;
foreach($this->size_array as $size => $v)
{
if($size !== 'max_size')
{
if($img_max < $size && isset($v[0]) && isset($v[1]))
{
$this->font_size = (int)$v[0];
$this->horizontal = (int)$v[1];
$this->vertical = (int)$v[2];
$font_set = true;
break;
}
}
}
# 设置最大尺寸
if(!$font_set && isset($this->size_array['max_size'][0]) && isset($this->size_array['max_size'][1]))
{
$this->font_size = (int)$this->size_array['max_size'][0];
$this->horizontal = (int)$this->size_array['max_size'][1];
$this->vertical = (int)$this->size_array['max_size'][1];
}
}
# png
if($this->tmp_data[2] == 3)
{
$img = imagecreatetruecolor($this->img_w, $this->img_h);
$img_source = imagecreatefrompng($this->all_config['draw_bg']);
imagecopyresampled($img, $img_source, 0, 0, 0, 0, $this->img_w, $this->img_h, $this->img_w, $this->img_h);
}
else
{
# 按需生成图像资源
$f = 'imagecreatefrom' . self::$mime_types[$this->tmp_data[2]][0];
$img = $f($this->all_config['draw_bg']);
}
# 设置水印颜色
$rgb = $this->rgbStringToArray($this->all_config['text_rgb']);
$rgb2 = $this->rgbStringToArray($this->all_config['shadow_rgb']);
# 如果设置透明度需要使用透明效果
if((int)$this->all_config['opacity'] > 0 && (int)$this->all_config['opacity'] <= 127)
{
$wm_color = imagecolorallocatealpha($img, $rgb[0], $rgb[1], $rgb[2], (int)$this->all_config['opacity']);
if($this->all_config['shadow'])
{
$wm_shadow_color = imagecolorallocatealpha($img, $rgb2[0], $rgb2[1], $rgb2[2], (int)$this->all_config['opacity']);
}
}
else
{
$wm_color = imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
if($this->all_config['shadow'])
{
$wm_shadow_color = imagecolorallocate($img, $rgb2[0], $rgb2[1], $rgb2[2]);
}
}
# 生成绘制坐标点
$p = $this->all_config['random_location'] ? rand(10,100) : 25;
$i = $max_x = 0;
$points = array();
for($x = $p; $x < $this->img_w; $x += $this->horizontal)
{
if($x > $max_x)
{
$max_x = $x;
}
for($y = $p; $y < $this->img_h; $y += $this->vertical)
{
$points[$i] = array('x' => $x, 'y' => $y);
if($this->all_config['shadow'])
{
$points[$i]['x2'] = $x + $this->all_config['shadow_offset'];
$points[$i]['y2'] = $y + $this->all_config['shadow_offset'];
}
$i++;
}
}
switch ($this->all_config['draw_type'])
{
case 'txt':
foreach($points as $m)
{
if(isset($m['x2']) && isset($m['y2']))
{
# 绘制阴影
imagettftext($img, $this->font_size, $this->all_config['rotate_angle'], $m['x2'], $m['y2'],
$wm_shadow_color, $this->all_config['font_file'], $this->all_config['watermark_text']);
}
# 绘制水印
imagettftext($img, $this->font_size, $this->all_config['rotate_angle'], $m['x'], $m['y'], $wm_color,
$this->all_config['font_file'], $this->all_config['watermark_text']);
}
break;
case 'img':
$f = 'imagecreatefrom' . self::$mime_types[$this->logo_tmp_data[2]][0];
$logo_img = $f($this->all_config['logo_img']);
foreach($points as $m)
{
# 合并图像
imagecopy($img,$logo_img,$m['x'],$m['y'],0,0,$this->logo_w,$this->logo_h);
}
break;
default:
trigger_error('水印类型参数不正确!', E_USER_ERROR);
break;
}
if ($this->tmp_data[2] == 3 || (isset($this->logo_tmp_data[2]) && $this->logo_tmp_data[2] == 3))
{
$f = 'imagepng';
$o_ext = self::$mime_types[3][1];
}
else
{
$f = 'image' . self::$mime_types[$this->tmp_data[2]][0];
$o_ext = self::$mime_types[$this->tmp_data[2]][1];
}
if(isset($this->logo_tmp_data[2]) && $this->logo_tmp_data[2] == 3)
{
$this->out_image_mime = $this->logo_tmp_data['mime'];
}
else
{
$this->out_image_mime = $this->tmp_data['mime'];
}
# 输出图像
header('Content-type: '.$this->out_image_mime);
$f($img);
//$f($img, './resources/newfile'. $o_ext);
# 释放资源
if (isset($logo_img))
{
imagedestroy($logo_img);
}
imagedestroy($img);
}
}