logo

PHP image upload with Thumbnail

image uploading

It is an easy task to upload image files and create its smaller version as thumbnail.
Thumbnails are smaller version of Images to reduce its size to use these smaller version of images where small image can work well instead of using actual image which is bigger in size and dimensions.

this method results into optimized overall page-load.

To start uploading file we have to create a form with attribute enctype=”multipart/form-data” without this attribute image will not be posted.

Here I will be taking a basic form

when the form is submitted we can upload file by 1 line of code

but before uploading we must check the following to handle errors and keep it secure.

  1. Check if a file is selected, else skip file uploading and output a message
  2. Check if provided file is valid image i.e (jpg, png etc)
  3. Check if file is not too big on wamp default max-file-size is 8MB

You can check other things like image with and height or any thing else but these are most common things and must be checked.

here we will be creating an empty array of errors and checking above things if above validations does not meet, we will add an item to errors array. after validations we will be checking errors array if it is empty that means all validations were ok and valid we will upload file, else we will display message from errors array.

NOTE: you need to have images folder at same path where upload_script.php resides and a thumbs folder inside images folder.

your validation criteria and flow of project may be different, customize it according to your needs, I have just provided the basic method for how to upload image file using PHP and create its thumbnail on the fly.
you can also make multiple versions of thumbnails like 32×32 100×100 and 300×300 etc

Comments