How to Create Thumbnail Image in PHP?

Hi Friends.

In this article, i will let you know how to generate thumbnail image while uploading in PHP. Here i will show you quick and simple code of upload picture and make thumbnail image using php gd library.

If you know, we moistly require to set several images in website and that's why it take more time to load of webpage. So if you make it better on size like generate thumbnail image for it then it make it smooth and easy to load. So thumbnail image will help to quick and fast your web page. We always require to generate thumbnail image for users, products, profile, categories etc.

Here i wanted to show you code of generate thumbnail image in PHP. So you can also use this code in your other framework too. Here i created index.php and process.php file, write code for make thumbnail image. You have to create

"media" folder in your root directory, all the images will store on "media" folder.

So let's just follow bellow two files.

Create index.php file:

<!DOCTYPE html>



<title>How to Create Thumbnail Image in PHP?</title>



<h1>How to Create Thumbnail Image in PHP?</h1>

<form action="process.php" method="post" enctype="multipart/form-data">

<input type="file" name="picture" />

<input type="submit" name="submit" value="Submit" />




Create process.php file:


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

if(is_array($_FILES)) {

$file = $_FILES['picture']['tmp_name'];

$sourceProperties = getimagesize($file);

$fileNewName = time();

$folderPath = "media/";

$ext = pathinfo($_FILES['picture']['name'], PATHINFO_EXTENSION);

$imageType = $sourceProperties[2];

switch ($imageType) {


$imageResourceId = imagecreatefrompng($file);

$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);

imagepng($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);



$imageResourceId = imagecreatefromgif($file);

$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);

imagegif($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);



$imageResourceId = imagecreatefromjpeg($file);

$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);

imagejpeg($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);



echo "Invalid Image type.";




move_uploaded_file($file, $folderPath. $fileNewName. ".". $ext);

echo "Image Uploaded with created thumbnail Successfully.";



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

$targetWidth =250;

$targetHeight =250;


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

return $targetLayer;



After create both files, make sure again you have to create "media" folder with full permission.

I hope you found your best.

November 19, 2017 | 7850 Viewer | Category : PHP
Related Posts