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