Showing posts with label image compression. Show all posts
Showing posts with label image compression. Show all posts

Tuesday, 12 July 2016

PHP Function to compress image in lossless quality

Efficient php function to compress image quality without any loss in quality and clarity,
function compressImageQuality($source_path, $destination_path , $quality) {
    $info = getimagesize($source_path);
    if ($info['mime'] == 'image/jpeg') {
        $image = imagecreatefromjpeg($source_path);
    }
    elseif ($info['mime'] == 'image/gif') {
        $image = imagecreatefromgif($source_path);
    }
    elseif ($info['mime'] == 'image/png') {
        $image = imagecreatefrompng($source_path);
    }
    else { die('File format not supported'); }

    imagejpeg($image, $destination_path, $quality);
    return $destination_path;
}


This function will read jpg/gif/png image and compress it and save to jpg format. The imagejpeg  function compresses the image and stores as jpeg image.