A web page that has a huge graphic takes longer to load. If you want to load a large image without slowing down the website load time, you’ll need to optimise the image. Image compression is particularly useful for reducing image size. When uploading an image to a website, the user usually does not optimise it. In this scenario, compress the image before uploading it to make it more efficient.
Using PHP, you may simply compress/optimize an image before uploading it. Before uploading, the file size is decreased using the picture compress feature. The compressed image saves space on the server and allows the web page to load faster. We’ll show you how to use PHP to compress an image before uploading it in this article.
Form for Uploading Files
Create a file input field and a submit button in an HTML form. The following attributes must be present in the form> tag.
method=”post” \senctype=”multipart/form-data”
<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>
The file data is sent to the upload.php file for processing after the form is submitted.
PHP Image Compression and Upload
The picture compression and upload procedures are handled in the upload.php file.
compressImage() is a PHP-based custom function for compressing and saving images on the server.
If the file has been submitted, use the PHP $_FILES method to get the file information.
Using the compressImage() function, reduce the image’s size and upload it.
Render the status of the picture upload.
<?php
/*
* Custom function to compress image size and
* upload to the server using PHP
*/
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}
// File upload path
$uploadPath = "uploads/";
// If file upload form is submitted
$status = $statusMsg = '';
if(isset($_POST["submit"])){
$status = 'error';
if(!empty($_FILES["image"]["name"])) {
// File info
$fileName = basename($_FILES["image"]["name"]);
$imageUploadPath = $uploadPath . $fileName;
$fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(in_array($fileType, $allowTypes)){
// Image temp source
$imageTemp = $_FILES["image"]["tmp_name"];
// Compress size and upload image
$compressedImage = compressImage($imageTemp, $imageUploadPath, 75);
if($compressedImage){
$status = 'success';
$statusMsg = "Image compressed successfully.";
}else{
$statusMsg = "Image compress failed!";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select an image file to upload.';
}
}
// Display status message
echo $statusMsg;
?>