The huge image takes longer to load on a web page. If you wish to load a huge image without slowing down the page, the image must be optimised to decrease its size. Image compression is particularly useful for reducing image size. In most cases, the user does not optimise the image before uploading it to the website. In this scenario, compress the image before uploading it to optimise it.
Using PHP, you can simply compress/optimize images before uploading them. The file size is decreased before uploading using the picture compress capability. The compressed picture reduces the use of server storage and speeds up the loading of the web page. In this lesson, we'll teach you how to use PHP to compress a picture before uploading it.
Make an HTML form that includes a file input field and a submit button. Check that the form> tag has the following attributes.
method="post"
enctype="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>
Following the submission of the form, the file data is sent to the upload.php file for further processing.
The picture compression and upload procedures are handled in the upload.php file.
- compressImage() is a PHP custom function that allows you to compress and store images on the server.
- If the file is submitted,
- use the PHP $_FILES method to retrieve the file information.
- Use the compressImage() method to reduce picture size and upload it.
- Display the picture upload status.
<?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; ?>
In general, the move uploaded file() method is used in PHP to upload files. However, if you wish to compress the picture before uploading it, our unique PHP method (compressImage()) comes in handy. The example code demonstrates how to compress an image file without the need of a compression package. You may compress several sorts of picture files with our compression script (JPG, JPEG, PNG, and GIF).
© ThemesGiant Copyright @2015-2022 | All rights reserved.