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