Compress Image Before Upload Using PHP Example

03-Apr-2023

.

Admin

Compress Image Before Upload Using PHP Example

Hi guys,

Today i will explained how to Compress Image Before Upload using PHP. This example is so easy to use in php. THis example to i am a upload a image file and compress to the file size.

This exampel to i am use two php files and one uploads name folder create in your directory to upload compress image file. So let's start to the example.

index.php


<?php

include 'upload.php';

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Compress Image Before Upload using PHP Example</title>

<link href="css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

<script src="js/bootstrap.min.js"></script>

<script src="js/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>

<div class="container">

<div class="row">

<div class="col-md-12">

<div class="card">

<h2 class="card-header">Compress Image Before Upload using PHP Example</h2>

<?php if(!empty($statusMsg)){ ?>

<p class="mt-2 text-center <?php echo $status; ?>"><?php echo $statusMsg; ?></p>

<?php } ?>

<div class="card-body">

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

<label>Select Image File:</label>

<input type="file" name="image" class="form-control">

<button type="submit" name="submit" value="Upload" class="btn btn-block mt-1"> Submit</button>

</form>

<div class="result mt-3">

<?php if(!empty($compressedImage)){ ?>

<p><b>Original Image Size : </b><?php echo $imageSize ?></p>

<p><b>Compressed Image Size : </b><?php echo $compressedImageSize ?></p>

<img src="<?php echo $compressedImage ?>">

<?php } ?>

</div>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

upload.php

<?php

function compressImage($source, $destination, $quality) {

// Get image info

$imgInfo = getimagesize($source);

$mime = $imgInfo['mime'];

// Create a new image from file

switch($mime){

case 'image/jpeg':

$image = imagecreatefromjpeg($source);

break;

case 'image/png':

$image = imagecreatefrompng($source);

break;

case 'image/gif':

$image = imagecreatefromgif($source);

break;

default:

$image = imagecreatefromjpeg($source);

}

// Save image

imagejpeg($image, $destination, $quality);

// Return compressed image

return $destination;

}

// File upload path

$uploadPath = "uploads/";

// If file upload form is submitted

$status = $statusMsg = '';

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

$status = 'error';

if(!empty($_FILES["image"]["name"])) {

// File info

$fileName = basename($_FILES["image"]["name"]);

$imageUploadPath = $uploadPath . $fileName;

$fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION);

// Allow certain file formats

$allowTypes = array('jpg','png','jpeg','gif');

if(in_array($fileType, $allowTypes)){

// Image temp source

$imageTemp = $_FILES["image"]["tmp_name"];

$imageSize = $_FILES["image"]["size"];

// Compress size and upload image

$compressedImage = compressImage($imageTemp, $imageUploadPath, 75);

if($compressedImage){

$compressedImageSize = filesize($compressedImage);

$status = 'success';

$statusMsg = "Image compressed successfully.";

}else{

$statusMsg = "Image compress failed!";

}

}else{

$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';

}

}else{

$statusMsg = 'Please select an image file to upload.';

}

}

?>

Now you can check your own.

I hope it can help you...

Output :

#PHP 8

#PHP