Recently i had to work with image uploading on my some projects. The issue was to upload the user avatar and rename the avatars with their userid. So that it will be easy to work with avatar system.
I first used this idea to one of my project and i did this with procedural method. But again on my current projects i need this facility again. So i thought to finish the job for the last time and decided to make a class to re-use the codes. So here i go…
Firstly, i use my class constructor to define the maximum file size, width, height and also the upload directory. So now create an instance of my class like this-
<?php
//$image = new ImageUloader($max_size, $max_width, $max_height, $upload_dir)
$image = new ImageUploader(26, 200, 150, 'images/avatar/');
?>
Now you need to set the image name to upload. Don’t be afraid, it’s the file input field name of your html form if you have an input file name like this -
<form enctype="multipart/form-data" action="" method="POST">
<input name="input_field_name" type="file" />
<input type="submit" name="submit" value="Change Avatar" />
</form>
Then you need to setup the image name like this
<?php
$image->setImage('input_field_name');
?>
Now it’s time for validation process :D
you can check the image size, width, height and also file type by the following method
<?php
$image->checkSize();
$image->checkHeight();
$image->checkWidth();
$image->checkExt();
?>
These methods return true, if the conditions are passed. We have permitted to use only jpg, jpeg, png and gif files. So let’s use them
<?php
if(!$image->checkSize())
$errors[] = "File size is Big";
if(!$image->checkHeight())
$errors[] = "File height is Big";
if(!$image->checkWidth())
$errors[] = "File width is Big";
if(!$image->checkExt())
$errors[] = "File ext is not supported";
?>
So if the conditions are passed, we will set the name of the file to be uploaded and now it’s the time to check if there is any file with the same user id. If there is any file with same user id, we delete them for the sake of our need and upload them. By deleting the existing file with same user id clears the confusion of repeating same file name with same user id. Because, there is a possibility to find images with same file name but with different extensions.
<?php
if(!isset($errors)){
$image->setImageName($userid);
$image->deleteExisting();
$image->upload();
echo "<h2>Avatar Changed Successfully</h2>";
}
else{
echo "<h2>You must correct the errors to proceed</h2><br>";
print_r($errors);
}
?>
So here is the complete code for validating and uploading
<?php
if(isset($_POST['submit'])){
require 'class.imageupload.php';
//$image = new ImageUloader($max_size, $max_width, $max_height, $upload_dir)
$image = new ImageUploader(26, 200, 150, 'images/avatar/');
$image->setImage('input_field_name');
if(!$image->checkSize())
$errors[] = "File size is Big";
if(!$image->checkHeight())
$errors[] = "File height is Big";
if(!$image->checkWidth())
$errors[] = "File width is Big";
if(!$image->checkExt())
$errors[] = "File ext is not supported";
if(!isset($errors)){
$image->setImageName($userid);
$image->deleteExisting();
$image->upload();
echo "<h2>Avatar Changed Successfully</h2>";
}
else{
echo "<h2>You must correct the errors to proceed</h2><br>";
print_r($errors);
}
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<input name="input_field_name" type="file" />
<input type="submit" name="submit" value="Change Avatar" />
</form>
Download the class from here
3 thoughts on “Image upload & Validation: My first class to phpclasses.org”
Awesome script easy and clean,
But im having trouble with the $max_size,
I´m setting my value to 50 wich should be 512kb, right ?
No been able to upload PNG image that is 18kb
Any suggestions ?
If you set
$max_sizeto 50, that is equivalent to 50kb. May be you are missing something, check again.Hi, first thanks for nice upload with validation.
I think is there one mistake in the class.imageupload.php file.
About line 158, now is the "height" parameter, should be "width".
However, thanks for your code,
greetings.