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