How to Compress Images in Php?

by Dinesh 2013-12-27 18:51:20

How to Compress Images in Php?

The following function will process a images and compress it and save to jpg format.

function compress_image($src, $dest , $quality)
{
$info = getimagesize($src);

if ($info['mime'] == 'image/jpeg')
{
$image = imagecreatefromjpeg($src);
}
elseif ($info['mime'] == 'image/gif')
{
$image = imagecreatefromgif($src);
}
elseif ($info['mime'] == 'image/png')
{
$image = imagecreatefrompng($src);
}
else
{
die('Unknown image file format');
}

//compress and save file to jpg
imagejpeg($image, $dest, $quality);

//return destination file
return $dest;
}

//usage
$compressed = compress_image('boy.jpg', 'destination.jpg', 50);


The quality value controls the amount of image compression to be processed by function.
1178
like
0
dislike
0
mail
flag

You must LOGIN to add comments