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