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