How to Compress Image Size Without Losing Quality in PHP

PHP provides the ability to compress images without losing the quality of it. Even more, we can resize images in PHP to generate thumbnails and lightweight web images.

PHP has some default functions to compress images and to resize them. Many popular Core-PHP frameworks and CMS use default function to generate thumbnail and images without losing image quality.

Image Compression in PHP

Here we will learn how to compress an image in PHP without losing its quality in just 2 easy steps.

STEP 1: Create an HTML form

First, we have to create a simple HTML form to upload an image file.

<!DOCTYPE html>
    <title>How to compress an image without losing quality in PHP</title>
    <form action='' method='POST' enctype='multipart/form-data'>
        <input name="image_file" type="file" accept="image/*">
        <button type="submit">SUBMIT</button>

Note: enctype=”multipart/form-data” is compulsory for image upload precess via post method in form.

STEP 2: Add below PHP code at the top of PHP file



    $file_name = $_FILES["image_file"]["name"];
    $file_type = $_FILES["image_file"]["type"];
    $temp_name = $_FILES["image_file"]["tmp_name"];
    $file_size = $_FILES["image_file"]["size"];
    $error = $_FILES["image_file"]["error"];

        echo "ERROR: Please browse for file before uploading";

    function compress_image($source_url, $destination_url, $quality) {
        $info = getimagesize($source_url);
        if ($info['mime'] == 'image/jpeg')
            $image = imagecreatefromjpeg($source_url);
        elseif ($info['mime'] == 'image/gif')
            $image = imagecreatefromgif($source_url);
        elseif ($info['mime'] == 'image/png')
            $image = imagecreatefrompng($source_url);
        imagejpeg($image, $destination_url, $quality);
        echo "Image uploaded successfully.";

    if ($error > 0) {
        echo $error;
    }else if (($file_type == "image/gif") || ($file_type == "image/jpeg") || ($file_type == "image/png") || ($file_type == "image/pjpeg")) {
        $filename = compress_image($temp_name, "uploads/".$file_name, 80);
    }else {
        echo "Uploaded image should be jpg or gif or png.";


In the second step first, we checked for the form request method. For File and Image upload POST method is compulsory.

After that, we defined some variables related to the uploaded file. Like, File Name, File Type, File Size, Temporary Location of Uploaded File, and Error in File Upload Process, if any.

Next, we created a compress_image() function to upload and compress the image. In this function, we passed three variables of the uploaded file.

  1. $source_url: It is the temporary location in our case of file upload.
  2. $destination_url: It is the destination path where we want to upload our Image.
  3. $quality: It is the rate of Image quality we want to maintain from 1 to 100. If we want to maintain 100% quality, we can apply 100, or we can pass 80 if we want to compress and lose the image quality by 20%.

In the compress_image() function, we first found for image specifications using getimagesize() function and checked for its mime type.

From mime type, we checked the image is valid or not and create a related replica of the image in PHP according to jpeg, png, gif using imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif() respectively and saved it in $image variable.

Finally, we created a jpeg image with the help of a $image variable by using imagejpeg() function.

imagejpeg() function is basically image creator function from raw image data. There are three arguments we passed in imagejpeg() function.

  1. $image: Raw image data we created and stored in $image variable
  2. $destination_url: Destination Folder/Directory path where we want to create a new image
  3. $quality: The rate of image quality we want to maintain

In the end, we checked for error in the upload process and used our compress_image() function to handle the uploaded image’s compression.

Note: Change the destination directory according to your config. Here we have set the destination path to uploads/ in our working directory.

Read Also: How to Image Preview Before Upload Using JavaScript


In the above article, we learned a complete process of image upload and compression with necessary validations.

Happy Programming 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *