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 get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

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

Get Free Access Now