Recently I am developing my component library to make my library rich. Because it’s reduce my production time. For this reason I have developed the image uploading class. Almost every application need image uploading part. So that I have decided to make a class to for image uploading. Which will is very easy to use and no need to extra settings. Just copy the script and use it. I have made it open source so anyone can use it.
<?php
/*
* Image Upload Class
*/
class ImageUpload
{
/**
* @param null $Path Custom path where uploaded image will be stored
* @param null $MaximumSize Custom Maximum size in (Bytes) will be passed.
* @param array $AllowedType Custom extension type (JPG|GIF|PNG)
* @param null $Option Extra options as array where we can define 'Image File Name', blah blah blah
*
* @return string Here return/output will be a string (Message)
*/
public function ImgUpload($Path = null, $MaximumSize = null, array $AllowedType = null, $Option = null)
{
if (isset($_POST['submit'])) {
if (!empty($_FILES['ImgName']['name'])) {
$ImageFile = $_FILES['ImgName'];
if ($this->CheckSizeLimit($ImageFile, $MaximumSize)) {
if ($this->CheckImageType($ImageFile, $AllowedType)) {
if ($this->CheckPath($Path)) {
if ($this->CheckPermission($Path)) {
if (move_uploaded_file($ImageFile['tmp_name'], $Path . $ImageFile['name'])) {
$result = 'Image has been Uploaded';
return $result;
} else {
return false;
}
} else {
return "Directory do not have write permission,";
}
} else {
return "Directory could not found,";
}
} else {
if (isset($AllowedType)) {
return "Please select " . implode('"/"', $AllowedType) . " Image";
} else {
return "Please select jpg/png/gif image";
}
}
} else {
$result = "Image must me less then" . $MaximumSize . " byte";
return $result;
}
} else {
$result = 'You do not select any file';
return $result;
}
}
}
/**
* @param $ImageFile Image File which is image is selected for upload
* @param null $MaximumSize Custom max image size passed
*
* @return bool Return true or false
*/
function CheckSizeLimit($ImageFile, $MaximumSize = null)
{
if (isset($MaximumSize)) {
if ($ImageFile['size'] < $MaximumSize) {
return $ImageFile['size'];
} else {
return false;
}
} else {
return true;
}
}
/**
* @param $ImageFile Image File which is image is selected for upload
* @param array $AllowedType Custom extension type (JPG|GIF|PNG)
*
* @return bool Return true or false
*/
function CheckImageType($ImageFile, array $AllowedType = null)
{
if (isset($AllowedType)) {
$i = 0;
while ($i < sizeof($AllowedType)) {
if ($ImageFile['type'] == "image/" . $AllowedType[$i]) {
return true;
break;
}
$i++;
}
return true;
} else {
if ($ImageFile['type'] == "image/jpg" || $ImageFile['type'] = "image/png" || $ImageFile['type'] == "image/gif") {
return true;
} else {
return false;
}
}
}
/**
* @param null Custom path where uploaded image will be stored
*
* @return bool Return true or false
*/
function CheckPath($Path = null)
{
if (isset($Path)) {
if (is_dir($Path)) {
return true;
} else {
return false;
}
} else {
return true;
}
}
/**
* @param null Custom path where uploaded image will be stored
*
* @return bool Return true or false
*/
function CheckPermission($Path = null)
{
if (isset($Path)) {
if (is_writable($Path)) {
return true;
} else {
return false;
}
} else {
return true;
}
}
protected function ImgSize($ImgFile = null)
{
return "OK";
}
}
This is the class structure. you can get this code from github. There I have written more details about it. I hope this will be helpful for you development.