public function upload()
{
/* modify variable names for convenience */
$image = $this;
$files = $this->_files;
/* initialize image properties */
$image->name = $image->getName();
$image->width = $image->getWidth();
$image->height = $image->getHeight();
$image->location = $image->getLocation();
/* get image sizes */
list($minSize, $maxSize) = $image->size;
/* check for common upload errors */
if ($image->error = $image->uploadErrors($files["error"])) {
return;
}
/* check image for valid mime types and return mime */
$image->mime = $image->getImageMime($files["tmp_name"]);
/* validate image mime type */
if (!in_array($image->mime, $image->mimeTypes)) {
$ext = implode(", ", $image->mimeTypes);
$image->error = "Invalid File! Only ({$ext}) image types are allowed";
return;
}
/* check image size based on the settings */
if ($files["size"] < $minSize || $files["size"] > $maxSize) {
$min = intval($minSize / 1000) ?: 1;
$max = intval($maxSize / 1000);
$image->error = "Image size should be atleast more than min: {$min} and less than max: {$max} kb";
return;
}
/* check image dimension */
list($allowedWidth, $allowedHeight) = $image->dimensions;
if ($image->height > $allowedHeight || $image->width > $allowedWidth) {
$image->error = "Image height/width should be less than ' {$allowedHeight} \\ {$allowedWidth} ' pixels";
return;
}
if ($image->height < 4 || $image->width < 4) {
$image->error = "Invalid! Image height/width is too small or maybe corrupted";
return;
}
/* set and get folder name */
$image->fullPath = $image->location . "/" . $image->name . "." . $image->mime;
/* gather image info for json storage */
$image->serialize = array("name" => $image->name, "mime" => $image->mime, "height" => $image->height, "width" => $image->width, "size" => $files["size"], "location" => $image->location, "fullpath" => $image->fullPath);
if ($image->error === "") {
$moveUpload = $image->moveUploadedFile($files["tmp_name"], $image->fullPath);
if (false !== $moveUpload) {
return $image;
}
}
$image->error = "Upload failed, Unknown error occured";
return false;
}