PHP image upload with Thumbnail
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
1 2 3 4 5 |
<form action="upload_script.php" method="post" enctype="multipart/form-data"> Browse an image to upload: <input type="file" name="image" id="txtFile" /> <input type="submit" value="Upload Image" /> </form> |
when the form is submitted we can upload file by 1 line of code
1 |
move_uploaded_file($_FILES["image"]["tmp_name"], 'path/to/upload/directory/filename.jpg'); |
but before uploading we must check the following to handle errors and keep it secure.
- Check if a file is selected, else skip file uploading and output a message
- Check if provided file is valid image i.e (jpg, png etc)
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
//**********file name upload_script.php********** //errors array $errors=array(); if(!empty($_FILES["image"]['name'])) { //get provided file information $fileName = $_FILES["image"]['name']; $fileExtArr = explode('.',$fileName);//make array of file.name.ext as array(file,name,ext) $fileExt = strtolower(end($fileExtArr));//get last item of array of user file input $fileSize = $_FILES["image"]['size']; $fileTmp = $_FILES["image"]['tmp_name']; //which files we accept $allowed_files('jpg','png','gif'); //validate file size if($fileSize > (1024*1024*2)){ $errors[] = 'Maximum 2MB files are allowed'; } //validating file extension if(!in_array($fileExt,$allowed_files)){ $errors[] = 'only ('.implode(', ',$allowed_files).') files are allowed.'; } //do other validations here if you need more //before uploading we will look at errors array if empty if(empty($errors)){ move_uploaded_file($fileTmp, 'images/'.$fileName); //here we can create thumbnails by create_thumb() function //it takes 5 parametes //1- original image, 2- file extension, 3-thumb full path, 4- max width of thumb, 5-max height of thumb create_thumb('images/'.$fileName,$fileExt,'images/thumbs/thumb_'.$fileName,200,200); echo 'File uploaded successfully.'; header('Location:index.php'); }else{ echo 'Some Error Occured: <br>'.implode('<br>',$errors); } }else{ $errors[] = 'No Image is provided.'; } //function to create thumbnail create_thumb($target,$ext,$thumb_path,$w,$h){ list($w_orig,$h_orig)=getimagesize($target); $scale_ratio=$w_orig/$h_orig; if(($w/$h)>$scale_ratio) $w=$h*$scale_ratio; else $h=$w/$scale_ratio; if($w_orig<=$w){ $w=$w_orig; $h=$h_orig; } $img=""; if($ext=="gif") $img=imagecreatefromgif($target); else if($ext=="png") $img=imagecreatefrompng($target); else if($ext=="jpg") $img=imagecreatefromjpeg($target); $tci=imagecreatetruecolor($w,$h); imagecopyresampled($tci,$img,0,0,0,0,$w,$h,$w_orig,$h_orig); imagejpeg($tci,$thumb_path,80); imagedestroy($tci); }//end function create_thumb() |
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
Salaam brother: Da PDO driver bara k hm posts jor ka. Ma core php training IT ARTIFICER na kare day, Cake Php training km zae na okm suggest plz…..
I will try to post about PDO as soon as possible
I do not know any institute who are giving CakePHP trainings, but I will suggest IT-Artificer to start CakePHP classes, keep in touch with them.
Adil khan thank for your interest, I have posted an article about PDO search it or click this link
http://codingsips.alampk.com/php-basic-pdo-mysql-connection-crud/
Thank you…..
Thanks you very much Sir,