@@ -15,456 +15,456 @@  | 
                                                    ||
| 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', 'webp'  | 
                                                        |
| 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 | -  | 
                                                        |
| 102 | - /* check if php_exif is enabled */  | 
                                                        |
| 103 | -        if (!function_exists('exif_imagetype')) { | 
                                                        |
| 104 | - $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini';  | 
                                                        |
| 105 | - return false;  | 
                                                        |
| 106 | - }  | 
                                                        |
| 107 | -  | 
                                                        |
| 108 | - $this->_files = $_files;  | 
                                                        |
| 109 | - }  | 
                                                        |
| 110 | -  | 
                                                        |
| 111 | - /**  | 
                                                        |
| 112 | - * \ArrayAccess unused method  | 
                                                        |
| 113 | - *  | 
                                                        |
| 114 | - * @param mixed $offset  | 
                                                        |
| 115 | - * @param mixed $value  | 
                                                        |
| 116 | - */  | 
                                                        |
| 117 | -    public function offsetSet($offset, $value) : void {} | 
                                                        |
| 118 | -  | 
                                                        |
| 119 | - /**  | 
                                                        |
| 120 | - * \ArrayAccess unused method  | 
                                                        |
| 121 | - *  | 
                                                        |
| 122 | - * @param mixed $offset  | 
                                                        |
| 123 | - */  | 
                                                        |
| 124 | -    public function offsetExists($offset) : bool {} | 
                                                        |
| 125 | -  | 
                                                        |
| 126 | - /**  | 
                                                        |
| 127 | - * \ArrayAccess unused method  | 
                                                        |
| 128 | - *  | 
                                                        |
| 129 | - * @param mixed $offset  | 
                                                        |
| 130 | - */  | 
                                                        |
| 131 | -    public function offsetUnset($offset) : void {} | 
                                                        |
| 132 | -  | 
                                                        |
| 133 | - /**  | 
                                                        |
| 134 | - * \ArrayAccess - get array value from object  | 
                                                        |
| 135 | - *  | 
                                                        |
| 136 | - * @param mixed $offset  | 
                                                        |
| 137 | - *  | 
                                                        |
| 138 | - * @return string|bool  | 
                                                        |
| 139 | - */  | 
                                                        |
| 140 | - public function offsetGet($offset) : mixed  | 
                                                        |
| 141 | -    { | 
                                                        |
| 142 | - /* return false if $image['key'] isn't found */  | 
                                                        |
| 143 | -        if (!isset($this->_files[$offset])) { | 
                                                        |
| 144 | -          $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', 'webp'  | 
                                                        |
| 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 | +  | 
                                                        |
| 102 | + /* check if php_exif is enabled */  | 
                                                        |
| 103 | +		if (!function_exists('exif_imagetype')) { | 
                                                        |
| 104 | + $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your PHP.ini';  | 
                                                        |
| 105 | + return false;  | 
                                                        |
| 106 | + }  | 
                                                        |
| 107 | +  | 
                                                        |
| 108 | + $this->_files = $_files;  | 
                                                        |
| 109 | + }  | 
                                                        |
| 110 | +  | 
                                                        |
| 111 | + /**  | 
                                                        |
| 112 | + * \ArrayAccess unused method  | 
                                                        |
| 113 | + *  | 
                                                        |
| 114 | + * @param mixed $offset  | 
                                                        |
| 115 | + * @param mixed $value  | 
                                                        |
| 116 | + */  | 
                                                        |
| 117 | +	public function offsetSet($offset, $value) : void {} | 
                                                        |
| 118 | +  | 
                                                        |
| 119 | + /**  | 
                                                        |
| 120 | + * \ArrayAccess unused method  | 
                                                        |
| 121 | + *  | 
                                                        |
| 122 | + * @param mixed $offset  | 
                                                        |
| 123 | + */  | 
                                                        |
| 124 | +	public function offsetExists($offset) : bool {} | 
                                                        |
| 125 | +  | 
                                                        |
| 126 | + /**  | 
                                                        |
| 127 | + * \ArrayAccess unused method  | 
                                                        |
| 128 | + *  | 
                                                        |
| 129 | + * @param mixed $offset  | 
                                                        |
| 130 | + */  | 
                                                        |
| 131 | +	public function offsetUnset($offset) : void {} | 
                                                        |
| 132 | +  | 
                                                        |
| 133 | + /**  | 
                                                        |
| 134 | + * \ArrayAccess - get array value from object  | 
                                                        |
| 135 | + *  | 
                                                        |
| 136 | + * @param mixed $offset  | 
                                                        |
| 137 | + *  | 
                                                        |
| 138 | + * @return string|bool  | 
                                                        |
| 139 | + */  | 
                                                        |
| 140 | + public function offsetGet($offset) : mixed  | 
                                                        |
| 141 | +	{ | 
                                                        |
| 142 | + /* return false if $image['key'] isn't found */  | 
                                                        |
| 143 | +		if (!isset($this->_files[$offset])) { | 
                                                        |
| 144 | +		  $this->error = sprintf('No file input found with name: (%s)', $offset); | 
                                                        |
| 145 | 145 | |
| 146 | - return false;  | 
                                                        |
| 147 | - }  | 
                                                        |
| 148 | -  | 
                                                        |
| 149 | - $this->_files = $this->_files[$offset];  | 
                                                        |
| 150 | -  | 
                                                        |
| 151 | - /* check for common upload errors */  | 
                                                        |
| 152 | -      if (isset($this->_files['error'])) { | 
                                                        |
| 153 | - $this->error = $this->commonErrors[$this->_files['error']];  | 
                                                        |
| 154 | - }  | 
                                                        |
| 155 | -  | 
                                                        |
| 156 | - return $this->error ? false : true;  | 
                                                        |
| 157 | - }  | 
                                                        |
| 158 | -  | 
                                                        |
| 159 | - /**  | 
                                                        |
| 160 | - * Sets max image height and width limit.  | 
                                                        |
| 161 | - *  | 
                                                        |
| 162 | - * @param $maxWidth int max width value  | 
                                                        |
| 163 | - * @param $maxHeight int max height value  | 
                                                        |
| 164 | - *  | 
                                                        |
| 165 | - * @return $this  | 
                                                        |
| 166 | - */  | 
                                                        |
| 167 | - public function setDimension($maxWidth, $maxHeight)  | 
                                                        |
| 168 | -    { | 
                                                        |
| 169 | - $this->dimensions = array($maxWidth, $maxHeight);  | 
                                                        |
| 170 | -  | 
                                                        |
| 171 | - return $this;  | 
                                                        |
| 172 | - }  | 
                                                        |
| 173 | -  | 
                                                        |
| 174 | - /**  | 
                                                        |
| 175 | - * Returns the full path of the image ex 'storage/image.mime'.  | 
                                                        |
| 176 | - *  | 
                                                        |
| 177 | - * @return string  | 
                                                        |
| 178 | - */  | 
                                                        |
| 179 | - public function getPath()  | 
                                                        |
| 180 | -    { | 
                                                        |
| 181 | - return $this->path = $this->getStorage() . '/' . $this->getName() . '.' . $this->getMime();  | 
                                                        |
| 182 | - }  | 
                                                        |
| 183 | -  | 
                                                        |
| 184 | - /**  | 
                                                        |
| 185 | - * Returns the image size in bytes.  | 
                                                        |
| 186 | - *  | 
                                                        |
| 187 | - * @return int  | 
                                                        |
| 188 | - */  | 
                                                        |
| 189 | - public function getSize()  | 
                                                        |
| 190 | -    { | 
                                                        |
| 191 | - return (int) $this->_files['size'];  | 
                                                        |
| 192 | - }  | 
                                                        |
| 193 | -  | 
                                                        |
| 194 | - /**  | 
                                                        |
| 195 | - * Define a min and max image size for uploading.  | 
                                                        |
| 196 | - *  | 
                                                        |
| 197 | - * @param $min int minimum value in bytes  | 
                                                        |
| 198 | - * @param $max int maximum value in bytes  | 
                                                        |
| 199 | - *  | 
                                                        |
| 200 | - * @return $this  | 
                                                        |
| 201 | - */  | 
                                                        |
| 202 | - public function setSize($min, $max)  | 
                                                        |
| 203 | -    { | 
                                                        |
| 204 | - $this->size = array($min, $max);  | 
                                                        |
| 205 | - return $this;  | 
                                                        |
| 206 | - }  | 
                                                        |
| 207 | -  | 
                                                        |
| 208 | - /**  | 
                                                        |
| 209 | - * Returns a JSON format of the image width, height, name, mime ...  | 
                                                        |
| 210 | - *  | 
                                                        |
| 211 | - * @return string  | 
                                                        |
| 212 | - */  | 
                                                        |
| 213 | - public function getJson()  | 
                                                        |
| 214 | -    { | 
                                                        |
| 215 | - return json_encode(  | 
                                                        |
| 216 | - array(  | 
                                                        |
| 217 | - 'name' => $this->name,  | 
                                                        |
| 218 | - 'mime' => $this->mime,  | 
                                                        |
| 219 | - 'height' => $this->height,  | 
                                                        |
| 220 | - 'width' => $this->width,  | 
                                                        |
| 221 | - 'size' => $this->_files['size'],  | 
                                                        |
| 222 | - 'storage' => $this->storage,  | 
                                                        |
| 223 | - 'path' => $this->path,  | 
                                                        |
| 224 | - )  | 
                                                        |
| 225 | - );  | 
                                                        |
| 226 | - }  | 
                                                        |
| 227 | -  | 
                                                        |
| 228 | - /**  | 
                                                        |
| 229 | - * Returns the image mime type.  | 
                                                        |
| 230 | - *  | 
                                                        |
| 231 | - * @return null|string  | 
                                                        |
| 232 | - */  | 
                                                        |
| 233 | - public function getMime()  | 
                                                        |
| 234 | -    { | 
                                                        |
| 235 | -      if (!$this->mime) { | 
                                                        |
| 236 | - $this->mime = $this->getImageMime($this->_files['tmp_name']);  | 
                                                        |
| 237 | - }  | 
                                                        |
| 238 | -  | 
                                                        |
| 239 | - return $this->mime;  | 
                                                        |
| 240 | - }  | 
                                                        |
| 241 | -  | 
                                                        |
| 242 | - /**  | 
                                                        |
| 243 | - * Define a mime type for uploading.  | 
                                                        |
| 244 | - *  | 
                                                        |
| 245 | - * @param array $fileTypes  | 
                                                        |
| 246 | - *  | 
                                                        |
| 247 | - * @return $this  | 
                                                        |
| 248 | - */  | 
                                                        |
| 249 | - public function setMime(array $fileTypes)  | 
                                                        |
| 250 | -    { | 
                                                        |
| 251 | - $this->mimeTypes = $fileTypes;  | 
                                                        |
| 252 | - return $this;  | 
                                                        |
| 253 | - }  | 
                                                        |
| 254 | -  | 
                                                        |
| 255 | - /**  | 
                                                        |
| 256 | - * Gets the real image mime type.  | 
                                                        |
| 257 | - *  | 
                                                        |
| 258 | - * @param $tmp_name string The upload tmp directory  | 
                                                        |
| 259 | - *  | 
                                                        |
| 260 | - * @return null|string  | 
                                                        |
| 261 | - */  | 
                                                        |
| 262 | - protected function getImageMime($tmp_name)  | 
                                                        |
| 263 | -    { | 
                                                        |
| 264 | - $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)];  | 
                                                        |
