To compress image in PHP, this is one of the way:
<?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);
imagejpeg($image, $destination, $quality);
return $destination;
}
$source_img = 'source.jpg';
$destination_img = 'destination .jpg';
$d = compress($source_img, $destination_img, 90);
?>
However, this is only to compress by specifying a certain quality. Currently I am working on an image upload function. Instead of setting a hard file size limit as usual, I would like to compress the uploaded image that is larger than the file size limit, so that it would be below the file size limit. Is there a way I can do that WITHOUT try and error?