We use compressImg($source, $destination, $quality)
function that helps to compress and save image on the server using PHP.
Here is the compression code.
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Select Image File:</label>
<input type="file" name="image">
<input type="submit" name="submit" value="Upload">
</form>
This function accepts the following parameters and returns the compressed image.
$source
– An image file source to compress size.$destination
– Server path to save the compressed image.$quality
– Image quality.function compressImg($source, $destination, $quality) {
// Get image info
$info = getimagesize($source);
// Create a new image from file
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}