| 265 | -      if (!$this->mime) { | 
                                                        |
| 266 | - return null;  | 
                                                        |
| 267 | - }  | 
                                                        |
| 268 | -  | 
                                                        |
| 269 | - return $this->mime;  | 
                                                        |
| 270 | - }  | 
                                                        |
| 271 | -  | 
                                                        |
| 272 | - /**  | 
                                                        |
| 273 | - * Returns error string  | 
                                                        |
| 274 | - *  | 
                                                        |
| 275 | - * @return string  | 
                                                        |
| 276 | - */  | 
                                                        |
| 277 | - public function getError()  | 
                                                        |
| 278 | -    { | 
                                                        |
| 279 | - return $this->error;  | 
                                                        |
| 280 | - }  | 
                                                        |
| 281 | -  | 
                                                        |
| 282 | - /**  | 
                                                        |
| 283 | - * Returns the image name.  | 
                                                        |
| 284 | - *  | 
                                                        |
| 285 | - * @return string  | 
                                                        |
| 286 | - */  | 
                                                        |
| 287 | - public function getName()  | 
                                                        |
| 288 | -    { | 
                                                        |
| 289 | - return $this->name;  | 
                                                        |
| 290 | - }  | 
                                                        |
| 291 | -  | 
                                                        |
| 292 | - /**  | 
                                                        |
| 293 | - * Provide image name if not provided.  | 
                                                        |
| 294 | - *  | 
                                                        |
| 295 | - * @param null $isNameProvided  | 
                                                        |
| 296 | - *  | 
                                                        |
| 297 | - * @return $this  | 
                                                        |
| 298 | - */  | 
                                                        |
| 299 | - public function setName($isNameProvided = null)  | 
                                                        |
| 300 | -    { | 
                                                        |
| 301 | -      if ($isNameProvided) { | 
                                                        |
| 302 | - $this->name = $isNameProvided;  | 
                                                        |
| 303 | -      }else{ | 
                                                        |
| 304 | -        $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); | 
                                                        |
| 305 | - }  | 
                                                        |
| 306 | -  | 
                                                        |
| 307 | - return $this;  | 
                                                        |
| 308 | - }  | 
                                                        |
| 309 | -  | 
                                                        |
| 310 | - /**  | 
                                                        |
| 311 | - * Returns the image width.  | 
                                                        |
| 312 | - *  | 
                                                        |
| 313 | - * @return int  | 
                                                        |
| 314 | - */  | 
                                                        |
| 315 | - public function getWidth()  | 
                                                        |
