|
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
|
|
|
* 'forceEffect' => false, // For change quality of small images |
|
23
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) |
|
24
|
|
|
* 'offDropWith' => null // To disable it if it is dropped with pressing the meta key |
|
25
|
|
|
* // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
|
26
|
|
|
* // In case of using any key, specify it as an array |
|
27
|
|
|
* ) |
|
28
|
|
|
* ), |
|
29
|
|
|
* // each volume configure (optional) |
|
30
|
|
|
* 'roots' => array( |
|
31
|
|
|
* array( |
|
32
|
|
|
* 'driver' => 'LocalFileSystem', |
|
33
|
|
|
* 'path' => '/path/to/files/', |
|
34
|
|
|
* 'URL' => 'http://localhost/to/files/' |
|
35
|
|
|
* 'plugin' => array( |
|
36
|
|
|
* 'AutoResize' => array( |
|
37
|
|
|
* 'enable' => true, // For control by volume driver |
|
38
|
|
|
* 'maxWidth' => 1024, // Path to Water mark image |
|
39
|
|
|
* 'maxHeight' => 1024, // Margin right pixel |
|
40
|
|
|
* 'quality' => 95, // JPEG image save quality |
|
41
|
|
|
* 'preserveExif' => false, // Preserve EXIF data (Imagick only) |
|
42
|
|
|
* 'forceEffect' => false, // For change quality of small images |
|
43
|
|
|
* 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) |
|
44
|
|
|
* 'offDropWith' => null // To disable it if it is dropped with pressing the meta key |
|
45
|
|
|
* // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
|
46
|
|
|
* // In case of using any key, specify it as an array |
|
47
|
|
|
* ) |
|
48
|
|
|
* ) |
|
49
|
|
|
* ) |
|
50
|
|
|
* ) |
|
51
|
|
|
* ); |
|
52
|
|
|
* |
|
53
|
|
|
* @author Naoki Sawada |
|
54
|
|
|
* @license New BSD |
|
55
|
|
|
*/ |
|
56
|
|
|
class elFinderPluginAutoResize extends elFinderPlugin |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
public function __construct($opts) |
|
59
|
|
|
{ |
|
60
|
|
|
$defaults = [ |
|
61
|
|
|
'enable' => true, // For control by volume driver |
|
62
|
|
|
'maxWidth' => 1024, // Path to Water mark image |
|
63
|
|
|
'maxHeight' => 1024, // Margin right pixel |
|
64
|
|
|
'quality' => 95, // JPEG image save quality |
|
65
|
|
|
'preserveExif' => false, // Preserve EXIF data (Imagick only) |
|
66
|
|
|
'forceEffect' => false, // For change quality of small images |
|
67
|
|
|
'targetType' => IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP, // Target image formats ( bit-field ) |
|
68
|
|
|
'offDropWith' => null, // To disable it if it is dropped with pressing the meta key |
|
69
|
|
|
// Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
|
70
|
|
|
// In case of using any key, specify it as an array |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
$this->opts = array_merge($defaults, $opts); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$opts = $this->getCurrentOpts($volume); |
|
79
|
|
|
|
|
80
|
|
|
if (! $this->iaEnabled($opts)) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$mime = mime_content_type($src); |
|
85
|
|
|
if (substr($mime, 0, 5) !== 'image') { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$srcImgInfo = getimagesize($src); |
|
90
|
|
|
if ($srcImgInfo === false) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// check target image type |
|
95
|
|
|
$imgTypes = [ |
|
96
|
|
|
IMAGETYPE_GIF => IMG_GIF, |
|
97
|
|
|
IMAGETYPE_JPEG => IMG_JPEG, |
|
98
|
|
|
IMAGETYPE_PNG => IMG_PNG, |
|
99
|
|
|
IMAGETYPE_BMP => IMG_WBMP, |
|
100
|
|
|
IMAGETYPE_WBMP => IMG_WBMP, |
|
101
|
|
|
]; |
|
102
|
|
View Code Duplication |
if (! isset($imgTypes[$srcImgInfo[2]]) || ! ($opts['targetType'] & $imgTypes[$srcImgInfo[2]])) { |
|
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($opts['forceEffect'] || $srcImgInfo[0] > $opts['maxWidth'] || $srcImgInfo[1] > $opts['maxHeight']) { |
|
107
|
|
|
return $this->resize($volume, $src, $srcImgInfo, $opts['maxWidth'], $opts['maxHeight'], $opts['quality'], $opts['preserveExif']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function resize($volume, $src, $srcImgInfo, $maxWidth, $maxHeight, $jpgQuality, $preserveExif) |
|
114
|
|
|
{ |
|
115
|
|
|
$zoom = min(($maxWidth / $srcImgInfo[0]), ($maxHeight / $srcImgInfo[1])); |
|
116
|
|
|
$width = round($srcImgInfo[0] * $zoom); |
|
117
|
|
|
$height = round($srcImgInfo[1] * $zoom); |
|
118
|
|
|
$unenlarge = true; |
|
119
|
|
|
|
|
120
|
|
|
return $volume->imageUtil('resize', $src, compact('width', 'height', 'jpgQuality', 'preserveExif', 'unenlarge')); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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.