1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* elFinder Plugin AutoRotate. |
4
|
|
|
* |
5
|
|
|
* Auto rotation on file upload of JPEG file by EXIF Orientation. |
6
|
|
|
* |
7
|
|
|
* ex. binding, configure on connector options |
8
|
|
|
* $opts = array( |
9
|
|
|
* 'bind' => array( |
10
|
|
|
* 'upload.presave' => array( |
11
|
|
|
* 'Plugin.AutoRotate.onUpLoadPreSave' |
12
|
|
|
* ) |
13
|
|
|
* ), |
14
|
|
|
* // global configure (optional) |
15
|
|
|
* 'plugin' => array( |
16
|
|
|
* 'AutoRotate' => array( |
17
|
|
|
* 'enable' => true, // For control by volume driver |
18
|
|
|
* 'quality' => 95, // JPEG image save quality |
19
|
|
|
* 'offDropWith' => null // To disable it if it is dropped with pressing the meta key |
20
|
|
|
* // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
21
|
|
|
* // In case of using any key, specify it as an array |
22
|
|
|
* ) |
23
|
|
|
* ), |
24
|
|
|
* // each volume configure (optional) |
25
|
|
|
* 'roots' => array( |
26
|
|
|
* array( |
27
|
|
|
* 'driver' => 'LocalFileSystem', |
28
|
|
|
* 'path' => '/path/to/files/', |
29
|
|
|
* 'URL' => 'http://localhost/to/files/' |
30
|
|
|
* 'plugin' => array( |
31
|
|
|
* 'AutoRotate' => array( |
32
|
|
|
* 'enable' => true, // For control by volume driver |
33
|
|
|
* 'quality' => 95, // JPEG image save quality |
34
|
|
|
* 'offDropWith' => null // To disable it if it is dropped with pressing the meta key |
35
|
|
|
* // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
36
|
|
|
* // In case of using any key, specify it as an array |
37
|
|
|
* ) |
38
|
|
|
* ) |
39
|
|
|
* ) |
40
|
|
|
* ) |
41
|
|
|
* ); |
42
|
|
|
* |
43
|
|
|
* @author Naoki Sawada |
44
|
|
|
* @license New BSD |
45
|
|
|
*/ |
46
|
|
|
class elFinderPluginAutoRotate extends elFinderPlugin |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
public function __construct($opts) |
49
|
|
|
{ |
50
|
|
|
$defaults = [ |
51
|
|
|
'enable' => true, // For control by volume driver |
52
|
|
|
'quality' => 95, // JPEG image save quality |
53
|
|
|
'offDropWith' => null, // To disable it if it is dropped with pressing the meta key |
54
|
|
|
// Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value |
55
|
|
|
// In case of using any key, specify it as an array |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$this->opts = array_merge($defaults, $opts); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$opts = $this->getCurrentOpts($volume); |
64
|
|
|
|
65
|
|
|
if (! $this->iaEnabled($opts)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$mime = mime_content_type($src); |
70
|
|
|
if (substr($mime, 0, 5) !== 'image') { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$srcImgInfo = getimagesize($src); |
75
|
|
|
if ($srcImgInfo === false) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// check target image type |
80
|
|
|
if ($srcImgInfo[2] !== IMAGETYPE_JPEG) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->rotate($volume, $src, $srcImgInfo, $opts['quality']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function rotate($volume, $src, $srcImgInfo, $quality) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
if (! function_exists('exif_read_data')) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
$degree = 0; |
93
|
|
|
$exif = exif_read_data($src); |
94
|
|
|
if ($exif && ! empty($exif['Orientation'])) { |
|
|
|
|
95
|
|
|
switch ($exif['Orientation']) { |
96
|
|
|
case 8: |
97
|
|
|
$degree = 270; |
98
|
|
|
break; |
99
|
|
|
case 3: |
100
|
|
|
$degree = 180; |
101
|
|
|
break; |
102
|
|
|
case 6: |
103
|
|
|
$degree = 90; |
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$opts = [ |
108
|
|
|
'degree' => $degree, |
109
|
|
|
'jpgQuality' => $quality, |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
return $volume->imageUtil('rotate', $src, $opts); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.