| 316 | -    { | 
                                                        |
| 317 | -      if ($this->width != null) { | 
                                                        |
| 318 | - return $this->width;  | 
                                                        |
| 319 | - }  | 
                                                        |
| 320 | -  | 
                                                        |
| 321 | - list($width) = getimagesize($this->_files['tmp_name']);  | 
                                                        |
| 322 | -  | 
                                                        |
| 323 | - return $width;  | 
                                                        |
| 324 | - }  | 
                                                        |
| 325 | -  | 
                                                        |
| 326 | - /**  | 
                                                        |
| 327 | - * Returns the image height in pixels.  | 
                                                        |
| 328 | - *  | 
                                                        |
| 329 | - * @return int  | 
                                                        |
| 330 | - */  | 
                                                        |
| 331 | - public function getHeight()  | 
                                                        |
| 332 | -    { | 
                                                        |
| 333 | -      if ($this->height != null) { | 
                                                        |
| 334 | - return $this->height;  | 
                                                        |
| 335 | - }  | 
                                                        |
| 336 | -  | 
                                                        |
| 337 | - list(, $height) = getimagesize($this->_files['tmp_name']);  | 
                                                        |
| 338 | -  | 
                                                        |
| 339 | - return $height;  | 
                                                        |
| 340 | - }  | 
                                                        |
| 341 | -  | 
                                                        |
| 342 | - /**  | 
                                                        |
| 343 | - * Returns the storage / folder name.  | 
                                                        |
| 344 | - *  | 
                                                        |
| 345 | - * @return string  | 
                                                        |
| 346 | - */  | 
                                                        |
| 347 | - public function getStorage()  | 
                                                        |
| 348 | -    { | 
                                                        |
| 349 | -      if (!$this->storage) { | 
                                                        |
| 350 | - $this->setStorage();  | 
                                                        |
| 351 | - }  | 
                                                        |
| 352 | -  | 
                                                        |
| 353 | - return $this->storage;  | 
                                                        |
| 354 | - }  | 
                                                        |
| 355 | -  | 
                                                        |
| 356 | - /**  | 
                                                        |
| 357 | - * Validate directory/permission before creating a folder.  | 
                                                        |
| 358 | - *  | 
                                                        |
| 359 | - * @param $dir string the folder name to check  | 
                                                        |
| 360 | - *  | 
                                                        |
| 361 | - * @return bool  | 
                                                        |
| 362 | - */  | 
                                                        |
| 363 | - private function isDirectoryValid($dir)  | 
                                                        |
| 364 | -    { | 
                                                        |
| 365 | - return !file_exists($dir) && !is_dir($dir) || is_writable($dir);  | 
                                                        |
| 366 | - }  | 
                                                        |
| 367 | -  | 
                                                        |
| 368 | - /**  | 
                                                        |
| 369 | - * Creates a storage for upload storage.  | 
                                                        |
| 370 | - *  | 
                                                        |
| 371 | - * @param $dir string the folder name to create  | 
                                                        |
| 372 | - * @param int $permission chmod permission  | 
                                                        |
| 373 | - *  | 
                                                        |
| 374 | - * @return $this  | 
                                                        |
| 375 | - */  | 
                                                        |
| 376 | - public function setStorage($dir = 'uploads', $permission = 0666)  | 
                                                        |
| 377 | -    { | 
                                                        |
| 378 | - $isDirectoryValid = $this->isDirectoryValid($dir);  | 
                                                        |
| 379 | -  | 
                                                        |
| 380 | -      if (!$isDirectoryValid) { | 
                                                        |
| 381 | - $this->error = 'Can not create a directory \''.$dir.'\', please check write permission';  | 
                                                        |
| 382 | - return false;  | 
                                                        |
| 383 | - }  | 
                                                        |
| 384 | -  | 
                                                        |
| 385 | -      $create = !is_dir($dir) ? @mkdir('' . $dir, (int) $permission, true) : true; | 
                                                        |
| 386 | -  | 
                                                        |
| 387 | -      if (!$create) { | 
                                                        |
| 388 | - $this->error = 'Error! directory \'' . $dir . '\' could not be created';  | 
                                                        |
| 389 | - return false;  | 
                                                        |
| 390 | - }  | 
                                                        |
| 391 | -  | 
                                                        |
| 392 | - $this->storage = $dir;  | 
                                                        |
| 393 | -  | 
                                                        |
| 394 | - return $this;  | 
                                                        |
| 395 | - }  | 
                                                        |
| 396 | -  | 
                                                        |
| 397 | - /**  | 
                                                        |
| 398 | - * Validate image size, dimension or mimetypes  | 
                                                        |
| 399 | - *  | 
                                                        |
| 400 | - * @return boolean  | 
                                                        |
| 401 | - */  | 
                                                        |
| 402 | - protected function constraintValidator()  | 
                                                        |
| 403 | -    { | 
                                                        |
| 404 | - /* check image for valid mime types and return mime */  | 
                                                        |
| 405 | - $this->getImageMime($this->_files['tmp_name']);  | 
                                                        |
| 406 | -  | 
                                                        |
| 407 | -  | 
                                                        |
| 408 | - /* validate image mime type */  | 
                                                        |
| 409 | -      if (!in_array($this->mime, $this->mimeTypes)) { | 
                                                        |
| 410 | -        $this->error = sprintf('Invalid File! Only (%s) image types are allowed', implode(', ', $this->mimeTypes)); | 
                                                        |
| 411 | - return false;  | 
                                                        |
| 412 | - }  | 
                                                        |
| 413 | -  | 
                                                        |
| 414 | - /* get image sizes */  | 
                                                        |
| 415 | - list($minSize, $maxSize) = $this->size;  | 
                                                        |
| 416 | -  | 
                                                        |
| 417 | -  | 
                                                        |
| 418 | - /* check image size based on the settings */  | 
                                                        |
| 419 | -      if ($this->_files['size'] < $minSize || $this->_files['size'] > $maxSize) { | 
                                                        |
| 420 | -        $min = $minSize.' bytes ('.intval($minSize / 1000).' kb)'; | 
                                                        |
| 421 | -        $max = $maxSize.' bytes ('.intval($maxSize / 1000).' kb)'; | 
                                                        |
| 422 | - $this->error = 'Image size should be minimum '.$min.', upto maximum '.$max;  | 
                                                        |
| 423 | - return false;  | 
                                                        |
| 424 | - }  | 
                                                        |
| 425 | -  | 
                                                        |
| 426 | - /* check image dimension */  | 
                                                        |
| 427 | - list($maxWidth, $maxHeight) = $this->dimensions;  | 
                                                        |
| 428 | - $this->width = $this->getWidth();  | 
                                                        |
| 429 | - $this->height = $this->getHeight();  | 
                                                        |
| 430 | -  | 
                                                        |
| 431 | -      if ($this->height > $maxHeight || $this->width > $maxWidth) { | 
                                                        |
| 432 | - $this->error = 'Image should be smaller than ' . $maxHeight . 'px in height, and smaller than ' . $maxWidth . 'px in width';  | 
                                                        |
| 433 | - return false;  | 
                                                        |
| 434 | - }  | 
                                                        |
| 435 | -  | 
                                                        |
| 436 | - return true;  | 
                                                        |
| 437 | - }  | 
                                                        |
