How to upload and resize image in PHP ?

By Hardik Savani | October 10, 2017 | | 3702 Viewer | Category : PHP

After a long time i am going to share something with you friends. But Anyway Today i will show you how to generate thumbnail image in PHP. Here i will make very simple code to upload image and resize that image too.

As we know well images are very important to display on our website. As specially when if you require small size of image and you are displaying big image then it can be take more time to load. So your photos should be resize able or need to generate thumbnail when user upload those images. So here i will explain how to create thumbnail image in your code php project.

Here, i use core php code so you can easily use in your project or any php framework like laravel, codeigniter, cackephp etc. In this example i will create two file as listed bellow:



In this example you can generate thumbnail image of png, jpg, jpeg, gif too. So make sure you have to upload with given extension. So just let's see bellow code and copy paste in your file then check it.


<!DOCTYPE html>



<title>PHP Image resize to upload</title>



<div class="container">

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

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

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







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) {


$imageResourceId = imagecreatefrompng($file);

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

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



$imageResourceId = imagecreatefromgif($file);

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

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



$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;



Create Upload Folder

In this step, you don't have to do more, you have to simple create "upload" folder in your root directory. So let's simple create upload folder now.

Ok, now we are ready to run our example. So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:


I hope it can help you...

Do you want to expert php developer or developer team for your projects ?
Contact US
We are Recommending you: