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.

define ("MAX_SIZE","512"); 
$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 '; 
    } else { $size=filesize($_FILES['file']['tmp_name']);
         if ($size > MAX_SIZE*1024) {
	 echo "You have exceeded the size limit";
	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);
	// new width
	$newwidth = 100;

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


	// Destroy temporary memory
//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"))
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.