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 Compress Image using PHP

In this tutorial we are going to talk about how to Compress Images using PHP, You can reduce the image […]

13 October, 2021

How to backup and download Database using PHP

Learn How to backup and download Database using PHP. Most of the time we develop some applications, the most important […]

8 October, 2021

How to make a Zip file using PHP

Hello Everyone In this tutorial, we are going to make a zip file using PHP. and we also unzip the […]

How to make pretty URL in PHP using .htaccess

Hello Everyone, Here in this tutorial, you will learn How to make a pretty URL in PHP using .htaccess. and […]

18 September, 2021

Digital Marketing Toolkit

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

Get Free Access Now