| 438 | -  | 
                                                        |
| 439 | - /**  | 
                                                        |
| 440 | - * Validate and save (upload) file  | 
                                                        |
| 441 | - *  | 
                                                        |
| 442 | - * @return false|Image  | 
                                                        |
| 443 | - */  | 
                                                        |
| 444 | - public function upload()  | 
                                                        |
| 445 | -    { | 
                                                        |
| 446 | -      if ($this->error !== '') { | 
                                                        |
| 447 | - return false;  | 
                                                        |
| 448 | - }  | 
                                                        |
| 449 | -  | 
                                                        |
| 450 | - $isValid = $this->constraintValidator();  | 
                                                        |
| 451 | -  | 
                                                        |
| 452 | -  | 
                                                        |
| 453 | - $isSuccess = $isValid && $this->isSaved($this->_files['tmp_name'], $this->getPath());  | 
                                                        |
| 454 | -  | 
                                                        |
| 455 | - return $isSuccess ? $this : false;  | 
                                                        |
| 456 | - }  | 
                                                        |
| 457 | -  | 
                                                        |
| 458 | - /**  | 
                                                        |
| 459 | - * Final upload method to be called, isolated for testing purposes.  | 
                                                        |
| 460 | - *  | 
                                                        |
| 461 | - * @param $tmp_name int the temporary storage of the image file  | 
                                                        |
| 462 | - * @param $destination int upload destination  | 
                                                        |
| 463 | - *  | 
                                                        |
| 464 | - * @return bool  | 
                                                        |
| 465 | - */  | 
                                                        |
| 466 | - protected function isSaved($tmp_name, $destination)  | 
                                                        |
| 467 | -    { | 
                                                        |
| 468 | - return move_uploaded_file($tmp_name, $destination);  | 
                                                        |
| 469 | - }  | 
                                                        |
| 146 | + return false;  | 
                                                        |
| 147 | + }  | 
                                                        |
| 148 | +  | 
                                                        |
| 149 | + $this->_files = $this->_files[$offset];  | 
                                                        |
| 150 | +  | 
                                                        |
| 151 | + /* check for common upload errors */  | 
                                                        |
| 152 | +	  if (isset($this->_files['error'])) { | 
                                                        |
| 153 | + $this->error = $this->commonErrors[$this->_files['error']];  | 
                                                        |
| 154 | + }  | 
                                                        |
| 155 | +  | 
                                                        |
| 156 | + return $this->error ? false : true;  | 
                                                        |
| 157 | + }  | 
                                                        |
| 158 | +  | 
                                                        |
| 159 | + /**  | 
                                                        |
| 160 | + * Sets max image height and width limit.  | 
                                                        |
| 161 | + *  | 
                                                        |
| 162 | + * @param $maxWidth int max width value  | 
                                                        |
| 163 | + * @param $maxHeight int max height value  | 
                                                        |
| 164 | + *  | 
                                                        |
| 165 | + * @return $this  | 
                                                        |
| 166 | + */  | 
                                                        |
| 167 | + public function setDimension($maxWidth, $maxHeight)  | 
                                                        |
| 168 | +	{ | 
                                                        |
| 169 | + $this->dimensions = array($maxWidth, $maxHeight);  | 
                                                        |
| 170 | +  | 
                                                        |
| 171 | + return $this;  | 
                                                        |
| 172 | + }  | 
                                                        |
| 173 | +  | 
                                                        |
| 174 | + /**  | 
                                                        |
| 175 | + * Returns the full path of the image ex 'storage/image.mime'.  | 
                                                        |
| 176 | + *  | 
                                                        |
| 177 | + * @return string  | 
                                                        |
| 178 | + */  | 
                                                        |
| 179 | + public function getPath()  | 
                                                        |
| 180 | +	{ | 
                                                        |
| 181 | + return $this->path = $this->getStorage() . '/' . $this->getName() . '.' . $this->getMime();  | 
                                                        |
| 182 | + }  | 
                                                        |
| 183 | +  | 
                                                        |
| 184 | + /**  | 
                                                        |
| 185 | + * Returns the image size in bytes.  | 
                                                        |
| 186 | + *  | 
                                                        |
| 187 | + * @return int  | 
                                                        |
| 188 | + */  | 
                                                        |
| 189 | + public function getSize()  | 
                                                        |
| 190 | +	{ | 
                                                        |
| 191 | + return (int) $this->_files['size'];  | 
                                                        |
| 192 | + }  | 
                                                        |
| 193 | +  | 
                                                        |
| 194 | + /**  | 
                                                        |
| 195 | + * Define a min and max image size for uploading.  | 
                                                        |
| 196 | + *  | 
                                                        |
| 197 | + * @param $min int minimum value in bytes  | 
                                                        |
| 198 | + * @param $max int maximum value in bytes  | 
                                                        |
| 199 | + *  | 
                                                        |
| 200 | + * @return $this  | 
                                                        |
| 201 | + */  | 
                                                        |
| 202 | + public function setSize($min, $max)  | 
                                                        |
| 203 | +	{ | 
                                                        |
| 204 | + $this->size = array($min, $max);  | 
                                                        |
| 205 | + return $this;  | 
                                                        |
| 206 | + }  | 
                                                        |
| 207 | +  | 
                                                        |
| 208 | + /**  | 
                                                        |
| 209 | + * Returns a JSON format of the image width, height, name, mime ...  | 
                                                        |
| 210 | + *  | 
                                                        |
| 211 | + * @return string  | 
                                                        |
| 212 | + */  | 
                                                        |
| 213 | + public function getJson()  | 
                                                        |
| 214 | +	{ | 
                                                        |
| 215 | + return json_encode(  | 
                                                        |
| 216 | + array(  | 
                                                        |
| 217 | + 'name' => $this->name,  | 
                                                        |
| 218 | + 'mime' => $this->mime,  | 
                                                        |
| 219 | + 'height' => $this->height,  | 
                                                        |
| 220 | + 'width' => $this->width,  | 
                                                        |
| 221 | + 'size' => $this->_files['size'],  | 
                                                        |
| 222 | + 'storage' => $this->storage,  | 
                                                        |
| 223 | + 'path' => $this->path,  | 
                                                        |
| 224 | + )  | 
                                                        |
| 225 | + );  | 
                                                        |
| 226 | + }  | 
                                                        |
| 227 | +  | 
                                                        |
| 228 | + /**  | 
                                                        |
| 229 | + * Returns the image mime type.  | 
                                                        |
| 230 | + *  | 
                                                        |
| 231 | + * @return null|string  | 
                                                        |
| 232 | + */  | 
                                                        |
| 233 | + public function getMime()  | 
                                                        |
