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 :Â
Share with your friends:
Drag and Drop with Swapy and PHP
Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]
September 17, 2024
How To Generate A PDF from HTML in Laravel 11
Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]
June 22, 2024
How to get the next value of an array and loop it in the array in PHP
Do you know How to get the next value of an array and loop it in the array in PHP […]
July 28, 2023
New Open Source CRM for project Management and Invoicing
I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]
December 31, 2022
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now