Upload and Resize an Image with PHP

Upload and Resize an Image with PHP

Upload and Resize an Image with PHP

In this tutorial, We will learn about Upload and Resize an Image with PHP. Upload and Resizing images is very useful when you want show the same images with different dimensions like thumbnail image, icon images. Resizing images will also help in speed optimization of web application.

So here is the simple PHP script to re-sizing image into different dimensions. It’s also useful to your web projects in terms of loading time of web page.

In this below script, will upload a large image and create two small image of width size of 100px and 75px. The height of images will adjust as per aspect ratio. We will check validation for image extension so that images with jpg, jpeg, png and gif extension will be accepted. We also validate the size of images. In the script we assign MAX_SIZE to 512 KB. The image size of new resize and MAX_SIZE for upload limit can be change as per requirement.

PHP Code

This script resize an Image into two 100px and 75px. You can modify $newwidth values for different sizes.

<?php 
define ("MAX_SIZE","512"); 
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = check_extension($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" ) { //// if jpg/jpeg image
	   $uploadedfile = $_FILES['file']['tmp_name'];
	   $src = imagecreatefromjpeg($uploadedfile);
	} else if($extension=="png") {  // if png image
	   $uploadedfile = $_FILES['file']['tmp_name'];
	    $src = imagecreatefrompng($uploadedfile);
	} else { // // if gif image
	    $src = imagecreatefromgif($uploadedfile);
	}
	list($width,$height)=getimagesize($uploadedfile);
	// new width
	$newwidth = 100;
	$newheight=($height/$width)*$newwidth;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	$newwidth1 = 75;
	$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);

	//path to 100_px images
	$filename = "images/100_px". $_FILES['file']['name'];

	//path to 75_px images
	$filename1 = "images/75_px". $_FILES['file']['name'];

	imagejpeg($tmp,$filename,100);
	imagejpeg($tmp1,$filename1,100);

	// Destroy temporary memory
	imagedestroy($src);
	imagedestroy($tmp);
	imagedestroy($tmp1);
     }
  }
}
//If no errors found then print the success message
 if(isset($_POST['submit']) && !$errors) 
 {
       // do your stuff
	echo "Image Uploaded Successfully!";
 }
 ?>

Below php function is used to check the extensions of file uploaded.
This function will be called in above script. When you Upload and Resize an Image with PHP, this function will check the file name and check the file name extension. If the correct extensions like .jpeg, .jpg, .png or .gif is found then the above script will work further. But if the filename extensions doesn’t match with the extensions that used to check further it will return the null and exit from script.
The below piece of script is used to check the file extensions and process further for Upload and Resize an Image with PHP.

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
<?php
function check_extension($str) {
    $i = strrpos($str,".");
    if (!$i) {   
       
      return ""; 
    } 
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
     
    return $ext;
 }
?>

 

That’s all.I hope you liked this tutorial for Upload and Resize an Image with PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.