elFinderPluginAutoRotate::onUpLoadPreSave()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 5
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $thash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $srcImgInfo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exif of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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