Image Upload and Resize with PHP

Image uploading is the need of websites which handling form processing or user login. Most of the realtime web application needs this feature. But the main problem is the image size and resolution while uploading the photo. In this post, I am going to write about image upload and resize with PHP. You can use this code and check if it works for you. By resizing the image, you can save space on your hosting and small images also takes less time in loading in browser.
This script resize an Image into two 60px and 25px. see the variables $newwidth and $newwidth1 in the code. Change the values according to your need to convert image size according to your need.
PHP Code
<?php define ("MAX_SIZE","400"); $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image =$_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo ' Unknown Image extension '; $errors=1; } else { $size=filesize($_FILES['file']['tmp_name']); if ($size > MAX_SIZE*1024) { echo "You have exceeded the size limit"; $errors=1; } if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list($width,$height)=getimagesize($uploadedfile); $newwidth=60; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=25; $newheight1=($height/$width)*$newwidth1; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height); $filename = "images/". $_FILES['file']['name']; $filename1 = "images/small". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } } } //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { // mysql_query("update SQL statement "); echo "Image Uploaded Successfully!"; } ?> <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?>
Similar Articles
1 Comments
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.
It’s really a nice and useful piece of info. I am glad that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.