Friday, February 19, 2016

How to compress image in php

Compress image in php:

For compressing an image in php we have to use this code for this:


<?php
    function compress($source, $destination, $quality) {
        $info = getimagesize($source);
        if ($info['mime'] == 'image/jpeg')
            $image = imagecreatefromjpeg($source);
        elseif ($info['mime'] == 'image/gif')
            $image = imagecreatefromgif($source);
        elseif ($info['mime'] == 'image/png')
            $image = imagecreatefrompng($source);
        $res=imagejpeg($image, $destination, $quality);
        return $res;
    }
   compress('image.jpg','img/image.jpg',80);
?>



Here are the $source is the path of image and $destination is the path where we have to save the image after editing.

Thanks

No comments:

Post a Comment