Completed
Branch prettify (0b8ea6)
by samayo
01:17
created
src/bulletproof.php 3 patches
Indentation   +456 added lines, -456 removed lines patch added patch discarded remove patch
@@ -16,463 +16,463 @@
 block discarded – undo
16 16
 
17 17
 class Image implements \ArrayAccess
18 18
 {
19
-    /**
20
-     * @var string The new image name, to be provided or will be generated.
21
-     */
22
-    protected $name;
23
-
24
-    /**
25
-     * @var int The image width in pixels
26
-     */
27
-    protected $width;
28
-
29
-    /**
30
-     * @var int The image height in pixels
31
-     */
32
-    protected $height;
33
-
34
-    /**
35
-     * @var string The image mime type (extension)
36
-     */
37
-    protected $mime;
38
-
39
-    /**
40
-     * @var string The full image path (dir + image + mime)
41
-     */
42
-    protected $fullPath;
43
-
44
-    /**
45
-     * @var string The folder or image storage location
46
-     */
47
-    protected $location;
48
-
49
-    /**
50
-     * @var array The min and max image size allowed for upload (in bytes)
51
-     */
52
-    protected $size = array(100, 500000);
53
-
54
-    /**
55
-     * @var array The max height and width image allowed
56
-     */
57
-    protected $dimensions = array(5000, 5000);
58
-
59
-    /**
60
-     * @var array The mime types allowed for upload
61
-     */
62
-    protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg');
63
-
64
-    /**
65
-     * @var array list of known image types
66
-     */
67
-    protected $acceptedMimes = array(
68
-        1 => 'gif', 'jpeg', 'png', 'swf', 'psd',
69
-        'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx',
70
-        'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico'
71
-    );
72
-    /**
73
-     * @var array error messages strings
74
-     */
75
-    protected $commonUploadErrors = array(
76
-        UPLOAD_ERR_OK         => '',
77
-        UPLOAD_ERR_INI_SIZE   => 'Image is larger than the specified amount set by the server',
78
-        UPLOAD_ERR_FORM_SIZE  => 'Image is larger than the specified amount specified by browser',
79
-        UPLOAD_ERR_PARTIAL    => 'Image could not be fully uploaded. Please try again later',
80
-        UPLOAD_ERR_NO_FILE    => 'Image is not found',
81
-        UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )',
82
-        UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions',
83
-        UPLOAD_ERR_EXTENSION  => 'A PHP extension has halted this file upload process'
84
-    );
85
-    /**
86
-     * @var array storage for the $_FILES global array
87
-     */
88
-    private $_files = array();
89
-    /**
90
-     * @var string storage for any errors
91
-     */
92
-    private $error = '';
93
-
94
-    /**
95
-     * @param array $_files represents the $_FILES array passed as dependency
96
-     */
97
-    public function __construct(array $_files = array())
98
-    {
99
-        /* check if php_exif is enabled */
100
-        if (!function_exists('exif_imagetype')) {
101
-            $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini';
102
-            return null;
103
-        }
104
-
105
-        $this->_files = $_files;
106
-    }
107
-
108
-    /**
109
-     * @param mixed $offset
110
-     * @param mixed $value
111
-     * @return null
112
-     */
113
-    public function offsetSet($offset, $value){}
114
-
115
-    /**
116
-     * @param mixed $offset
117
-     * @return null
118
-     */
119
-    public function offsetExists($offset){}
120
-
121
-    /**
122
-     * @param mixed $offset
123
-     * @return null
124
-     */
125
-    public function offsetUnset($offset){}
126
-
127
-    /**
128
-     * Gets array value \ArrayAccess
129
-     *
130
-     * @param mixed $offset
131
-     *
132
-     * @return string|boolean
133
-     */
134
-    public function offsetGet($offset)
135
-    {   
136
-        // return error if requested
137
-        if ($offset == 'error') {
138
-            return $this->error;
139
-        }
19
+	/**
20
+	 * @var string The new image name, to be provided or will be generated.
21
+	 */
22
+	protected $name;
23
+
24
+	/**
25
+	 * @var int The image width in pixels
26
+	 */
27
+	protected $width;
28
+
29
+	/**
30
+	 * @var int The image height in pixels
31
+	 */
32
+	protected $height;
33
+
34
+	/**
35
+	 * @var string The image mime type (extension)
36
+	 */
37
+	protected $mime;
38
+
39
+	/**
40
+	 * @var string The full image path (dir + image + mime)
41
+	 */
42
+	protected $fullPath;
43
+
44
+	/**
45
+	 * @var string The folder or image storage location
46
+	 */
47
+	protected $location;
48
+
49
+	/**
50
+	 * @var array The min and max image size allowed for upload (in bytes)
51
+	 */
52
+	protected $size = array(100, 500000);
53
+
54
+	/**
55
+	 * @var array The max height and width image allowed
56
+	 */
57
+	protected $dimensions = array(5000, 5000);
58
+
59
+	/**
60
+	 * @var array The mime types allowed for upload
61
+	 */
62
+	protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg');
63
+
64
+	/**
65
+	 * @var array list of known image types
66
+	 */
67
+	protected $acceptedMimes = array(
68
+		1 => 'gif', 'jpeg', 'png', 'swf', 'psd',
69
+		'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx',
70
+		'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico'
71
+	);
72
+	/**
73
+	 * @var array error messages strings
74
+	 */
75
+	protected $commonUploadErrors = array(
76
+		UPLOAD_ERR_OK         => '',
77
+		UPLOAD_ERR_INI_SIZE   => 'Image is larger than the specified amount set by the server',
78
+		UPLOAD_ERR_FORM_SIZE  => 'Image is larger than the specified amount specified by browser',
79
+		UPLOAD_ERR_PARTIAL    => 'Image could not be fully uploaded. Please try again later',
80
+		UPLOAD_ERR_NO_FILE    => 'Image is not found',
81
+		UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )',
82
+		UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions',
83
+		UPLOAD_ERR_EXTENSION  => 'A PHP extension has halted this file upload process'
84
+	);
85
+	/**
86
+	 * @var array storage for the $_FILES global array
87
+	 */
88
+	private $_files = array();
89
+	/**
90
+	 * @var string storage for any errors
91
+	 */
92
+	private $error = '';
93
+
94
+	/**
95
+	 * @param array $_files represents the $_FILES array passed as dependency
96
+	 */
97
+	public function __construct(array $_files = array())
98
+	{
99
+		/* check if php_exif is enabled */
100
+		if (!function_exists('exif_imagetype')) {
101
+			$this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini';
102
+			return null;
103
+		}
104
+
105
+		$this->_files = $_files;
106
+	}
107
+
108
+	/**
109
+	 * @param mixed $offset
110
+	 * @param mixed $value
111
+	 * @return null
112
+	 */
113
+	public function offsetSet($offset, $value){}
114
+
115
+	/**
116
+	 * @param mixed $offset
117
+	 * @return null
118
+	 */
119
+	public function offsetExists($offset){}
120
+
121
+	/**
122
+	 * @param mixed $offset
123
+	 * @return null
124
+	 */
125
+	public function offsetUnset($offset){}
126
+
127
+	/**
128
+	 * Gets array value \ArrayAccess
129
+	 *
130
+	 * @param mixed $offset
131
+	 *
132
+	 * @return string|boolean
133
+	 */
134
+	public function offsetGet($offset)
135
+	{   
136
+		// return error if requested
137
+		if ($offset == 'error') {
138
+			return $this->error;
139
+		}
140 140
         
141
-        // return false if $image['key'] isn't found
142
-        if (!isset($this->_files[$offset])) {
143
-            return false;
144
-        }
145
-
146
-        $this->_files = $this->_files[$offset];
147
-
148
-        // check for common upload errors
149
-        if(isset($this->_files['error'])){
150
-            $this->error = $this->commonUploadErrors[$this->_files['error']];
151
-        }
152
-
153
-        return true;
154
-    }
155
-
156
-    /**
157
-     * Sets max image height and width limit
158
-     *
159
-     * @param $maxWidth int max width value
160
-     * @param $maxHeight int max height value
161
-     *
162
-     * @return $this
163
-     */
164
-    public function setDimension($maxWidth, $maxHeight)
165
-    {
166
-        $this->dimensions = array($maxWidth, $maxHeight);
167
-        return $this;
168
-    }
169
-
170
-    /**
171
-     * Returns the full path of the image ex 'location/image.mime'
172
-     *
173
-     * @return string
174
-     */
175
-    public function getFullPath()
176
-    {
177
-        $this->fullPath = $this->location . '/' . $this->name . '.' . $this->mime;
178
-        return $this->fullPath;
179
-    }
180
-
181
-    /**
182
-     * Returns the image size in bytes
183
-     *
184
-     * @return int
185
-     */
186
-    public function getSize()
187
-    {
188
-        return (int) $this->_files['size'];
189
-    }
190
-
191
-    /**
192
-     * Define a min and max image size for uploading
193
-     *
194
-     * @param $min int minimum value in bytes
195
-     * @param $max int maximum value in bytes
196
-     *
197
-     * @return $this
198
-     */
199
-    public function setSize($min, $max)
200
-    {
201
-        $this->size = array($min, $max);
202
-        return $this;
203
-    }
204
-
205
-    /**
206
-     * Returns a JSON format of the image width, height, name, mime ...
207
-     *
208
-     * @return string
209
-     */
210
-    public function getJson()
211
-    {
212
-        /* gather image info for json storage */
213
-        return json_encode (
214
-            array(
215
-                'name'      => $this->name,
216
-                'mime'      => $this->mime,
217
-                'height'    => $this->height,
218
-                'width'     => $this->width,
219
-                'size'      => $this->_files['size'],
220
-                'location'  => $this->location,
221
-                'fullpath'  => $this->fullPath
222
-            )
223
-        );
224
-    }
225
-
226
-    /**
227
-     * Returns the image mime type
228
-     *
229
-     * @return null|string
230
-     */
231
-    public function getMime()
232
-    {
233
-        if (!$this->mime) {
234
-            return $this->getImageMime($this->_files['tmp_name']);
235
-        }
236
-        return $this->mime;
237
-    }
238
-
239
-    /**
240
-     * Define a mime type for uploading
241
-     *
242
-     * @param array $fileTypes
243
-     *
244
-     * @return $this
245
-     */
246
-    public function setMime(array $fileTypes)
247
-    {
248
-        $this->mimeTypes = $fileTypes;
249
-        return $this;
250
-    }
251
-
252
-    /**
253
-     * Gets the real image mime type
254
-     *
255
-     * @param $tmp_name string The upload tmp directory
256
-     *
257
-     * @return null|string
258
-     */
259
-    protected function getImageMime($tmp_name)
260
-    {
261
-        $mime = @ $this->acceptedMimes[exif_imagetype($tmp_name)];
262
-
263
-        if (!$mime) {
264
-            return null;
265
-        }
266
-
267
-        return $mime;
268
-    }
269
-
270
-    /**
271
-     * Returns error string or false if no errors occurred
272
-     *
273
-     * @return string|false
274
-     */
275
-    public function getError()
276
-    {
277
-        return $this->error != '' ? $this->error : false;
278
-    }
279
-
280
-    /**
281
-     * Returns the image name
282
-     *
283
-     * @return string
284
-     */
285
-    public function getName()
286
-    {
287
-        if (!$this->name) {
288
-            return uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q')));
289
-        }
290
-
291
-        return $this->name;
292
-    }
293
-
294
-    /**
295
-     * Provide image name if not provided
296
-     *
297
-     * @param null $isNameProvided
298
-     * @return $this
299
-     */
300
-    public function setName($isNameProvided = null)
301
-    {
302
-        if ($isNameProvided) {
303
-            $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING);
304
-        }
305
-
306
-        return $this;
307
-    }
308
-
309
-    /**
310
-     * Returns the image width
311
-     *
312
-     * @return int
313
-     */
314
-    public function getWidth()
315
-    {
316
-        if ($this->width != null) {
317
-            return $this->width;
318
-        }
319
-
320
-        list($width) = getImageSize($this->_files['tmp_name']);
321
-        return $width;
322
-    }
323
-
324
-    /**
325
-     * Returns the image height in pixels
326
-     *
327
-     * @return int
328
-     */
329
-    public function getHeight()
330
-    {
331
-        if ($this->height != null) {
332
-            return $this->height;
333
-        }
334
-
335
-        list(, $height) = getImageSize($this->_files['tmp_name']);
336
-        return $height;
337
-    }
338
-
339
-    /**
340
-     * Returns the storage / folder name
341
-     *
342
-     * @return string
343
-     */
344
-    public function getLocation()
345
-    {
346
-        if (!$this->location) {
347
-            $this->setLocation();
348
-        }
349
-
350
-        return $this->location;
351
-    }
352
-
353
-    /**
354
-     * Validate directory/permission before creating a folder
355
-     * 
356
-     * @param $dir string the folder name to check
357
-     * 
358
-     * @return bool
359
-     */
360
-    private function isDirectoryValid($dir) 
361
-    {
362
-        $isValid = file_exists($dir) || is_dir($dir) || !is_writable($dir); 
363
-
364
-        if(!$isValid){
365
-            $this->error = 'Can not create a folder \'' . $dir . '\', please permission issues ';
366
-            return false;
367
-        }
368
-
369
-        return true;
370
-    }
371
-
372
-    /**
373
-     * Creates a location for upload storage
374
-     *
375
-     * @param $dir string the folder name to create
376
-     * @param int $permission chmod permission
377
-     *
378
-     * @return $this
379
-     */
380
-    public function setLocation($dir = 'bulletproof', $permission = 0666)
381
-    {
382
-        $isDirectoryValid = $this->isDirectoryValid($dir); 
383
-
384
-        if(!$isDirectoryValid){
385
-            return false;
386
-        }
141
+		// return false if $image['key'] isn't found
142
+		if (!isset($this->_files[$offset])) {
143
+			return false;
144
+		}
145
+
146
+		$this->_files = $this->_files[$offset];
147
+
148
+		// check for common upload errors
149
+		if(isset($this->_files['error'])){
150
+			$this->error = $this->commonUploadErrors[$this->_files['error']];
151
+		}
152
+
153
+		return true;
154
+	}
155
+
156
+	/**
157
+	 * Sets max image height and width limit
158
+	 *
159
+	 * @param $maxWidth int max width value
160
+	 * @param $maxHeight int max height value
161
+	 *
162
+	 * @return $this
163
+	 */
164
+	public function setDimension($maxWidth, $maxHeight)
165
+	{
166
+		$this->dimensions = array($maxWidth, $maxHeight);
167
+		return $this;
168
+	}
169
+
170
+	/**
171
+	 * Returns the full path of the image ex 'location/image.mime'
172
+	 *
173
+	 * @return string
174
+	 */
175
+	public function getFullPath()
176
+	{
177
+		$this->fullPath = $this->location . '/' . $this->name . '.' . $this->mime;
178
+		return $this->fullPath;
179
+	}
180
+
181
+	/**
182
+	 * Returns the image size in bytes
183
+	 *
184
+	 * @return int
185
+	 */
186
+	public function getSize()
187
+	{
188
+		return (int) $this->_files['size'];
189
+	}
190
+
191
+	/**
192
+	 * Define a min and max image size for uploading
193
+	 *
194
+	 * @param $min int minimum value in bytes
195
+	 * @param $max int maximum value in bytes
196
+	 *
197
+	 * @return $this
198
+	 */
199
+	public function setSize($min, $max)
200
+	{
201
+		$this->size = array($min, $max);
202
+		return $this;
203
+	}
204
+
205
+	/**
206
+	 * Returns a JSON format of the image width, height, name, mime ...
207
+	 *
208
+	 * @return string
209
+	 */
210
+	public function getJson()
211
+	{
212
+		/* gather image info for json storage */
213
+		return json_encode (
214
+			array(
215
+				'name'      => $this->name,
216
+				'mime'      => $this->mime,
217
+				'height'    => $this->height,
218
+				'width'     => $this->width,
219
+				'size'      => $this->_files['size'],
220
+				'location'  => $this->location,
221
+				'fullpath'  => $this->fullPath
222
+			)
223
+		);
224
+	}
225
+
226
+	/**
227
+	 * Returns the image mime type
228
+	 *
229
+	 * @return null|string
230
+	 */
231
+	public function getMime()
232
+	{
233
+		if (!$this->mime) {
234
+			return $this->getImageMime($this->_files['tmp_name']);
235
+		}
236
+		return $this->mime;
237
+	}
238
+
239
+	/**
240
+	 * Define a mime type for uploading
241
+	 *
242
+	 * @param array $fileTypes
243
+	 *
244
+	 * @return $this
245
+	 */
246
+	public function setMime(array $fileTypes)
247
+	{
248
+		$this->mimeTypes = $fileTypes;
249
+		return $this;
250
+	}
251
+
252
+	/**
253
+	 * Gets the real image mime type
254
+	 *
255
+	 * @param $tmp_name string The upload tmp directory
256
+	 *
257
+	 * @return null|string
258
+	 */
259
+	protected function getImageMime($tmp_name)
260
+	{
261
+		$mime = @ $this->acceptedMimes[exif_imagetype($tmp_name)];
262
+
263
+		if (!$mime) {
264
+			return null;
265
+		}
266
+
267
+		return $mime;
268
+	}
269
+
270
+	/**
271
+	 * Returns error string or false if no errors occurred
272
+	 *
273
+	 * @return string|false
274
+	 */
275
+	public function getError()
276
+	{
277
+		return $this->error != '' ? $this->error : false;
278
+	}
279
+
280
+	/**
281
+	 * Returns the image name
282
+	 *
283
+	 * @return string
284
+	 */
285
+	public function getName()
286
+	{
287
+		if (!$this->name) {
288
+			return uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q')));
289
+		}
290
+
291
+		return $this->name;
292
+	}
293
+
294
+	/**
295
+	 * Provide image name if not provided
296
+	 *
297
+	 * @param null $isNameProvided
298
+	 * @return $this
299
+	 */
300
+	public function setName($isNameProvided = null)
301
+	{
302
+		if ($isNameProvided) {
303
+			$this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING);
304
+		}
305
+
306
+		return $this;
307
+	}
308
+
309
+	/**
310
+	 * Returns the image width
311
+	 *
312
+	 * @return int
313
+	 */
314
+	public function getWidth()
315
+	{
316
+		if ($this->width != null) {
317
+			return $this->width;
318
+		}
319
+
320
+		list($width) = getImageSize($this->_files['tmp_name']);
321
+		return $width;
322
+	}
323
+
324
+	/**
325
+	 * Returns the image height in pixels
326
+	 *
327
+	 * @return int
328
+	 */
329
+	public function getHeight()
330
+	{
331
+		if ($this->height != null) {
332
+			return $this->height;
333
+		}
334
+
335
+		list(, $height) = getImageSize($this->_files['tmp_name']);
336
+		return $height;
337
+	}
338
+
339
+	/**
340
+	 * Returns the storage / folder name
341
+	 *
342
+	 * @return string
343
+	 */
344
+	public function getLocation()
345
+	{
346
+		if (!$this->location) {
347
+			$this->setLocation();
348
+		}
349
+
350
+		return $this->location;
351
+	}
352
+
353
+	/**
354
+	 * Validate directory/permission before creating a folder
355
+	 * 
356
+	 * @param $dir string the folder name to check
357
+	 * 
358
+	 * @return bool
359
+	 */
360
+	private function isDirectoryValid($dir) 
361
+	{
362
+		$isValid = file_exists($dir) || is_dir($dir) || !is_writable($dir); 
363
+
364
+		if(!$isValid){
365
+			$this->error = 'Can not create a folder \'' . $dir . '\', please permission issues ';
366
+			return false;
367
+		}
368
+
369
+		return true;
370
+	}
371
+
372
+	/**
373
+	 * Creates a location for upload storage
374
+	 *
375
+	 * @param $dir string the folder name to create
376
+	 * @param int $permission chmod permission
377
+	 *
378
+	 * @return $this
379
+	 */
380
+	public function setLocation($dir = 'bulletproof', $permission = 0666)
381
+	{
382
+		$isDirectoryValid = $this->isDirectoryValid($dir); 
383
+
384
+		if(!$isDirectoryValid){
385
+			return false;
386
+		}
387 387
       
388
-        $create = @mkdir('' . $dir, (int) $permission, true);
389
-
390
-        if (!$create) {
391
-            $this->error = 'Error! Folder ' . $dir . ' could not be created';
392
-            return false;
393
-        }
394
-
395
-        $this->location = $dir;
396
-        return $this;
397
-    }
398
-
399
-    /**
400
-     * This methods validates and uploads the image
401
-     * @return false|Image
402
-     */
403
-    public function upload()
404
-    {
405
-        /* modify variable names for convenience */
406
-        $image = $this;
407
-        $files = $this->_files;
408
-
409
-        if ($this->error || !isset($files['tmp_name'])) {
410
-            return false;
411
-        }
412
-
413
-        /* check image for valid mime types and return mime */
414
-        $image->mime = $image->getImageMime($files['tmp_name']);
415
-        /* validate image mime type */
416
-        if (!in_array($image->mime, $image->mimeTypes)) {
417
-            $ext = implode(', ', $image->mimeTypes);
418
-            $image->error = sprintf('Invalid File! Only (%s) image types are allowed', $ext);
419
-            return false;
420
-        }
421
-
422
-        /* initialize image properties */
423
-        $image->name = $image->getName();
424
-        $image->width = $image->getWidth();
425
-        $image->height = $image->getHeight();
426
-        $image->location = $image->getLocation();
388
+		$create = @mkdir('' . $dir, (int) $permission, true);
389
+
390
+		if (!$create) {
391
+			$this->error = 'Error! Folder ' . $dir . ' could not be created';
392
+			return false;
393
+		}
394
+
395
+		$this->location = $dir;
396
+		return $this;
397
+	}
398
+
399
+	/**
400
+	 * This methods validates and uploads the image
401
+	 * @return false|Image
402
+	 */
403
+	public function upload()
404
+	{
405
+		/* modify variable names for convenience */
406
+		$image = $this;
407
+		$files = $this->_files;
408
+
409
+		if ($this->error || !isset($files['tmp_name'])) {
410
+			return false;
411
+		}
412
+
413
+		/* check image for valid mime types and return mime */
414
+		$image->mime = $image->getImageMime($files['tmp_name']);
415
+		/* validate image mime type */
416
+		if (!in_array($image->mime, $image->mimeTypes)) {
417
+			$ext = implode(', ', $image->mimeTypes);
418
+			$image->error = sprintf('Invalid File! Only (%s) image types are allowed', $ext);
419
+			return false;
420
+		}
421
+
422
+		/* initialize image properties */
423
+		$image->name = $image->getName();
424
+		$image->width = $image->getWidth();
425
+		$image->height = $image->getHeight();
426
+		$image->location = $image->getLocation();
427 427
         
428
-        $image->getFullPath();
429
-
430
-        /* get image sizes */
431
-        list($minSize, $maxSize) = $image->size;
432
-
433
-        /* check image size based on the settings */
434
-        if ($files['size'] < $minSize || $files['size'] > $maxSize) {
435
-            $min = intval($minSize / 1000) ?: 1;
436
-            $max = intval($maxSize / 1000) ?: 1;
437
-            $image->error = 'Image size should be at least ' . $min . ' KB, and no more than ' . $max . ' KB';
438
-            return false;
439
-        }
440
-
441
-        /* check image dimension */
442
-        list($allowedWidth, $allowedHeight) = $image->dimensions;
443
-
444
-        if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
445
-            $image->error = 'Image height/width should be less than ' . $allowedHeight . '/' . $allowedWidth . ' pixels';
446
-            return false;
447
-        }
448
-
449
-        if ($image->height < 2 || $image->width < 2) {
450
-            $image->error = 'Image height/width too small or corrupted.';
451
-            return false;
452
-        }
453
-
454
-        if ($image->error === '') {
455
-            $moveUpload = $image->moveUploadedFile($files['tmp_name'], $image->fullPath);
456
-            if (false !== $moveUpload) {
457
-                return $image;
458
-            }
459
-        }
460
-
461
-        $image->error = 'Upload failed, Unknown error occured';
462
-        return false;
463
-    }
464
-
465
-
466
-    /**
467
-     * Final upload method to be called, isolated for testing purposes
468
-     *
469
-     * @param $tmp_name int the temporary location of the image file
470
-     * @param $destination int upload destination
471
-     *
472
-     * @return bool
473
-     */
474
-    public function moveUploadedFile($tmp_name, $destination)
475
-    {
476
-        return move_uploaded_file($tmp_name, $destination);
477
-    }
428
+		$image->getFullPath();
429
+
430
+		/* get image sizes */
431
+		list($minSize, $maxSize) = $image->size;
432
+
433
+		/* check image size based on the settings */
434
+		if ($files['size'] < $minSize || $files['size'] > $maxSize) {
435
+			$min = intval($minSize / 1000) ?: 1;
436
+			$max = intval($maxSize / 1000) ?: 1;
437
+			$image->error = 'Image size should be at least ' . $min . ' KB, and no more than ' . $max . ' KB';
438
+			return false;
439
+		}
440
+
441
+		/* check image dimension */
442
+		list($allowedWidth, $allowedHeight) = $image->dimensions;
443
+
444
+		if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
445
+			$image->error = 'Image height/width should be less than ' . $allowedHeight . '/' . $allowedWidth . ' pixels';
446
+			return false;
447
+		}
448
+
449
+		if ($image->height < 2 || $image->width < 2) {
450
+			$image->error = 'Image height/width too small or corrupted.';
451
+			return false;
452
+		}
453
+
454
+		if ($image->error === '') {
455
+			$moveUpload = $image->moveUploadedFile($files['tmp_name'], $image->fullPath);
456
+			if (false !== $moveUpload) {
457
+				return $image;
458
+			}
459
+		}
460
+
461
+		$image->error = 'Upload failed, Unknown error occured';
462
+		return false;
463
+	}
464
+
465
+
466
+	/**
467
+	 * Final upload method to be called, isolated for testing purposes
468
+	 *
469
+	 * @param $tmp_name int the temporary location of the image file
470
+	 * @param $destination int upload destination
471
+	 *
472
+	 * @return bool
473
+	 */
474
+	public function moveUploadedFile($tmp_name, $destination)
475
+	{
476
+		return move_uploaded_file($tmp_name, $destination);
477
+	}
478 478
 }
