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:

index.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Resize Image Before Upload - Mywebtuts.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
    <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 - Mywebtuts.com</h4>
                    </div>
                    <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>
                            <div class="d-flex justify-content-center">
                                <input type="submit" name="submit" value="Submit" class="btn btn-success rounded-0">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
pro.php
<?php
    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);
                    break;

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

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

                default:
                    echo "Invalid Image type.";
                    exit;
                    break;
            }

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

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

        $targetWidth = 200;
        $targetHeight = 200;

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

        return $targetLayer;
    }
?>
Output:

It will help you...
#PHP