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