Reducing Image File Size (Compression) using PHP GD

Posted in Web Development on 16 February 2012 6 comments

Most of the time, photos uploaded by users are not optimized for web. In other words, they might not be using the best file format for their photos, thus taking much more spaces in your server than it actually need. Using PHP, you can easily reduce the file size of those uploaded images during upload time. But of course, when reducing the file size, we sacrifice the image quality. Therefore, if you are running image uploader website, this is not recommended.

The simple function

The best file format for photo is JPEG as it provides the best reduction in file size. The downside is losing details – your photo will be blurred depending on your quality setting. If your photo contains crisp texts, the text will most likely be blurred. Check out the code below.

This function below can load from PNG, JPEG or GIF file ($source_url) and save the compressed photo as JPEG in $destination_url.

<?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);
 
	//save file
	imagejpeg($image, $destination_url, $quality);
 
	//return destination file
	return $destination_url;
}
 
//usage
$compressed = compress_image('source.png', 'destination.jpg', 90);
 
?>

Explanation

  • imagecreatefromjpeg() is where PHP loads your source image.
  • imagejpeg() is the part where PHP output the image. In our case, we specified the second parameter (destination_url) therefore PHP saved the image instead of outputting it.
  • You can control the quality of the image. In the example above, we used 90/100 to maintain the quality of the photo while reducing file size. You need to try out various setting to suit your need.
  • A quick testing page

    I have written a small PHP page to test the image compression. It put your original and compressed photos side by side so that you can compare the quality and file size. Save it as compress.php, change the image URL and quality setting then run it in your server.

    <?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);
     
    	//save it
    	imagejpeg($image, $destination_url, $quality);
     
    	//return destination file url
    	return $destination_url;
    }
     
    $source_photo = 'uploads/source.jpg';
    $dest_photo = 'uploads/testing.jpg';
     
    $d = compress_image($source_photo, $dest_photo, 90);
     
    echo '
    <div style="float:left;margin-right:10px">
    	<img src="'.$source_photo.'" alt="" />
    	<br />'.filesize($source_photo).' Bytes
    </div>
     
    <div style="float:left;">
    	<img src="'.$dest_photo.'" alt="" />
    	<br />'.filesize($dest_photo).' Bytes
    </div>
    ';
     
    ?>

    Hope this helps :)

 

Posted by Zen on 16 February 2012 • 43,688 visits 6 comments
Tags :


or Subscribe to specific category only :




  - 6 Comments


estuches de perfumes originales says:

Hi my friend! I want to say that this article is amazing, nice written and come with almost all significant infos. I’d like to look extra posts like this .

Sonal says:

Really usefull..
I am using imagejpeg($image, $destination_url, $quality); for jpeg image and imagepng($image, $destination_url, $quality); for png . But it does not work with gif, imagegif ($image, $destination_url, $quality); is not available.

How can i resize a gif image?

couple communication says:

I’m very happy to find this web site. I want to to thank you for your time due to this wonderful read!! I definitely loved every bit of it and i also have you book-marked to see new things on your website.

click here says:

Hi, I just hopped over to your webpage thru StumbleUpon. Not somthing I would usually browse, but I liked your thoughts none the less. Thanks for creating something worthy of browsing.

Debashis Chowdhury says:

Hi,

I had a confusion with the source path ($source_photo) url. What should be the ideal path.

Suppose, i have form through which i am uploading the image file. I tried to use $_FILES['imagename']['tmp_name'] as the source path. But, it did not work.
Can’t i use it?

Next, i have uploaded the image into the destination folder using “move_uploaded_file()” and then i used the destination folder path as the source path of the image. It worked then. By following this method, it takes more time for execution using large images. I want to reduce the execution time as well.

Is there any other way of uploading? Please suggest me the needful.

Mark says:

This looks great! Thank you. Unfortunately I’m new to PHP and I’m having some difficulty using it. Could someone break down the steps to optimize an image that the user uploads? Thanks

Leave a Reply

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Previous Post
«
Next Post
»