| 234 | +	{ | 
                                                        |
| 235 | +	  if (!$this->mime) { | 
                                                        |
| 236 | + $this->mime = $this->getImageMime($this->_files['tmp_name']);  | 
                                                        |
| 237 | + }  | 
                                                        |
| 238 | +  | 
                                                        |
| 239 | + return $this->mime;  | 
                                                        |
| 240 | + }  | 
                                                        |
| 241 | +  | 
                                                        |
| 242 | + /**  | 
                                                        |
| 243 | + * Define a mime type for uploading.  | 
                                                        |
| 244 | + *  | 
                                                        |
| 245 | + * @param array $fileTypes  | 
                                                        |
| 246 | + *  | 
                                                        |
| 247 | + * @return $this  | 
                                                        |
| 248 | + */  | 
                                                        |
| 249 | + public function setMime(array $fileTypes)  | 
                                                        |
| 250 | +	{ | 
                                                        |
| 251 | + $this->mimeTypes = $fileTypes;  | 
                                                        |
| 252 | + return $this;  | 
                                                        |
| 253 | + }  | 
                                                        |
| 254 | +  | 
                                                        |
| 255 | + /**  | 
                                                        |
| 256 | + * Gets the real image mime type.  | 
                                                        |
| 257 | + *  | 
                                                        |
| 258 | + * @param $tmp_name string The upload tmp directory  | 
                                                        |
| 259 | + *  | 
                                                        |
| 260 | + * @return null|string  | 
                                                        |
| 261 | + */  | 
                                                        |
| 262 | + protected function getImageMime($tmp_name)  | 
                                                        |
| 263 | +	{ | 
                                                        |
| 264 | + $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)];  | 
                                                        |
| 265 | +	  if (!$this->mime) { | 
                                                        |
| 266 | + return null;  | 
                                                        |
| 267 | + }  | 
                                                        |
| 268 | +  | 
                                                        |
| 269 | + return $this->mime;  | 
                                                        |
| 270 | + }  | 
                                                        |
| 271 | +  | 
                                                        |
| 272 | + /**  | 
                                                        |
| 273 | + * Returns error string  | 
                                                        |
| 274 | + *  | 
                                                        |
| 275 | + * @return string  | 
                                                        |
| 276 | + */  | 
                                                        |
| 277 | + public function getError()  | 
                                                        |
| 278 | +	{ | 
                                                        |
| 279 | + return $this->error;  | 
                                                        |
| 280 | + }  | 
                                                        |
| 281 | +  | 
                                                        |
| 282 | + /**  | 
                                                        |
| 283 | + * Returns the image name.  | 
                                                        |
| 284 | + *  | 
                                                        |
| 285 | + * @return string  | 
                                                        |
| 286 | + */  | 
                                                        |
| 287 | + public function getName()  | 
                                                        |
| 288 | +	{ | 
                                                        |
| 289 | + return $this->name;  | 
                                                        |
| 290 | + }  | 
                                                        |
| 291 | +  | 
                                                        |
| 292 | + /**  | 
                                                        |
| 293 | + * Provide image name if not provided.  | 
                                                        |
| 294 | + *  | 
                                                        |
| 295 | + * @param null $isNameProvided  | 
                                                        |
| 296 | + *  | 
                                                        |
| 297 | + * @return $this  | 
                                                        |
| 298 | + */  | 
                                                        |
| 299 | + public function setName($isNameProvided = null)  | 
                                                        |
| 300 | +	{ | 
                                                        |
| 301 | +	  if ($isNameProvided) { | 
                                                        |
| 302 | + $this->name = $isNameProvided;  | 
                                                        |
| 303 | +	  }else{ | 
                                                        |
| 304 | +		$this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); | 
                                                        |
| 305 | + }  | 
                                                        |
| 306 | +  | 
                                                        |
| 307 | + return $this;  | 
                                                        |
| 308 | + }  | 
                                                        |
| 309 | +  | 
                                                        |
| 310 | + /**  | 
                                                        |
| 311 | + * Returns the image width.  | 
                                                        |
| 312 | + *  | 
                                                        |
| 313 | + * @return int  | 
                                                        |
| 314 | + */  | 
                                                        |
| 315 | + public function getWidth()  | 
                                                        |
| 316 | +	{ | 
                                                        |
| 317 | +	  if ($this->width != null) { | 
                                                        |
| 318 | + return $this->width;  | 
                                                        |
| 319 | + }  | 
                                                        |
| 320 | +  | 
                                                        |
| 321 | + list($width) = getimagesize($this->_files['tmp_name']);  | 
                                                        |
| 322 | +  | 
                                                        |
| 323 | + return $width;  | 
                                                        |
| 324 | + }  | 
                                                        |
| 325 | +  | 
                                                        |
| 326 | + /**  | 
                                                        |
| 327 | + * Returns the image height in pixels.  | 
                                                        |
| 328 | + *  | 
                                                        |
| 329 | + * @return int  | 
                                                        |
| 330 | + */  | 
                                                        |
| 331 | + public function getHeight()  | 
                                                        |
| 332 | +	{ | 
                                                        |
| 333 | +	  if ($this->height != null) { | 
                                                        |
| 334 | + return $this->height;  | 
                                                        |
| 335 | + }  | 
                                                        |
| 336 | +  | 
                                                        |
| 337 | + list(, $height) = getimagesize($this->_files['tmp_name']);  | 
                                                        |
| 338 | +  | 
                                                        |
| 339 | + return $height;  | 
                                                        |
| 340 | + }  | 
                                                        |
| 341 | +  | 
                                                        |
| 342 | + /**  | 
                                                        |
| 343 | + * Returns the storage / folder name.  | 
                                                        |
| 344 | + *  | 
                                                        |
| 345 | + * @return string  | 
                                                        |
| 346 | + */  | 
                                                        |
| 347 | + public function getStorage()  | 
                                                        |
| 348 | +	{ | 
                                                        |
| 349 | +	  if (!$this->storage) { | 
                                                        |
| 350 | + $this->setStorage();  | 
                                                        |
| 351 | + }  | 
                                                        |
| 352 | +  | 
                                                        |
| 353 | + return $this->storage;  | 
                                                        |
| 354 | + }  | 
                                                        |
| 355 | +  | 
                                                        |
| 356 | + /**  | 
                                                        |
| 357 | + * Validate directory/permission before creating a folder.  | 
                                                        |
| 358 | + *  | 
                                                        |
| 359 | + * @param $dir string the folder name to check  | 
                                                        |
| 360 | + *  | 
                                                        |
| 361 | + * @return bool  | 
                                                        |
| 362 | + */  | 
                                                        |
| 363 | + private function isDirectoryValid($dir)  | 
                                                        |
| 364 | +	{ | 
                                                        |
| 365 | + return !file_exists($dir) && !is_dir($dir) || is_writable($dir);  | 
                                                        |
| 366 | + }  | 
                                                        |
| 367 | +  | 
                                                        |
| 368 | + /**  | 
                                                        |
| 369 | + * Creates a storage for upload storage.  | 
                                                        |
| 370 | + *  | 
                                                        |
| 371 | + * @param $dir string the folder name to create  | 
                                                        |
| 372 | + * @param int $permission chmod permission  | 
                                                        |
| 373 | + *  | 
                                                        |
| 374 | + * @return $this  | 
                                                        |
| 375 | + */  | 
                                                        |
