How to Resize Image Before Upload in PHP?

03-Apr-2023

.

Admin

How to Resize Image Before Upload in PHP?

Hi friends,

In this post, we will learn how to resize image before upload in PHP. i explained simply step by step how to resize image when upload in PHP. Here you will learn how to set upload image size in PHP. This tutorial will give you simple example of how to reduce image size while uploading in PHP.

I will give you simple Example of how to resize image in php before upload.

So let's see bellow example:

index.php


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Resize Image Before Upload in PHP? - Nicesnippets.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 m-auto" style="width: 60%;">

<div class="card-header text-center bg-danger text-white">

<h4>How to Resize Image Before Upload in PHP? - Nicesnippets.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:

I hope it will help you....

#PHP