How to Generate Thumbnail Image in PHP

Mar 10, 2022 . Admin



Hello Friends,

I am going to explain you How To Generate Thumbnail Image in PHP. You will learn How to Create a Thumbnail Image in PHP. In side this article we will see Creating a thumbnail from an uploaded image - php.

This article will give you simple example PHP Thumbnail Image generation from uploaded file script. We will use get simple Upload image and create thumbnail using PHP.

You can use from How to Create Thumbnail Image in PHP?. I will give you simple example Generate thumbnails with PHP.

So, let's see bellow solution:

index.php
<?php
    session_start(); 
?>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Generate Thumbnail Image in PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
        img
        {
            height: 200px;
            width: 200px;
            padding: 10px;
        }
    </style>
</head>
<body class="bg-dark">
    <div class="container mt-5">
        <div class="card">
            
            <div class="card-header text-center bg-primary text-white">
                <h3>How To Generate Thumbnail Image in PHP - Mywebtuts.com</h3>
            </div>
            
            <div class="card-body" style="height: 450px;">
                <div class="row">
                    <div class="col-md-12">
                        <?php 
                            if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
                            {
                                $msg=$_SESSION["msg"];
                                echo "<div class='msgbox alert alert-info text-center mt-1 mb-1 p-0''>".$msg."</div>";

                                unset($_SESSION['msg']);
                                session_destroy();
                            }
                            if (isset($_SESSION["thumbnail"]) && !empty($_SESSION["thumbnail"])) {
                                $img = $_SESSION['thumbnail'];
                                echo "<img src=".$img."/>";
                                unset($_SESSION['thumbnail']);
                                session_destroy();
                            }
                        ?>      
                    </div>
                </div>
                
                <form enctype="multipart/form-data" action ="thumbnail.php" method=POST>
                    
                    <label for="file">Select Image:</label>
                    <input name="file" type="file" id="file" class="form-control mt-3"> 
                    
                    <div class="d-flex justify-content-center">
                        <INPUT type="submit" value="Send File" class="btn btn-primary mt-3  "></form>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
thumbnail.php
<?php
    session_start();
    $add="uploaded-img/".$_FILES['file']['name'];
    $tsrc="thumbnail/".$_FILES['file']['name'];   

    $n_width=200;          
    $n_height=200;     

    if (($_FILES['file']['type'] =="image/jpeg" OR $_FILES['file']['type']=="image/gif" OR $_FILES['file']['type']=="image/png")){

        if(move_uploaded_file ($_FILES['file']['tmp_name'],$add)){
        $_SESSION["msg"]="Thumbnail Create successfully";
        $_SESSION['thumbnail']=$tsrc;
        header("location:index.php");
        chmod("$add",0777);
        }
    
    }else{
        $_SESSION["msg"]= "Your uploaded file must be of JPG,GIF or PNG. Other file types are not allowed";
        header("location:index.php");   
    }

    if (@$_FILES['file']['type']=="image/gif"){
        
        $im=ImageCreateFromGIF($add);
        $width=ImageSx($im);              
        $height=ImageSy($im);                  
        $n_height=($n_width/$width) * $height; 
        $newimage=imagecreatetruecolor($n_width,$n_height);
        imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

        if (function_exists("imagegif")){
            Header("Content-type: image/gif");
            ImageGIF($newimage,$tsrc);
        
        }elseif(function_exists("imagejpeg")){
            Header("Content-type: image/jpeg");
            ImageJPEG($newimage,$tsrc);
        }
        
        chmod("$tsrc",0777);
    }

    if($_FILES['file']['type']=="image/jpeg"){
        $im=ImageCreateFromJPEG($add); 
        $width=ImageSx($im);              
        $height=ImageSy($im);             
        $n_height=($n_width/$width) * $height;
        $newimage=imagecreatetruecolor($n_width,$n_height);                 
        imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
        ImageJpeg($newimage,$tsrc);
        chmod("$tsrc",0777);
    }
   
    if($_FILES['file']['type']=="image/png"){
        $im=ImageCreateFromPNG($add); 
        $width=ImageSx($im);              
        $height=ImageSy($im);             
        $n_height=($n_width/$width) * $height;
        $newimage=imagecreatetruecolor($n_width,$n_height);                 
        imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
        ImageJpeg($newimage,$tsrc);
        chmod("$tsrc",0777);
    }
?>
Output:

It will help you...
#PHP