479 479
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -110,19 +110,19 @@  discard block
 block discarded – undo
110 110
      * @param mixed $value
111 111
      * @return null
112 112
      */
113
-    public function offsetSet($offset, $value){}
113
+    public function offsetSet($offset, $value) {}
114 114
 
115 115
     /**
116 116
      * @param mixed $offset
117 117
      * @return null
118 118
      */
119
-    public function offsetExists($offset){}
119
+    public function offsetExists($offset) {}
120 120
 
121 121
     /**
122 122
      * @param mixed $offset
123 123
      * @return null
124 124
      */
125
-    public function offsetUnset($offset){}
125
+    public function offsetUnset($offset) {}
126 126
 
127 127
     /**
128 128
      * Gets array value \ArrayAccess
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
         $this->_files = $this->_files[$offset];
147 147
 
148 148
         // check for common upload errors
149
-        if(isset($this->_files['error'])){
149
+        if (isset($this->_files['error'])) {
150 150
             $this->error = $this->commonUploadErrors[$this->_files['error']];
151 151
         }
152 152
 
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
      */
175 175
     public function getFullPath()
176 176
     {
177
-        $this->fullPath = $this->location . '/' . $this->name . '.' . $this->mime;
177
+        $this->fullPath = $this->location.'/'.$this->name.'.'.$this->mime;
178 178
         return $this->fullPath;
179 179
     }
