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