Compress or resize image before upload is an important functionality for any web developer. Everyone knows that fast web page loading, so much important these days. If your website has a very high loading time then its affects performance very badly.
Generally, images took more time to load on a page, so it’s a good practice that you compress images before uploading them on the website.
To optimize the image, PHP gives you to easily implement compress / resize image functionality before upload. The image file size is reduced before uploading and it will reduce the storage uses of the server and reduce the web page loading time.
Example: Compress Image Before Upload using PHP
Let’s check it out the example of compress an image before upload in php. We will resize the image without losing image quality.
Step 1: Create File Upload Form – index.php
First, create an HTML form with file upload element.
Must add these attribute in <form> tag:
enctype=”multipart/form-data”
method=”POST”
index.php
1 2 3 4 5 |
<form action="imageupload.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> |
Step-2: Compress and Upload Image with PHP
The compress / resize before upload file in PHP functionality is managed in the imageupload.php file.
imageupload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php $uploadPath = "uploadFile/"; // File upload path // If file upload is not empty $statusMsg = '' if(!empty($_FILES["image"]["name"])) { $fileName = basename($_FILES["image"]["name"]); $imageUploadPath = $uploadPath . $fileName; $fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION); $allowTypes = array('jpg','png','jpeg','gif'); // Allow certain file formats if(in_array($fileType, $allowTypes)) { $imageTemp = $_FILES["image"]["tmp_name"]; $compressedImage = compressImageData($imageTemp, $imageUploadPath, 75); // Compress size and upload image if($compressedImage){ $status = 'compress success'; $statusMsg = "Image compressed successfully."; }else{ $statusMsg = "Image compress failed!"; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; } } // Display status message echo $statusMsg; function compressImageData($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); imagejpeg($image, $destination, $quality); break; case 'image/png': $image = imagecreatefrompng($source); imagepng($image, $destination, $quality); break; case 'image/gif': $image = imagecreatefromgif($source); imagegif($image, $destination, $quality); break; default: $image = imagecreatefromjpeg($source); imagejpeg($image, $destination, $quality); } // Return compressed image return $destination; } ?> |
The above example code helps you to compress the image file without using any compression library. With this compression PHP script, you can resize different types of image files before uploading on the server.