elFinderPluginAutoResize   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 4.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 3
loc 67
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
D onUpLoadPreSave() 3 36 9
A resize() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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...
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)
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...
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]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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