How to Compress Image using PHP

In this tutorial we are going to talk about how to Compress Images using PHP, You can reduce the image file size with PHP.

I created a function to make it happen. You can use this code to compress any images and save them in .jpeg format. we are reducing the image quality a little bit to make it happen. here is the code.

 

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
 	
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
elseif ($info['mime'] == 'image/jpg') $image = imagecreatefromjpeg($source_url);
 
//save it
imagejpeg($image, $destination_url, $quality);
 	
//return destination file url
return $destination_url;	
}

 

You can change the image quality by changing the 60 value in compress_image function.

$imname =$_FILES["image"]["tmp_name"]; 		
$source_photo =$imname;
$namecreate= "codeconia_".time();
$namecreatenumber= rand(1000 , 10000);
$picname= $namecreate.$namecreatenumber;
$finalname= $picname.".jpeg";
$dest_photo = 'uploads/'.$finalname;
$compressimage = compress_image($source_photo, $dest_photo, 60);

Here is the actual working code .. Make the upload from first



<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image">
    <button type="submit"> Upload </button>
</form>

Final Make your Upload File

<?php
function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
     
    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
    elseif ($info['mime'] == 'image/jpg') $image = imagecreatefromjpeg($source_url);
     
    //save it
    imagejpeg($image, $destination_url, $quality);
         
    //return destination file url
    return $destination_url;    
}



$imname =$_FILES["image"]["tmp_name"];         
$source_photo =$imname;
$namecreate= "codeconia_".time();
$namecreatenumber= rand(1000 , 10000);
$picname= $namecreate.$namecreatenumber;
$finalname= $picname.".jpeg";
$dest_photo = 'uploads/'.$finalname;
$compressimage = compress_image($source_photo, $dest_photo, 60);

?>

Images will save to uploads folder. please check it . hope this helps

 

Download Source Code : 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now