Compress Image Before Upload Using PHP Example




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.



include 'upload.php';


<!DOCTYPE html>



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



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


<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 } ?>











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

// Get image info

$imgInfo = getimagesize($source);

$mime = $imgInfo['mime'];

// Create a new image from file


case 'image/jpeg':

$image = imagecreatefromjpeg($source);


case 'image/png':

$image = imagecreatefrompng($source);


case 'image/gif':

$image = imagecreatefromgif($source);



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


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


$compressedImageSize = filesize($compressedImage);

$status = 'success';

$statusMsg = "Image compressed successfully.";


$statusMsg = "Image compress failed!";



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



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




Now you can check your own.

I hope it can help you...

Output :

#PHP 8