| 376 | + public function setStorage($dir = 'uploads', $permission = 0666)  | 
                                                        |
| 377 | +	{ | 
                                                        |
| 378 | + $isDirectoryValid = $this->isDirectoryValid($dir);  | 
                                                        |
| 379 | +  | 
                                                        |
| 380 | +	  if (!$isDirectoryValid) { | 
                                                        |
| 381 | + $this->error = 'Can not create a directory \''.$dir.'\', please check write permission';  | 
                                                        |
| 382 | + return false;  | 
                                                        |
| 383 | + }  | 
                                                        |
| 384 | +  | 
                                                        |
| 385 | +	  $create = !is_dir($dir) ? @mkdir('' . $dir, (int) $permission, true) : true; | 
                                                        |
| 386 | +  | 
                                                        |
| 387 | +	  if (!$create) { | 
                                                        |
| 388 | + $this->error = 'Error! directory \'' . $dir . '\' could not be created';  | 
                                                        |
| 389 | + return false;  | 
                                                        |
| 390 | + }  | 
                                                        |
| 391 | +  | 
                                                        |
| 392 | + $this->storage = $dir;  | 
                                                        |
| 393 | +  | 
                                                        |
| 394 | + return $this;  | 
                                                        |
| 395 | + }  | 
                                                        |
| 396 | +  | 
                                                        |
| 397 | + /**  | 
                                                        |
| 398 | + * Validate image size, dimension or mimetypes  | 
                                                        |
| 399 | + *  | 
                                                        |
| 400 | + * @return boolean  | 
                                                        |
| 401 | + */  | 
                                                        |
| 402 | + protected function constraintValidator()  | 
                                                        |
| 403 | +	{ | 
                                                        |
| 404 | + /* check image for valid mime types and return mime */  | 
                                                        |
| 405 | + $this->getImageMime($this->_files['tmp_name']);  | 
                                                        |
| 406 | +  | 
                                                        |
| 407 | +  | 
                                                        |
| 408 | + /* validate image mime type */  | 
                                                        |
| 409 | +	  if (!in_array($this->mime, $this->mimeTypes)) { | 
                                                        |
| 410 | +		$this->error = sprintf('Invalid File! Only (%s) image types are allowed', implode(', ', $this->mimeTypes)); | 
                                                        |
| 411 | + return false;  | 
                                                        |
| 412 | + }  | 
                                                        |
| 413 | +  | 
                                                        |
| 414 | + /* get image sizes */  | 
                                                        |
| 415 | + list($minSize, $maxSize) = $this->size;  | 
                                                        |
| 416 | +  | 
                                                        |
| 417 | +  | 
                                                        |
| 418 | + /* check image size based on the settings */  | 
                                                        |
| 419 | +	  if ($this->_files['size'] < $minSize || $this->_files['size'] > $maxSize) { | 
                                                        |
| 420 | +		$min = $minSize.' bytes ('.intval($minSize / 1000).' kb)'; | 
                                                        |
| 421 | +		$max = $maxSize.' bytes ('.intval($maxSize / 1000).' kb)'; | 
                                                        |
| 422 | + $this->error = 'Image size should be minimum '.$min.', upto maximum '.$max;  | 
                                                        |
| 423 | + return false;  | 
                                                        |
| 424 | + }  | 
                                                        |
| 425 | +  | 
                                                        |
| 426 | + /* check image dimension */  | 
                                                        |
| 427 | + list($maxWidth, $maxHeight) = $this->dimensions;  | 
                                                        |
| 428 | + $this->width = $this->getWidth();  | 
                                                        |
| 429 | + $this->height = $this->getHeight();  | 
                                                        |
| 430 | +  | 
                                                        |
| 431 | +	  if ($this->height > $maxHeight || $this->width > $maxWidth) { | 
                                                        |
| 432 | + $this->error = 'Image should be smaller than ' . $maxHeight . 'px in height, and smaller than ' . $maxWidth . 'px in width';  | 
                                                        |
| 433 | + return false;  | 
                                                        |
| 434 | + }  | 
                                                        |
| 435 | +  | 
                                                        |
| 436 | + return true;  | 
                                                        |
| 437 | + }  | 
                                                        |
| 438 | +  | 
                                                        |
| 439 | + /**  | 
                                                        |
| 440 | + * Validate and save (upload) file  | 
                                                        |
| 441 | + *  | 
                                                        |
| 442 | + * @return false|Image  | 
                                                        |
| 443 | + */  | 
                                                        |
| 444 | + public function upload()  | 
                                                        |
| 445 | +	{ | 
                                                        |
| 446 | +	  if ($this->error !== '') { | 
                                                        |
| 447 | + return false;  | 
                                                        |
| 448 | + }  | 
                                                        |
| 449 | +  | 
                                                        |
| 450 | + $isValid = $this->constraintValidator();  | 
                                                        |
| 451 | +  | 
                                                        |
| 452 | +  | 
                                                        |
| 453 | + $isSuccess = $isValid && $this->isSaved($this->_files['tmp_name'], $this->getPath());  | 
                                                        |
| 454 | +  | 
                                                        |
| 455 | + return $isSuccess ? $this : false;  | 
                                                        |
| 456 | + }  | 
                                                        |
| 457 | +  | 
                                                        |
| 458 | + /**  | 
                                                        |
| 459 | + * Final upload method to be called, isolated for testing purposes.  | 
                                                        |
| 460 | + *  | 
                                                        |
| 461 | + * @param $tmp_name int the temporary storage of the image file  | 
                                                        |
| 462 | + * @param $destination int upload destination  | 
                                                        |
| 463 | + *  | 
                                                        |
| 464 | + * @return bool  | 
                                                        |
| 465 | + */  | 
                                                        |
| 466 | + protected function isSaved($tmp_name, $destination)  | 
                                                        |
| 467 | +	{ | 
                                                        |
| 468 | + return move_uploaded_file($tmp_name, $destination);  | 
                                                        |
| 469 | + }  | 
                                                        |
| 470 | 470 | }  | 
                                                        
| 471 | 471 | \ No newline at end of file  | 
                                                        
@@ -178,7 +178,7 @@ discard block  | 
                                                    ||
| 178 | 178 | */  | 
                                                        
| 179 | 179 | public function getPath()  | 
                                                        
| 180 | 180 |      { | 
                                                        
| 181 | - return $this->path = $this->getStorage() . '/' . $this->getName() . '.' . $this->getMime();  | 
                                                        |
| 181 | + return $this->path = $this->getStorage().'/'.$this->getName().'.'.$this->getMime();  | 
                                                        |
| 182 | 182 | }  | 
                                                        
