|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* elFinder Plugin Watermark |
|
4
|
|
|
* |
|
5
|
|
|
* Print watermark on file upload. |
|
6
|
|
|
* |
|
7
|
|
|
* ex. binding, configure on connector options |
|
8
|
|
|
* $opts = array( |
|
9
|
|
|
* 'bind' => array( |
|
10
|
|
|
* 'upload.presave' => array( |
|
11
|
|
|
* 'Plugin.Watermark.onUpLoadPreSave' |
|
12
|
|
|
* ) |
|
13
|
|
|
* ), |
|
14
|
|
|
* // global configure (optional) |
|
15
|
|
|
* 'plugin' => array( |
|
16
|
|
|
* 'Watermark' => array( |
|
17
|
|
|
* 'enable' => true, // For control by volume driver |
|
18
|
|
|
* 'source' => 'logo.png', // Path to Water mark image |
|
19
|
|
|
* 'marginRight' => 5, // Margin right pixel |
|
20
|
|
|
* 'marginBottom' => 5, // Margin bottom pixel |
|
21
|
|
|
* 'quality' => 95, // JPEG image save quality |
|
22
|
|
|
* 'transparency' => 70, // Water mark image transparency ( other than PNG ) |
|
23
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) |
|
24
|
|
|
* 'targetMinPixel' => 200 // Target image minimum pixel size |
|
25
|
|
|
* ) |
|
26
|
|
|
* ), |
|
27
|
|
|
* // each volume configure (optional) |
|
28
|
|
|
* 'roots' => array( |
|
29
|
|
|
* array( |
|
30
|
|
|
* 'driver' => 'LocalFileSystem', |
|
31
|
|
|
* 'path' => '/path/to/files/', |
|
32
|
|
|
* 'URL' => 'http://localhost/to/files/' |
|
33
|
|
|
* 'plugin' => array( |
|
34
|
|
|
* 'Watermark' => array( |
|
35
|
|
|
* 'enable' => true, // For control by volume driver |
|
36
|
|
|
* 'source' => 'logo.png', // Path to Water mark image |
|
37
|
|
|
* 'marginRight' => 5, // Margin right pixel |
|
38
|
|
|
* 'marginBottom' => 5, // Margin bottom pixel |
|
39
|
|
|
* 'quality' => 95, // JPEG image save quality |
|
40
|
|
|
* 'transparency' => 70, // Water mark image transparency ( other than PNG ) |
|
41
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) |
|
42
|
|
|
* 'targetMinPixel' => 200 // Target image minimum pixel size |
|
43
|
|
|
* ) |
|
44
|
|
|
* ) |
|
45
|
|
|
* ) |
|
46
|
|
|
* ) |
|
47
|
|
|
* ); |
|
48
|
|
|
* |
|
49
|
|
|
* @package elfinder |
|
50
|
|
|
* @author Naoki Sawada |
|
51
|
|
|
* @license New BSD |
|
52
|
|
|
*/ |
|
53
|
|
|
class elFinderPluginWatermark { |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
private $opts = array(); |
|
56
|
|
|
private $watermarkImgInfo = null; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public function __construct($opts) { |
|
59
|
|
|
$defaults = array( |
|
60
|
|
|
'enable' => true, // For control by volume driver |
|
61
|
|
|
'source' => 'logo.png', // Path to Water mark image |
|
62
|
|
|
'marginRight' => 5, // Margin right pixel |
|
63
|
|
|
'marginBottom' => 5, // Margin bottom pixel |
|
64
|
|
|
'quality' => 95, // JPEG image save quality |
|
65
|
|
|
'transparency' => 70, // Water mark image transparency ( other than PNG ) |
|
66
|
|
|
'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) |
|
67
|
|
|
'targetMinPixel' => 200 // Target image minimum pixel size |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$this->opts = array_merge($defaults, $opts); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) { |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$opts = $this->opts; |
|
77
|
|
|
$volOpts = $volume->getOptionsPlugin('Watermark'); |
|
78
|
|
|
if (is_array($volOpts)) { |
|
79
|
|
|
$opts = array_merge($this->opts, $volOpts); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (! $opts['enable']) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$srcImgInfo = @getimagesize($src); |
|
87
|
|
|
if ($srcImgInfo === false) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// check Animation Gif |
|
92
|
|
|
if (elFinder::isAnimationGif($src)) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// check water mark image |
|
97
|
|
|
if (! file_exists($opts['source'])) { |
|
98
|
|
|
$opts['source'] = dirname(__FILE__) . "/" . $opts['source']; |
|
99
|
|
|
} |
|
100
|
|
|
if (is_readable($opts['source'])) { |
|
101
|
|
|
$watermarkImgInfo = @getimagesize($opts['source']); |
|
102
|
|
|
if (! $watermarkImgInfo) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$watermark = $opts['source']; |
|
110
|
|
|
$marginLeft = $opts['marginRight']; |
|
111
|
|
|
$marginBottom = $opts['marginBottom']; |
|
112
|
|
|
$quality = $opts['quality']; |
|
113
|
|
|
$transparency = $opts['transparency']; |
|
114
|
|
|
|
|
115
|
|
|
// check target image type |
|
116
|
|
|
$imgTypes = array( |
|
117
|
|
|
IMAGETYPE_GIF => IMG_GIF, |
|
118
|
|
|
IMAGETYPE_JPEG => IMG_JPEG, |
|
119
|
|
|
IMAGETYPE_PNG => IMG_PNG, |
|
120
|
|
|
IMAGETYPE_WBMP => IMG_WBMP, |
|
121
|
|
|
); |
|
122
|
|
|
if (! ($opts['targetType'] & $imgTypes[$srcImgInfo[2]])) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// check target image size |
|
127
|
|
|
if ($opts['targetMinPixel'] > 0 && $opts['targetMinPixel'] > min($srcImgInfo[0], $srcImgInfo[1])) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$watermark_width = $watermarkImgInfo[0]; |
|
132
|
|
|
$watermark_height = $watermarkImgInfo[1]; |
|
133
|
|
|
$dest_x = $srcImgInfo[0] - $watermark_width - $marginLeft; |
|
134
|
|
|
$dest_y = $srcImgInfo[1] - $watermark_height - $marginBottom; |
|
135
|
|
|
|
|
136
|
|
|
if (class_exists('Imagick', false)) { |
|
137
|
|
|
return $this->watermarkPrint_imagick($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo); |
|
138
|
|
|
} else { |
|
139
|
|
|
return $this->watermarkPrint_gd($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo, $srcImgInfo); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function watermarkPrint_imagick($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo) { |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
try { |
|
146
|
|
|
// Open the original image |
|
147
|
|
|
$img = new Imagick($src); |
|
148
|
|
|
|
|
149
|
|
|
// Open the watermark |
|
150
|
|
|
$watermark = new Imagick($watermark); |
|
151
|
|
|
|
|
152
|
|
|
// Set transparency |
|
153
|
|
|
if (strtoupper($watermark->getImageFormat()) !== 'PNG') { |
|
154
|
|
|
$watermark->setImageOpacity($transparency/100); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Overlay the watermark on the original image |
|
158
|
|
|
$img->compositeImage($watermark, imagick::COMPOSITE_OVER, $dest_x, $dest_y); |
|
159
|
|
|
|
|
160
|
|
|
// Set quality |
|
161
|
|
|
if (strtoupper($img->getImageFormat()) === 'JPEG') { |
|
162
|
|
|
$img->setImageCompression(imagick::COMPRESSION_JPEG); |
|
163
|
|
|
$img->setCompressionQuality($quality); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$result = $img->writeImage($src); |
|
167
|
|
|
|
|
168
|
|
|
$img->clear(); |
|
169
|
|
|
$img->destroy(); |
|
170
|
|
|
$watermark->clear(); |
|
171
|
|
|
$watermark->destroy(); |
|
172
|
|
|
|
|
173
|
|
|
return $result ? true : false; |
|
174
|
|
|
} catch (Exception $e) { |
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function watermarkPrint_gd($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo, $srcImgInfo) { |
|
180
|
|
|
|
|
181
|
|
|
$watermark_width = $watermarkImgInfo[0]; |
|
182
|
|
|
$watermark_height = $watermarkImgInfo[1]; |
|
183
|
|
|
|
|
184
|
|
|
$ermsg = ''; |
|
185
|
|
View Code Duplication |
switch ($watermarkImgInfo['mime']) { |
|
|
|
|
|
|
186
|
|
|
case 'image/gif': |
|
187
|
|
|
if (@imagetypes() & IMG_GIF) { |
|
188
|
|
|
$oWatermarkImg = @imagecreatefromgif($watermark); |
|
189
|
|
|
} else { |
|
190
|
|
|
$ermsg = 'GIF images are not supported'; |
|
191
|
|
|
} |
|
192
|
|
|
break; |
|
193
|
|
|
case 'image/jpeg': |
|
194
|
|
|
if (@imagetypes() & IMG_JPG) { |
|
195
|
|
|
$oWatermarkImg = @imagecreatefromjpeg($watermark) ; |
|
196
|
|
|
} else { |
|
197
|
|
|
$ermsg = 'JPEG images are not supported'; |
|
198
|
|
|
} |
|
199
|
|
|
break; |
|
200
|
|
|
case 'image/png': |
|
201
|
|
|
if (@imagetypes() & IMG_PNG) { |
|
202
|
|
|
$oWatermarkImg = @imagecreatefrompng($watermark) ; |
|
203
|
|
|
} else { |
|
204
|
|
|
$ermsg = 'PNG images are not supported'; |
|
205
|
|
|
} |
|
206
|
|
|
break; |
|
207
|
|
|
case 'image/wbmp': |
|
208
|
|
|
if (@imagetypes() & IMG_WBMP) { |
|
209
|
|
|
$oWatermarkImg = @imagecreatefromwbmp($watermark); |
|
210
|
|
|
} else { |
|
211
|
|
|
$ermsg = 'WBMP images are not supported'; |
|
212
|
|
|
} |
|
213
|
|
|
break; |
|
214
|
|
|
default: |
|
215
|
|
|
$oWatermarkImg = false; |
|
216
|
|
|
$ermsg = $watermarkImgInfo['mime'].' images are not supported'; |
|
217
|
|
|
break; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (! $ermsg) { |
|
221
|
|
View Code Duplication |
switch ($srcImgInfo['mime']) { |
|
|
|
|
|
|
222
|
|
|
case 'image/gif': |
|
223
|
|
|
if (@imagetypes() & IMG_GIF) { |
|
224
|
|
|
$oSrcImg = @imagecreatefromgif($src); |
|
225
|
|
|
} else { |
|
226
|
|
|
$ermsg = 'GIF images are not supported'; |
|
227
|
|
|
} |
|
228
|
|
|
break; |
|
229
|
|
|
case 'image/jpeg': |
|
230
|
|
|
if (@imagetypes() & IMG_JPG) { |
|
231
|
|
|
$oSrcImg = @imagecreatefromjpeg($src) ; |
|
232
|
|
|
} else { |
|
233
|
|
|
$ermsg = 'JPEG images are not supported'; |
|
234
|
|
|
} |
|
235
|
|
|
break; |
|
236
|
|
|
case 'image/png': |
|
237
|
|
|
if (@imagetypes() & IMG_PNG) { |
|
238
|
|
|
$oSrcImg = @imagecreatefrompng($src) ; |
|
239
|
|
|
} else { |
|
240
|
|
|
$ermsg = 'PNG images are not supported'; |
|
241
|
|
|
} |
|
242
|
|
|
break; |
|
243
|
|
|
case 'image/wbmp': |
|
244
|
|
|
if (@imagetypes() & IMG_WBMP) { |
|
245
|
|
|
$oSrcImg = @imagecreatefromwbmp($src); |
|
246
|
|
|
} else { |
|
247
|
|
|
$ermsg = 'WBMP images are not supported'; |
|
248
|
|
|
} |
|
249
|
|
|
break; |
|
250
|
|
|
default: |
|
251
|
|
|
$oSrcImg = false; |
|
252
|
|
|
$ermsg = $srcImgInfo['mime'].' images are not supported'; |
|
253
|
|
|
break; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if ($ermsg || false === $oSrcImg || false === $oWatermarkImg) { |
|
|
|
|
|
|
258
|
|
|
return false; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
if ($srcImgInfo['mime'] === 'image/png') { |
|
262
|
|
|
if (function_exists('imagecolorallocatealpha')) { |
|
263
|
|
|
$bg = imagecolorallocatealpha($oSrcImg, 255, 255, 255, 127); |
|
264
|
|
|
imagefill($oSrcImg, 0, 0 , $bg); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if ($watermarkImgInfo['mime'] === 'image/png') { |
|
269
|
|
|
imagecopy($oSrcImg, $oWatermarkImg, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); |
|
270
|
|
|
} else { |
|
271
|
|
|
imagecopymerge($oSrcImg, $oWatermarkImg, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $transparency); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
View Code Duplication |
switch ($srcImgInfo['mime']) { |
|
|
|
|
|
|
275
|
|
|
case 'image/gif': |
|
276
|
|
|
imagegif($oSrcImg, $src); |
|
277
|
|
|
break; |
|
278
|
|
|
case 'image/jpeg': |
|
279
|
|
|
imagejpeg($oSrcImg, $src, $quality); |
|
280
|
|
|
break; |
|
281
|
|
|
case 'image/png': |
|
282
|
|
|
if (function_exists('imagesavealpha') && function_exists('imagealphablending')) { |
|
283
|
|
|
imagealphablending($oSrcImg, false); |
|
284
|
|
|
imagesavealpha($oSrcImg, true); |
|
285
|
|
|
} |
|
286
|
|
|
imagepng($oSrcImg, $src); |
|
287
|
|
|
break; |
|
288
|
|
|
case 'image/wbmp': |
|
289
|
|
|
imagewbmp($oSrcImg, $src); |
|
290
|
|
|
break; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
imageDestroy($oSrcImg); |
|
294
|
|
|
imageDestroy($oWatermarkImg); |
|
295
|
|
|
|
|
296
|
|
|
return true; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.