PHP Resize Image Before Upload

Apr 01, 2022 . Admin

Hello dev,

I am going to explain you how to PHP resize image on upload. You will learn PHP reduce image size before upload. In side this article we will see how to upload and resize an image using PHP.

This article will give you simple example of resize image before upload in PHP. We will use get simple PHP resize image from upload.

I will give you simple Example of how to resize image before upload in PHP.

So, let's see bellow solution:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Resize Image Before Upload -</title>
    <link href="" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card w-50 m-auto">
                    <div class="card-header text-center bg-danger text-white">
                        <h4>PHP Resize Image Before Upload -</h4>
                    <div class="card-body">
                        <form action="pro.php" method="post" enctype="multipart/form-data">         
                            <div class="mb-3">
                                <input type="file" name="image" class="form-control"> 
                            <div class="d-flex justify-content-center">
                                <input type="submit" name="submit" value="Submit" class="btn btn-success rounded-0">
    if(isset($_POST["submit"])) {

        if(is_array($_FILES)) {
            $file = $_FILES['image']['tmp_name']; 
            $sourceProperties = getimagesize($file);
            $fileNewName = time();
            $folderPath = "upload/";
            $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
            $imageType = $sourceProperties[2];

            switch ($imageType) {
                case IMAGETYPE_PNG:
                    $imageResourceId = imagecreatefrompng($file); 
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    imagepng($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);

                case IMAGETYPE_GIF:
                    $imageResourceId = imagecreatefromgif($file); 
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    imagegif($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);

                case IMAGETYPE_JPEG:
                    $imageResourceId = imagecreatefromjpeg($file); 
                    $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                    imagejpeg($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);

                    echo "Invalid Image type.";

            move_uploaded_file($file, $folderPath. $fileNewName. ".". $ext);
            echo "Image Resize Successfully.";

    function imageResize($imageResourceId,$width,$height) {

        $targetWidth = 200;
        $targetHeight = 200;

        imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);

        return $targetLayer;

It will help you...