| 183 | 183 | |
| 184 | 184 | /**  | 
                                                        
@@ -300,8 +300,8 @@ discard block  | 
                                                    ||
| 300 | 300 |      { | 
                                                        
| 301 | 301 |        if ($isNameProvided) { | 
                                                        
| 302 | 302 | $this->name = $isNameProvided;  | 
                                                        
| 303 | -      }else{ | 
                                                        |
| 304 | -        $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); | 
                                                        |
| 303 | +      } else { | 
                                                        |
| 304 | +        $this->name = uniqid('', true).'_'.str_shuffle(implode(range('e', 'q'))); | 
                                                        |
| 305 | 305 | }  | 
                                                        
| 306 | 306 | |
| 307 | 307 | return $this;  | 
                                                        
@@ -382,10 +382,10 @@ discard block  | 
                                                    ||
| 382 | 382 | return false;  | 
                                                        
| 383 | 383 | }  | 
                                                        
| 384 | 384 | |
| 385 | -      $create = !is_dir($dir) ? @mkdir('' . $dir, (int) $permission, true) : true; | 
                                                        |
| 385 | +      $create = !is_dir($dir) ? @mkdir(''.$dir, (int) $permission, true) : true; | 
                                                        |
| 386 | 386 | |
| 387 | 387 |        if (!$create) { | 
                                                        
| 388 | - $this->error = 'Error! directory \'' . $dir . '\' could not be created';  | 
                                                        |
| 388 | + $this->error = 'Error! directory \''.$dir.'\' could not be created';  | 
                                                        |
| 389 | 389 | return false;  | 
                                                        
| 390 | 390 | }  | 
                                                        
| 391 | 391 | |
@@ -429,7 +429,7 @@ discard block  | 
                                                    ||
| 429 | 429 | $this->height = $this->getHeight();  | 
                                                        
| 430 | 430 | |
| 431 | 431 |        if ($this->height > $maxHeight || $this->width > $maxWidth) { | 
                                                        
| 432 | - $this->error = 'Image should be smaller than ' . $maxHeight . 'px in height, and smaller than ' . $maxWidth . 'px in width';  | 
                                                        |
| 432 | + $this->error = 'Image should be smaller than '.$maxHeight.'px in height, and smaller than '.$maxWidth.'px in width';  | 
                                                        |
| 433 | 433 | return false;  | 
                                                        
| 434 | 434 | }  | 
                                                        
| 435 | 435 | |
@@ -96,8 +96,7 @@ discard block  | 
                                                    ||
| 96 | 96 | /**  | 
                                                        
| 97 | 97 | * @param array $_files represents the $_FILES array passed as dependency  | 
                                                        
| 98 | 98 | */  | 
                                                        
| 99 | - public function __construct(array $_files = array())  | 
                                                        |
