This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWatermark.class.php
386 lines (348 loc) · 13.5 KB
/
Watermark.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
<?php
/**
* ThumbGen Watermark Plugin
*
* This class adds a watermark to the generated thumbnails
*
* LICENSE: CC BY-NC-SA 3.0
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* Requires PHP 5.2+
*
* @package ThumbGen
* @subpackage Plugin
* @version 1.1.1
* @link https://github.com/avataru/ThumbGen
* @author Mihai Zaharie <[email protected]>
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/ CC BY-NC-SA 3.0
*/
namespace ThumbGen;
require_once('Plugin.class.php');
class Watermark extends Plugin
{
/**
* Watermark image file
*
* @var string
*/
protected $watermarkImage = null;
/**
* Watermark dimensions
*
* @var array of width and height
*/
protected $watermarkDimensions = array(
'width' => 20,
'height' => 10
);
/**
* Watermark position
*
* @var array of horizontal and vertical alignment, x and y
*/
protected $watermarkPosition = array(
'hAlign' => 'right',
'vAlign' => 'bottom',
'x' => 5,
'y' => 5
);
/**
* Watermark pattern repetition
* Type values: no-repeat, repeat-x, repeat-y, repeat-xy
*
* @var array of type and padding
*/
protected $watermarkRepetition = array(
'type' => 'no-repeat',
'paddingX' => 0,
'paddingY' => 0
);
/**
* Watermark opacity (percentage)
* Values: 0-100
*
* @var integer
*/
protected $watermarkOpacity = 75;
/**
* Adds the watermark to the thumbnail
*
* @param ThumbGen $thumbGen ThumbGen object
* @return boolean Returns true if the watermark was applied sucessfully
*/
public function addWatermark(\ThumbGen $thumbGen)
{
$thumbnail = imagecreatefromstring($thumbGen->getThumbnailData());
$width = $thumbGen->thumbnailDimensions['width'];
$height = $thumbGen->thumbnailDimensions['height'];
if ($thumbnail != null)
{
$imWatermark = imagecreatefrompng($this->watermarkImage);
imagealphablending($imWatermark, false);
imagesavealpha($imWatermark, true);
list($watermarkSourceWidth, $watermarkSourceHeight) = getimagesize($this->watermarkImage);
$thumbnailWidth = ($width != null && is_int($width) && $width > 0) ? $width : $this->thumbnailDimensions['width'];
$thumbnailHeight = ($height != null && is_int($height) && $height > 0) ? $height : $this->thumbnailDimensions['height'];
if ($this->watermarkPosition['hAlign'] == 'right')
{
$position['x'] = $thumbnailWidth - $this->watermarkDimensions['width'] - $this->watermarkPosition['x'];
}
elseif ($this->watermarkPosition['hAlign'] == 'center')
{
$position['x'] = round($thumbnailWidth / 2) - round($this->watermarkDimensions['width'] / 2) + $this->watermarkPosition['x'];
}
else
{
$position['x'] = $this->watermarkPosition['x'];
}
if ($this->watermarkPosition['vAlign'] == 'bottom')
{
$position['y'] = $thumbnailHeight - $this->watermarkDimensions['height'] - $this->watermarkPosition['y'];
}
elseif ($this->watermarkPosition['vAlign'] == 'middle')
{
$position['y'] = round($thumbnailHeight / 2) - round($this->watermarkDimensions['height'] / 2) + $this->watermarkPosition['y'];
}
else
{
$position['y'] = $this->watermarkPosition['y'];
}
// Opacity
$imTransparentWatermark = imagecreatetruecolor($watermarkSourceWidth, $watermarkSourceHeight);
imagealphablending($imTransparentWatermark, false);
imagesavealpha($imTransparentWatermark, true);
$this->imagecopymergealpha($imTransparentWatermark, $imWatermark, 0, 0, 0, 0, $watermarkSourceWidth, $watermarkSourceHeight, $this->watermarkOpacity);
// Repetition
switch ($this->watermarkRepetition['type'])
{
case 'repeat-x':
for ($position['x'] = 0; $position['x'] < $thumbnailWidth; $position['x'] = $position['x'] + $this->watermarkDimensions['width'] + $this->watermarkRepetition['paddingX'])
{
imagecopyresampled($thumbnail, $imTransparentWatermark, $position['x'], $position['y'], 0, 0, $this->watermarkDimensions['width'], $this->watermarkDimensions['height'], $watermarkSourceWidth, $watermarkSourceHeight);
}
break;
case 'repeat-y':
for ($position['y'] = 0; $position['y'] < $thumbnailHeight; $position['y'] = $position['y'] + $this->watermarkDimensions['height'] + $this->watermarkRepetition['paddingY'])
{
imagecopyresampled($thumbnail, $imTransparentWatermark, $position['x'], $position['y'], 0, 0, $this->watermarkDimensions['width'], $this->watermarkDimensions['height'], $watermarkSourceWidth, $watermarkSourceHeight);
}
break;
case 'repeat-xy':
for ($position['x'] = 0; $position['x'] < $thumbnailWidth; $position['x'] = $position['x'] + $this->watermarkDimensions['width'] + $this->watermarkRepetition['paddingX'])
{
for ($position['y'] = 0; $position['y'] < $thumbnailHeight; $position['y'] = $position['y'] + $this->watermarkDimensions['height'] + $this->watermarkRepetition['paddingY'])
{
imagecopyresampled($thumbnail, $imTransparentWatermark, $position['x'], $position['y'], 0, 0, $this->watermarkDimensions['width'], $this->watermarkDimensions['height'], $watermarkSourceWidth, $watermarkSourceHeight);
}
}
break;
case 'no-repeat':
default:
imagecopyresampled($thumbnail, $imTransparentWatermark, $position['x'], $position['y'], 0, 0, $this->watermarkDimensions['width'], $this->watermarkDimensions['height'], $watermarkSourceWidth, $watermarkSourceHeight);
}
// Save the image data for future processing
ob_start();
imagegd2($thumbnail, null, null, IMG_GD2_RAW);
$this->thumbnail = ob_get_clean();
return true;
}
return false;
}
/**
* Sets the watermark
*
* @param string $watermark OPTIONAL Watermark image path
* @param array $dimensions OPTIONAL Watermark width and height in pixels
* @param array $position OPTIONAL Watermark vertical and horizontal alignment and x and y coordinates
* @param mixed $repetition OPTIONAL Watermark repetition type and padding if needed
* @return bool
*/
public function setWatermark($watermark = null, $opacity = null, array $dimensions = null, array $position = null, $repetition = null)
{
if ($watermark != null && !$this->setWatermarkImage($watermark))
{
$this->throwError('The watermark file does not exist or is not a valid PNG image');
return false;
}
if ($opacity != null && !$this->setWatermarkOpacity($opacity))
{
$this->throwError('The watermark opacity is not valid');
return false;
}
if ($dimensions != null && !$this->setWatermarkDimensions($dimensions[0], $dimensions[1]))
{
$this->throwError('The watermark dimensions are not valid');
return false;
}
if ($position != null && !$this->setWatermarkPosition($position[0], $position[1], $position[2], $position[3]))
{
$this->throwError('The watermark position is not valid');
return false;
}
if ($repetition != null)
{
if (
(is_array($repetition) && !$this->setWatermarkRepetition($repetition[0], $repetition[1], $repetition[2]))
|| ($repetition == 'no-repeat' && !$this->setWatermarkRepetition($repetition))
){
$this->throwError('The watermark repetition is not valid');
return false;
}
}
return true;
}
/**
* Sets the watermark image
*
* @param string $watermark Watermark image path
* @return bool
*/
public function setWatermarkImage($watermark)
{
if (file_exists($watermark))
{
$imageInfo = getimagesize($watermark);
if ($imageInfo && $imageInfo['2'] == IMAGETYPE_PNG)
{
$this->watermarkImage = $watermark;
return true;
}
}
return false;
}
/**
* Sets the watermark dimensions
*
* @param integer $width Watermark width in pixels
* @param integer $height Watermark height in pixels
* @return bool
*/
public function setWatermarkDimensions($width, $height)
{
if (is_int($width) && is_int($height) && $width > 0 && $height > 0)
{
$this->watermarkDimensions['width'] = $width;
$this->watermarkDimensions['height'] = $height;
return true;
}
return false;
}
/**
* Sets the watermark position
*
* @param string $vAlign Vertical watermark alignment (top, middle or bottom)
* @param string $hAlign Horizontal watermark alignment (left, center or right)
* @param integer $x Watermark x coordinate
* @param integer $y Watermark y coordinate
* @return bool
*/
public function setWatermarkPosition($hAlign, $vAlign, $x, $y)
{
if (in_array($hAlign, array('left', 'center', 'right')) && in_array($vAlign, array('top', 'middle', 'bottom')) && is_int($x) && is_int($y))
{
$this->watermarkPosition = array(
'hAlign' => $hAlign,
'vAlign' => $vAlign,
'x' => $x,
'y' => $y
);
return true;
}
return false;
}
/**
* Sets the watermark opacity
*
* @param integer $opacity Watermark opacity in percents
* @return bool
*/
public function setWatermarkOpacity($opacity)
{
if (is_int($opacity) && $opacity >= 0 && $opacity <= 100)
{
$this->watermarkOpacity = $opacity;
return true;
}
return false;
}
/**
* Sets the watermark repetition
*
* @param string $repetition Watermark repetition
* @return bool
*/
public function setWatermarkRepetition($type, $paddingX = 0, $paddingY = 0)
{
if (in_array($type, array('no-repeat', 'repeat-x', 'repeat-y', 'repeat-xy')) && is_int($paddingX) && is_int($paddingY))
{
$this->watermarkRepetition = array(
'type' => $type,
'paddingX' => $paddingX,
'paddingY' => $paddingY
);
return true;
}
return false;
}
/**
* PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
* This is a function like imagecopymerge but it handle alpha channel well!!!
**/
// A fix to get a function like imagecopymerge WITH ALPHA SUPPORT
// Main script by aiden dot mail at freemail dot hu
// Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com
private function imagecopymergealpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
if(!isset($pct))
{
return false;
}
$pct /= 100;
// Get image width and height
$w = imagesx($src_im);
$h = imagesy($src_im);
// Turn alpha blending off
imagealphablending($src_im, false);
// Find the most opaque pixel in the image (the one with the smallest alpha value)
$minalpha = 127;
for($x = 0; $x < $w; $x++)
{
for($y = 0; $y < $h; $y++)
{
$alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF;
if($alpha < $minalpha)
{
$minalpha = $alpha;
}
}
}
// Loop through image pixels and modify alpha for each
for($x = 0; $x < $w; $x++)
{
for($y = 0; $y < $h; $y++)
{
// Get current alpha value (represents the TANSPARENCY!)
$colorxy = imagecolorat($src_im, $x, $y);
$alpha = ($colorxy >> 24) & 0xFF;
// Calculate new alpha
if ($minalpha !== 127)
{
$alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
}
else
{
$alpha += 127 * $pct;
}
// Get the color index with new alpha
$alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);
// Set pixel with the new color + opacity
if(!imagesetpixel($src_im, $x, $y, $alphacolorxy))
{
return false;
}
}
}
// The image copy
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}
}