elFinderPluginAutoResize::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4286
cc 1
eloc 9
nc 1
nop 1
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 {
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...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $path 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...
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']) {
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...
115
			case 'image/gif':
116
				if (@imagetypes() & IMG_GIF) {
117
					$oSrcImg = @imagecreatefromgif($src);
118
				} else {
119
					$ermsg = 'GIF images are not supported';
0 ignored issues
show
Unused Code introduced by
$ermsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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';
0 ignored issues
show
Unused Code introduced by
$ermsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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';
0 ignored issues
show
Unused Code introduced by
$ermsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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';
0 ignored issues
show
Unused Code introduced by
$ermsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
				}
142
				break;
143
			default:
144
				$oSrcImg = false;
145
				$ermsg = $srcImgInfo['mime'].' images are not supported';
0 ignored issues
show
Unused Code introduced by
$ermsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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])) {
0 ignored issues
show
Bug introduced by
The variable $oSrcImg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
152
				return false;
153
			}
154
		
155 View Code Duplication
			switch ($srcImgInfo['mime']) {
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...
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') {
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...
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