1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* elFinder Plugin AutoResize |
4
|
|
|
* |
5
|
|
|
* Auto resize on file upload. |
6
|
|
|
* |
7
|
|
|
* ex. binding, configure on connector options |
8
|
|
|
* $opts = array( |
9
|
|
|
* 'bind' => array( |
10
|
|
|
* 'upload.presave' => array( |
11
|
|
|
* 'Plugin.AutoResize.onUpLoadPreSave' |
12
|
|
|
* ) |
13
|
|
|
* ), |
14
|
|
|
* // global configure (optional) |
15
|
|
|
* 'plugin' => array( |
16
|
|
|
* 'AutoResize' => array( |
17
|
|
|
* 'enable' => true, // For control by volume driver |
18
|
|
|
* 'maxWidth' => 1024, // Path to Water mark image |
19
|
|
|
* 'maxHeight' => 1024, // Margin right pixel |
20
|
|
|
* 'quality' => 95, // JPEG image save quality |
21
|
|
|
* 'preserveExif' => false, // Preserve EXIF data (Imagick only) |
22
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field ) |
23
|
|
|
* ) |
24
|
|
|
* ), |
25
|
|
|
* // each volume configure (optional) |
26
|
|
|
* 'roots' => array( |
27
|
|
|
* array( |
28
|
|
|
* 'driver' => 'LocalFileSystem', |
29
|
|
|
* 'path' => '/path/to/files/', |
30
|
|
|
* 'URL' => 'http://localhost/to/files/' |
31
|
|
|
* 'plugin' => array( |
32
|
|
|
* 'AutoResize' => array( |
33
|
|
|
* 'enable' => true, // For control by volume driver |
34
|
|
|
* 'maxWidth' => 1024, // Path to Water mark image |
35
|
|
|
* 'maxHeight' => 1024, // Margin right pixel |
36
|
|
|
* 'quality' => 95, // JPEG image save quality |
37
|
|
|
* 'preserveExif' => false, // Preserve EXIF data (Imagick only) |
38
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field ) |
39
|
|
|
* ) |
40
|
|
|
* ) |
41
|
|
|
* ) |
42
|
|
|
* ) |
43
|
|
|
* ); |
44
|
|
|
* |
45
|
|
|
* @package elfinder |
46
|
|
|
* @author Naoki Sawada |
47
|
|
|
* @license New BSD |
48
|
|
|
*/ |
49
|
|
|
class elFinderPluginAutoResize { |
|
|
|
|
50
|
|
|
|
51
|
|
|
private $opts = array(); |
52
|
|
|
|
53
|
|
|
public function __construct($opts) { |
54
|
|
|
$defaults = array( |
55
|
|
|
'enable' => true, // For control by volume driver |
56
|
|
|
'maxWidth' => 1024, // Path to Water mark image |
57
|
|
|
'maxHeight' => 1024, // Margin right pixel |
58
|
|
|
'quality' => 95, // JPEG image save quality |
59
|
|
|
'preserveExif' => false, // Preserve EXIF data (Imagick only) |
60
|
|
|
'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field ) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->opts = array_merge($defaults, $opts); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) { |
|
|
|
|
68
|
|
|
$opts = $this->opts; |
69
|
|
|
$volOpts = $volume->getOptionsPlugin('AutoResize'); |
70
|
|
|
if (is_array($volOpts)) { |
71
|
|
|
$opts = array_merge($this->opts, $volOpts); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! $opts['enable']) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$srcImgInfo = @getimagesize($src); |
79
|
|
|
if ($srcImgInfo === false) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// check target image type |
84
|
|
|
$imgTypes = array( |
85
|
|
|
IMAGETYPE_GIF => IMG_GIF, |
86
|
|
|
IMAGETYPE_JPEG => IMG_JPEG, |
87
|
|
|
IMAGETYPE_PNG => IMG_PNG, |
88
|
|
|
IMAGETYPE_WBMP => IMG_WBMP, |
89
|
|
|
); |
90
|
|
|
if (! ($opts['targetType'] & $imgTypes[$srcImgInfo[2]])) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($srcImgInfo[0] > $opts['maxWidth'] || $srcImgInfo[1] > $opts['maxHeight']) { |
95
|
|
|
return $this->resize($src, $srcImgInfo, $opts['maxWidth'], $opts['maxHeight'], $opts['quality'], $opts['preserveExif']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function resize($src, $srcImgInfo, $maxWidth, $maxHeight, $quality, $preserveExif) { |
102
|
|
|
$zoom = min(($maxWidth/$srcImgInfo[0]),($maxHeight/$srcImgInfo[1])); |
103
|
|
|
$width = round($srcImgInfo[0] * $zoom); |
104
|
|
|
$height = round($srcImgInfo[1] * $zoom); |
105
|
|
|
|
106
|
|
|
if (class_exists('Imagick', false)) { |
107
|
|
|
return $this->resize_imagick($src, $width, $height, $quality, $preserveExif); |
108
|
|
|
} else { |
109
|
|
|
return $this->resize_gd($src, $width, $height, $quality, $srcImgInfo); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function resize_gd($src, $width, $height, $quality, $srcImgInfo) { |
114
|
|
View Code Duplication |
switch ($srcImgInfo['mime']) { |
|
|
|
|
115
|
|
|
case 'image/gif': |
116
|
|
|
if (@imagetypes() & IMG_GIF) { |
117
|
|
|
$oSrcImg = @imagecreatefromgif($src); |
118
|
|
|
} else { |
119
|
|
|
$ermsg = 'GIF images are not supported'; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
break; |
122
|
|
|
case 'image/jpeg': |
123
|
|
|
if (@imagetypes() & IMG_JPG) { |
124
|
|
|
$oSrcImg = @imagecreatefromjpeg($src) ; |
125
|
|
|
} else { |
126
|
|
|
$ermsg = 'JPEG images are not supported'; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
break; |
129
|
|
|
case 'image/png': |
130
|
|
|
if (@imagetypes() & IMG_PNG) { |
131
|
|
|
$oSrcImg = @imagecreatefrompng($src) ; |
132
|
|
|
} else { |
133
|
|
|
$ermsg = 'PNG images are not supported'; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
break; |
136
|
|
|
case 'image/wbmp': |
137
|
|
|
if (@imagetypes() & IMG_WBMP) { |
138
|
|
|
$oSrcImg = @imagecreatefromwbmp($src); |
139
|
|
|
} else { |
140
|
|
|
$ermsg = 'WBMP images are not supported'; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
break; |
143
|
|
|
default: |
144
|
|
|
$oSrcImg = false; |
145
|
|
|
$ermsg = $srcImgInfo['mime'].' images are not supported'; |
|
|
|
|
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($oSrcImg && false != ($tmp = imagecreatetruecolor($width, $height))) { |
150
|
|
|
|
151
|
|
|
if (!imagecopyresampled($tmp, $oSrcImg, 0, 0, 0, 0, $width, $height, $srcImgInfo[0], $srcImgInfo[1])) { |
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
switch ($srcImgInfo['mime']) { |
|
|
|
|
156
|
|
|
case 'image/gif': |
157
|
|
|
imagegif($tmp, $src); |
158
|
|
|
break; |
159
|
|
|
case 'image/jpeg': |
160
|
|
|
imagejpeg($tmp, $src, $quality); |
161
|
|
|
break; |
162
|
|
|
case 'image/png': |
163
|
|
|
if (function_exists('imagesavealpha') && function_exists('imagealphablending')) { |
164
|
|
|
imagealphablending($tmp, false); |
165
|
|
|
imagesavealpha($tmp, true); |
166
|
|
|
} |
167
|
|
|
imagepng($tmp, $src); |
168
|
|
|
break; |
169
|
|
|
case 'image/wbmp': |
170
|
|
|
imagewbmp($tmp, $src); |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
imagedestroy($oSrcImg); |
175
|
|
|
imagedestroy($tmp); |
176
|
|
|
|
177
|
|
|
return true; |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function resize_imagick($src, $width, $height, $quality, $preserveExif) { |
184
|
|
|
try { |
185
|
|
|
$img = new imagick($src); |
186
|
|
|
|
187
|
|
View Code Duplication |
if (strtoupper($img->getImageFormat()) === 'JPEG') { |
|
|
|
|
188
|
|
|
$img->setImageCompression(imagick::COMPRESSION_JPEG); |
189
|
|
|
$img->setImageCompressionQuality($quality); |
190
|
|
|
if (!$preserveExif) { |
191
|
|
|
try { |
192
|
|
|
$orientation = $img->getImageOrientation(); |
193
|
|
|
} catch (ImagickException $e) { |
194
|
|
|
$orientation = 0; |
195
|
|
|
} |
196
|
|
|
$img->stripImage(); |
197
|
|
|
if ($orientation) { |
198
|
|
|
$img->setImageOrientation($orientation); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$img->resizeImage($width, $height, Imagick::FILTER_LANCZOS, true); |
204
|
|
|
|
205
|
|
|
$result = $img->writeImage($src); |
206
|
|
|
|
207
|
|
|
$img->clear(); |
208
|
|
|
$img->destroy(); |
209
|
|
|
|
210
|
|
|
return $result ? true : false; |
211
|
|
|
} catch (Exception $e) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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.