How to Compress Image Before Upload using PHP

How to Compress Image Before Upload using PHP

How to Compress Image Before Upload using PHP

0 Sales


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.

File Upload Form

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">



Following the submission of the form, the file data is sent to the upload.php file for further processing.


Compress and Upload Image with PHP

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.

 * 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 
        case 'image/jpeg': 
            $image imagecreatefromjpeg($source); 
        case 'image/png': 
            $image imagecreatefrompng($source); 
        case 'image/gif': 
            $image imagecreatefromgif($source); 
            $image imagecreatefromjpeg($source); 
    // Save image 
    // Return compressed image 
    return $destination; 
// File upload path 
$uploadPath "uploads/"; 
// If file upload form is submitted 
$status $statusMsg ''; 
    $status 'error'; 
    if(!empty($_FILES["image"]["name"])) { 
        // File info 
        $fileName basename($_FILES["image"]["name"]); 
        $imageUploadPath $uploadPath $fileName; 
        $fileType pathinfo($imageUploadPathPATHINFO_EXTENSION); 
        // Allow certain file formats 
        $allowTypes = array('jpg','png','jpeg','gif'); 
            // Image temp source 
            $imageTemp $_FILES["image"]["tmp_name"]; 
            // Compress size and upload image 
            $compressedImage compressImage($imageTemp$imageUploadPath75); 
                $status 'success'; 
                $statusMsg "Image compressed successfully."; 
                $statusMsg "Image compress failed!"; 
            $statusMsg 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
        $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).


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : Jan 22, 2022

Updated Date : Jan 23, 2022


Comments : 0

Downloads : 0