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