PHP image upload and resize it before uploading tutorial

PHP image upload and resize it before uploading tutorial

In this PHP Tutorial, I will let you know how to upload image in the folder and one of the most common actions in this example, you will know how to resize images and generate thumbnail of uploaded images.

In this article, we will learn about image processing like resizing an image, generating thumbnails and also creating a new image with desired image format and image size using PHP code. We are going to write an easy PHP code to upload an image, accept user-submitted image file data and use it to resize the image.

In today’s era of web based application, displaying images, sharing data and documents are an important part. Almost all the web based applications come with an option to upload an image, but we can’t restrict the user to upload an image with a specific format or with a specific file size.

However, uploading an image with a large file size will ultimately slow down the performance of the web application as it will take a longer time to load. Therefore, the uploaded image must be resized or a thumbnail should be generated of that image to keep the performance of the application at it’s best.

So, here we are going to create a thumbnail or a resized image from the uploaded original image in different formats like JPG, JPEG, PNG and GIF using PHP.


In this step, I will have simple HTML form with enctype="multipart/form-data" form to select a file and upload to the server.

<!DOCTYPE html>
<title>PHP resize and upload image tutorial</title>

    <form action="resize.php" method="post" enctype="multipart/form-data">
        <input type="file" name="image" /> 
        <input type="submit" name="submit" value="Submit" />


Now I will write the PHP script to handle the image upload process and with the help of some predefined methods in PHP, I will resize the image and generate the thumbnail accordingly.


if(isset($_POST["submit"])) {
    if(is_array($_FILES)) {

        $uploadedFile = $_FILES['file']['tmp_name']; 
        $sourceProperties = getimagesize($uploadedFile);
        $newFileName = time();
        $dirPath = "uploads/";
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $imageType = $sourceProperties[2];

        switch ($imageType) {

            case IMAGETYPE_PNG:
                $imageSrc = imagecreatefrompng($uploadedFile); 
                $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                imagepng($tmp,$dirPath. $newFileName. "_thump.". $ext);

            case IMAGETYPE_JPEG:
                $imageSrc = imagecreatefromjpeg($uploadedFile); 
                $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($tmp,$dirPath. $newFileName. "_thump.". $ext);
            case IMAGETYPE_GIF:
                $imageSrc = imagecreatefromgif($uploadedFile); 
                $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                imagegif($tmp,$dirPath. $newFileName. "_thump.". $ext);

                echo "Invalid Image type.";

        move_uploaded_file($uploadedFile, $dirPath. $newFileName. ".". $ext);
        echo "Image Resize Successfully.";

function imageResize($imageSrc,$imageWidth,$imageHeight) {

    $newImageWidth =200;
    $newImageHeight =200;


    return $newImageLayer;

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort sakarya escort