ItSolutionStuff.com

How to Upload and Resize Image in PHP?

By Hardik Savani • May 14, 2024
PHP

Hey Folks,

This article is focused on php resize image upload script. I explained simply about php resize image before move_uploaded_file. you will learn upload and resize image in php example. you will learn image resize in php example.

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:

index.php

pro.php

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.

index.php

<!DOCTYPE html>

<html>

<head>

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

</head>

<body>


<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" />

</form>

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

}

?>

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.

Run PHP App:

All the required steps have been done, now you have to type the given below command and hit enter to run the PHP app:

php -S localhost:8000

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Ajax Form Validation Example Tutorial

Read Now →

PHP MySQL DataTables Server-side Processing Example

Read Now →

PHP Ajax Inline Editing using X-editable Bootstrap JS Example

Read Now →

PHP MySQL Integrate Fullcalendar Example Tutorial

Read Now →

PHP MySQL Image Gallery CRUD Example

Read Now →

PHP JQuery Ajax Image Upload Example Tutorial

Read Now →

PHP MySQL Highcharts Chart Example

Read Now →

Convert HTML to PDF in PHP with Dompdf Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →

PHP JQuery Select2 Ajax Autocomplete Example

Read Now →

How to Remove Duplicate Values from Array in PHP?

Read Now →

PHP Paypal Payment Gateway Integration Example

Read Now →

How to Remove White Space from String in PHP?

Read Now →

How to Quick Run PHP Project on Localhost?

Read Now →

Crop, Resize, Frames etc on Selected Image in PHP using Aviary

Read Now →