180 180
 
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
     public function getJson()
211 211
     {
212 212
         /* gather image info for json storage */
213
-        return json_encode (
213
+        return json_encode(
214 214
             array(
215 215
                 'name'      => $this->name,
216 216
                 'mime'      => $this->mime,
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
     public function getName()
286 286
     {
287 287
         if (!$this->name) {
288
-            return uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q')));
288
+            return uniqid('', true).'_'.str_shuffle(implode(range('e', 'q')));
289 289
         }
290 290
 
291 291
         return $this->name;
@@ -361,8 +361,8 @@  discard block
 block discarded – undo
361 361
     {
362 362
         $isValid = file_exists($dir) || is_dir($dir) || !is_writable($dir); 
363 363
 
364
-        if(!$isValid){
365
-            $this->error = 'Can not create a folder \'' . $dir . '\', please permission issues ';
364
+        if (!$isValid) {
365
+            $this->error = 'Can not create a folder \''.$dir.'\', please permission issues ';
366 366
             return false;
367 367
         }
368 368
 
@@ -381,14 +381,14 @@  discard block
 block discarded – undo
381 381
     {
382 382
         $isDirectoryValid = $this->isDirectoryValid($dir); 
383 383
 
384
-        if(!$isDirectoryValid){
384
+        if (!$isDirectoryValid) {
385 385
             return false;
386 386
         }
387 387
       
388
-        $create = @mkdir('' . $dir, (int) $permission, true);
388
+        $create = @mkdir(''.$dir, (int) $permission, true);
389 389
 
390 390
         if (!$create) {
391
-            $this->error = 'Error! Folder ' . $dir . ' could not be created';
391
+            $this->error = 'Error! Folder '.$dir.' could not be created';
392 392
             return false;
393 393
         }
394 394
 
@@ -434,7 +434,7 @@  discard block
 block discarded – undo
434 434
         if ($files['size'] < $minSize || $files['size'] > $maxSize) {
435 435
             $min = intval($minSize / 1000) ?: 1;
436 436
             $max = intval($maxSize / 1000) ?: 1;
437
-            $image->error = 'Image size should be at least ' . $min . ' KB, and no more than ' . $max . ' KB';
437
+            $image->error = 'Image size should be at least '.$min.' KB, and no more than '.$max.' KB';
438 438
             return false;
439 439
         }
440 440
 
@@ -442,7 +442,7 @@  discard block
 block discarded – undo
442 442
         list($allowedWidth, $allowedHeight) = $image->dimensions;
443 443
 
444 444
         if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
445
-            $image->error = 'Image height/width should be less than ' . $allowedHeight . '/' . $allowedWidth . ' pixels';
445
+            $image->error = 'Image height/width should be less than '.$allowedHeight.'/'.$allowedWidth.' pixels';
446 446
             return false;
447 447
         }
448 448
 
Please login to merge, or discard this patch.
Braces   +20 added lines, -40 removed lines patch added patch discarded remove patch
@@ -94,8 +94,7 @@  discard block
 block discarded – undo
94 94
     /**
95 95
      * @param array $_files represents the $_FILES array passed as dependency
96 96
      */
97
-    public function __construct(array $_files = array())
98
-    {
97
+    public function __construct(array $_files = array()) {
99 98
         /* check if php_exif is enabled */
100 99
         if (!function_exists('exif_imagetype')) {
101 100
             $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini';
@@ -131,8 +130,7 @@  discard block
 block discarded – undo
131 130
      *
132 131
      * @return string|boolean
133 132
      */
134
-    public function offsetGet($offset)
135
-    {   
133
+    public function offsetGet($offset) {
136 134
         // return error if requested
137 135
         if ($offset == 'error') {
138 136
             return $this->error;
@@ -161,8 +159,7 @@  discard block
 block discarded – undo
161 159
      *
162 160
      * @return $this
163 161
      */
164
-    public function setDimension($maxWidth, $maxHeight)
165
-    {
162
+    public function setDimension($maxWidth, $maxHeight) {
166 163
         $this->dimensions = array($maxWidth, $maxHeight);
167 164
         return $this;
168 165
     }
@@ -172,8 +169,7 @@  discard block
 block discarded – undo
172 169
      *
173 170
      * @return string
174 171
      */
175
-    public function getFullPath()
176
-    {
172
+    public function getFullPath() {
177 173
         $this->fullPath = $this->location . '/' . $this->name . '.' . $this->mime;
178 174
         return $this->fullPath;
179 175
     }
@@ -183,8 +179,7 @@  discard block
 block discarded – undo
183 179
      *
184 180
      * @return int
185 181
      */
186
-    public function getSize()
187
-    {
182
+    public function getSize() {
188 183
         return (int) $this->_files['size'];
189 184
     }
190 185
 
@@ -196,8 +191,7 @@  discard block
 block discarded – undo
196 191
      *
197 192
      * @return $this
198 193
      */
199
-    public function setSize($min, $max)
200
-    {
194
+    public function setSize($min, $max) {
201 195
         $this->size = array($min, $max);
202 196
         return $this;
203 197
     }
@@ -207,8 +201,7 @@  discard block
 block discarded – undo
207 201
      *
208 202
      * @return string
209 203
      */
210
-    public function getJson()
211
-    {
204
+    public function getJson() {
212 205
         /* gather image info for json storage */
213 206
         return json_encode (
214 207
             array(
@@ -228,8 +221,7 @@  discard block
 block discarded – undo
228 221
      *
229 222
      * @return null|string
230 223
      */
231
-    public function getMime()
232
-    {
224
+    public function getMime() {
233 225
         if (!$this->mime) {
234 226
             return $this->getImageMime($this->_files['tmp_name']);
235 227
         }
@@ -243,8 +235,7 @@  discard block
 block discarded – undo
243 235
      *
244 236
      * @return $this
245 237
      */
246
-    public function setMime(array $fileTypes)
247
-    {
238
+    public function setMime(array $fileTypes) {
248 239
         $this->mimeTypes = $fileTypes;
249 240
         return $this;
250 241
     }
@@ -256,8 +247,7 @@  discard block
 block discarded – undo
256 247
      *
257 248
      * @return null|string
258 249
      */
259
-    protected function getImageMime($tmp_name)
260
-    {
250
+    protected function getImageMime($tmp_name) {
261 251
         $mime = @ $this->acceptedMimes[exif_imagetype($tmp_name)];
262 252
 
263 253
         if (!$mime) {
@@ -272,8 +262,7 @@  discard block
 block discarded – undo
272 262
      *
273 263
      * @return string|false
274 264
      */
275
-    public function getError()
276
-    {
265
+    public function getError() {
277 266
         return $this->error != '' ? $this->error : false;
278 267
     }
279 268
 
@@ -282,8 +271,7 @@  discard block
 block discarded – undo
282 271
      *
283 272
      * @return string
284 273
      */
285
-    public function getName()
286
-    {
274
+    public function getName() {
287 275
         if (!$this->name) {
288 276
             return uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q')));
289 277
         }
@@ -297,8 +285,7 @@  discard block
 block discarded – undo
297 285
      * @param null $isNameProvided
298 286
      * @return $this
299 287
      */
300
-    public function setName($isNameProvided = null)
301
-    {
288
+    public function setName($isNameProvided = null) {
302 289
         if ($isNameProvided) {
303 290
             $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING);
304 291
         }
@@ -311,8 +298,7 @@  discard block
 block discarded – undo
311 298
      *
312 299
      * @return int
313 300
      */
314
-    public function getWidth()
315
-    {
301
+    public function getWidth() {
316 302
         if ($this->width != null) {
317 303
             return $this->width;
318 304
         }
@@ -326,8 +312,7 @@  discard block
 block discarded – undo
326 312
      *
327 313
      * @return int
328 314
      */
329
-    public function getHeight()
330
-    {
315
+    public function getHeight() {
331 316
         if ($this->height != null) {
332 317
             return $this->height;
333 318
         }
@@ -341,8 +326,7 @@  discard block
 block discarded – undo
341 326
      *
342 327
      * @return string
343 328
      */
344
-    public function getLocation()
345
-    {
329
+    public function getLocation() {
346 330
         if (!$this->location) {
347 331
             $this->setLocation();
348 332
         }
@@ -357,8 +341,7 @@  discard block
 block discarded – undo
357 341
      * 
358 342
      * @return bool
359 343
      */
360
-    private function isDirectoryValid($dir) 
361
-    {
344
+    private function isDirectoryValid($dir) {
362 345
         $isValid = file_exists($dir) || is_dir($dir) || !is_writable($dir); 
363 346
 
364 347
         if(!$isValid){
@@ -377,8 +360,7 @@  discard block
 block discarded – undo
377 360
      *
378 361
      * @return $this
379 362
      */
380
-    public function setLocation($dir = 'bulletproof', $permission = 0666)
381
-    {
363
+    public function setLocation($dir = 'bulletproof', $permission = 0666) {
382 364
         $isDirectoryValid = $this->isDirectoryValid($dir); 
383 365
 
384 366
         if(!$isDirectoryValid){
@@ -400,8 +382,7 @@  discard block
 block discarded – undo
400 382
      * This methods validates and uploads the image
401 383
      * @return false|Image
402 384
      */
403
-    public function upload()
404
-    {
385
+    public function upload() {
405 386
         /* modify variable names for convenience */
406 387
         $image = $this;
407 388
         $files = $this->_files;
@@ -471,8 +452,7 @@  discard block
 block discarded – undo
471 452
      *
472 453
      * @return bool
473 454
      */
474
-    public function moveUploadedFile($tmp_name, $destination)
475
-    {
455
+    public function moveUploadedFile($tmp_name, $destination) {
476 456
         return move_uploaded_file($tmp_name, $destination);
477 457
     }
478 458
 }
479 459
\ No newline at end of file
Please login to merge, or discard this patch.