| 100 | -    { | 
                                                        |
| 99 | +    public function __construct(array $_files = array()) { | 
                                                        |
| 101 | 100 | |
| 102 | 101 | /* check if php_exif is enabled */  | 
                                                        
| 103 | 102 |          if (!function_exists('exif_imagetype')) { | 
                                                        
@@ -164,8 +163,7 @@ discard block  | 
                                                    ||
| 164 | 163 | *  | 
                                                        
| 165 | 164 | * @return $this  | 
                                                        
| 166 | 165 | */  | 
                                                        
| 167 | - public function setDimension($maxWidth, $maxHeight)  | 
                                                        |
| 168 | -    { | 
                                                        |
| 166 | +    public function setDimension($maxWidth, $maxHeight) { | 
                                                        |
| 169 | 167 | $this->dimensions = array($maxWidth, $maxHeight);  | 
                                                        
| 170 | 168 | |
| 171 | 169 | return $this;  | 
                                                        
@@ -176,8 +174,7 @@ discard block  | 
                                                    ||
| 176 | 174 | *  | 
                                                        
| 177 | 175 | * @return string  | 
                                                        
| 178 | 176 | */  | 
                                                        
| 179 | - public function getPath()  | 
                                                        |
| 180 | -    { | 
                                                        |
| 177 | +    public function getPath() { | 
                                                        |
| 181 | 178 | return $this->path = $this->getStorage() . '/' . $this->getName() . '.' . $this->getMime();  | 
                                                        
| 182 | 179 | }  | 
                                                        
| 183 | 180 | |
@@ -186,8 +183,7 @@ discard block  | 
                                                    ||
| 186 | 183 | *  | 
                                                        
| 187 | 184 | * @return int  | 
                                                        
| 188 | 185 | */  | 
                                                        
| 189 | - public function getSize()  | 
                                                        |
| 190 | -    { | 
                                                        |
| 186 | +    public function getSize() { | 
                                                        |
| 191 | 187 | return (int) $this->_files['size'];  | 
                                                        
| 192 | 188 | }  | 
                                                        
| 193 | 189 | |
@@ -199,8 +195,7 @@ discard block  | 
                                                    ||
| 199 | 195 | *  | 
                                                        
| 200 | 196 | * @return $this  | 
                                                        
| 201 | 197 | */  | 
                                                        
| 202 | - public function setSize($min, $max)  | 
                                                        |
| 203 | -    { | 
                                                        |
| 198 | +    public function setSize($min, $max) { | 
                                                        |
| 204 | 199 | $this->size = array($min, $max);  | 
                                                        
| 205 | 200 | return $this;  | 
                                                        
| 206 | 201 | }  | 
                                                        
@@ -210,8 +205,7 @@ discard block  | 
                                                    ||
| 210 | 205 | *  | 
                                                        
| 211 | 206 | * @return string  | 
                                                        
| 212 | 207 | */  | 
                                                        
| 213 | - public function getJson()  | 
                                                        |
| 214 | -    { | 
                                                        |
| 208 | +    public function getJson() { | 
                                                        |
| 215 | 209 | return json_encode(  | 
                                                        
| 216 | 210 | array(  | 
                                                        
| 217 | 211 | 'name' => $this->name,  | 
                                                        
@@ -230,8 +224,7 @@ discard block  | 
                                                    ||
| 230 | 224 | *  | 
                                                        
| 231 | 225 | * @return null|string  | 
                                                        
| 232 | 226 | */  | 
                                                        
| 233 | - public function getMime()  | 
                                                        |
| 234 | -    { | 
                                                        |
| 227 | +    public function getMime() { | 
                                                        |
| 235 | 228 |        if (!$this->mime) { | 
                                                        
| 236 | 229 | $this->mime = $this->getImageMime($this->_files['tmp_name']);  | 
                                                        
| 237 | 230 | }  | 
                                                        
@@ -246,8 +239,7 @@ discard block  | 
                                                    ||
| 246 | 239 | *  | 
                                                        
| 247 | 240 | * @return $this  | 
                                                        
| 248 | 241 | */  | 
                                                        
| 249 | - public function setMime(array $fileTypes)  | 
                                                        |
| 250 | -    { | 
                                                        |
| 242 | +    public function setMime(array $fileTypes) { | 
                                                        |
| 251 | 243 | $this->mimeTypes = $fileTypes;  | 
                                                        
| 252 | 244 | return $this;  | 
                                                        
| 253 | 245 | }  | 
                                                        
@@ -259,8 +251,7 @@ discard block  | 
                                                    ||
| 259 | 251 | *  | 
                                                        
| 260 | 252 | * @return null|string  | 
                                                        
| 261 | 253 | */  | 
                                                        
| 262 | - protected function getImageMime($tmp_name)  | 
                                                        |
| 263 | -    { | 
                                                        |
| 254 | +    protected function getImageMime($tmp_name) { | 
                                                        |
| 264 | 255 | $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)];  | 
                                                        
| 265 | 256 |        if (!$this->mime) { | 
                                                        
| 266 | 257 | return null;  | 
                                                        
@@ -274,8 +265,7 @@ discard block  | 
                                                    ||
| 274 | 265 | *  | 
                                                        
| 275 | 266 | * @return string  | 
                                                        
| 276 | 267 | */  | 
                                                        
| 277 | - public function getError()  | 
                                                        |
| 278 | -    { | 
                                                        |
| 268 | +    public function getError() { | 
                                                        |
| 279 | 269 | return $this->error;  | 
                                                        
| 280 | 270 | }  | 
                                                        
| 281 | 271 | |
@@ -284,8 +274,7 @@ discard block  | 
                                                    ||
| 284 | 274 | *  | 
                                                        
| 285 | 275 | * @return string  | 
                                                        
| 286 | 276 | */  | 
                                                        
| 287 | - public function getName()  | 
                                                        |
| 288 | -    { | 
                                                        |
| 277 | +    public function getName() { | 
                                                        |
| 289 | 278 | return $this->name;  | 
                                                        
| 290 | 279 | }  | 
                                                        
| 291 | 280 | |
@@ -296,11 +285,10 @@ discard block  | 
                                                    ||
| 296 | 285 | *  | 
                                                        
| 297 | 286 | * @return $this  | 
                                                        
| 298 | 287 | */  | 
                                                        
| 299 | - public function setName($isNameProvided = null)  | 
                                                        |
| 300 | -    { | 
                                                        |
| 288 | +    public function setName($isNameProvided = null) { | 
                                                        |
| 301 | 289 |        if ($isNameProvided) { | 
                                                        
| 302 | 290 | $this->name = $isNameProvided;  | 
                                                        
| 303 | -      }else{ | 
                                                        |
| 291 | +      } else{ | 
                                                        |
| 304 | 292 |          $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); | 
                                                        
| 305 | 293 | }  | 
                                                        
| 306 | 294 | |
@@ -312,8 +300,7 @@ discard block  | 
                                                    ||
| 312 | 300 | *  | 
                                                        
| 313 | 301 | * @return int  | 
                                                        
| 314 | 302 | */  | 
                                                        
| 315 | - public function getWidth()  | 
                                                        |
| 316 | -    { | 
                                                        |
| 303 | +    public function getWidth() { | 
                                                        |
| 317 | 304 |        if ($this->width != null) { | 
                                                        
| 318 | 305 | return $this->width;  | 
                                                        
| 319 | 306 | }  | 
                                                        
@@ -328,8 +315,7 @@ discard block  | 
                                                    ||
| 328 | 315 | *  | 
                                                        
| 329 | 316 | * @return int  | 
                                                        
| 330 | 317 | */  | 
                                                        
| 331 | - public function getHeight()  | 
                                                        |
| 332 | -    { | 
                                                        |
| 318 | +    public function getHeight() { | 
                                                        |
| 333 | 319 |        if ($this->height != null) { | 
                                                        
| 334 | 320 | return $this->height;  | 
                                                        
| 335 | 321 | }  | 
                                                        
@@ -344,8 +330,7 @@ discard block  | 
                                                    ||
| 344 | 330 | *  | 
                                                        
| 345 | 331 | * @return string  | 
                                                        
| 346 | 332 | */  | 
                                                        
| 347 | - public function getStorage()  | 
                                                        |
| 348 | -    { | 
                                                        |
| 333 | +    public function getStorage() { | 
                                                        |
| 349 | 334 |        if (!$this->storage) { | 
                                                        
| 350 | 335 | $this->setStorage();  | 
                                                        
| 351 | 336 | }  | 
                                                        
@@ -360,8 +345,7 @@ discard block  | 
                                                    ||
| 360 | 345 | *  | 
                                                        
| 361 | 346 | * @return bool  | 
                                                        
| 362 | 347 | */  | 
                                                        
| 363 | - private function isDirectoryValid($dir)  | 
                                                        |
| 364 | -    { | 
                                                        |
| 348 | +    private function isDirectoryValid($dir) { | 
                                                        |
| 365 | 349 | return !file_exists($dir) && !is_dir($dir) || is_writable($dir);  | 
                                                        
| 366 | 350 | }  | 
                                                        
| 367 | 351 | |
@@ -373,8 +357,7 @@ discard block  | 
                                                    ||
| 373 | 357 | *  | 
                                                        
| 374 | 358 | * @return $this  | 
                                                        
| 375 | 359 | */  | 
                                                        
| 376 | - public function setStorage($dir = 'uploads', $permission = 0666)  | 
                                                        |
| 377 | -    { | 
                                                        |
| 360 | +    public function setStorage($dir = 'uploads', $permission = 0666) { | 
                                                        |
| 378 | 361 | $isDirectoryValid = $this->isDirectoryValid($dir);  | 
                                                        
| 379 | 362 | |
| 380 | 363 |        if (!$isDirectoryValid) { | 
                                                        
@@ -399,8 +382,7 @@ discard block  | 
                                                    ||
| 399 | 382 | *  | 
                                                        
| 400 | 383 | * @return boolean  | 
                                                        
| 401 | 384 | */  | 
                                                        
| 402 | - protected function constraintValidator()  | 
                                                        |
| 403 | -    { | 
                                                        |
| 385 | +    protected function constraintValidator() { | 
                                                        |
| 404 | 386 | /* check image for valid mime types and return mime */  | 
                                                        
| 405 | 387 | $this->getImageMime($this->_files['tmp_name']);  | 
                                                        
| 406 | 388 | |
@@ -441,8 +423,7 @@ discard block  | 
                                                    ||
| 441 | 423 | *  | 
                                                        
| 442 | 424 | * @return false|Image  | 
                                                        
| 443 | 425 | */  | 
                                                        
| 444 | - public function upload()  | 
                                                        |
| 445 | -    { | 
                                                        |
| 426 | +    public function upload() { | 
                                                        |
| 446 | 427 |        if ($this->error !== '') { | 
                                                        
| 447 | 428 | return false;  | 
                                                        
| 448 | 429 | }  | 
                                                        
@@ -463,8 +444,7 @@ discard block  | 
                                                    ||
| 463 | 444 | *  | 
                                                        
| 464 | 445 | * @return bool  | 
                                                        
| 465 | 446 | */  | 
                                                        
| 466 | - protected function isSaved($tmp_name, $destination)  | 
                                                        |
| 467 | -    { | 
                                                        |
| 447 | +    protected function isSaved($tmp_name, $destination) { | 
                                                        |
| 468 | 448 | return move_uploaded_file($tmp_name, $destination);  | 
                                                        
| 469 | 449 | }  | 
                                                        
| 470 | 450 | }  | 
                                                        
| 471 | 451 | \ No newline at end of file  |