compress image in php

How to Compress Image Without Losing Visible Quality in PHP

How to compress an image in PHP? Compressing images is one of the inseparable jobs from websites and it is important to increase page speed load.

High-quality images take time to load on the webpage depending on the number of images. This issue affects SEO and performance. Compressing images is one of the optimized options to increase performance. Low-quality images also are bad for SEO.

In some CMS like WordPress, there are image compress plugins that are written in PHP too. But on other websites that are written in PHP, there are no such plugins that you have to write your own code.

This compression must not reduce the quality of images badly. Webpages’ image quality is as important as performance. Then we have to compress them as possible.

In this tutorial, you will learn how to compress images without losing visible quality while uploading with PHP.

Compress Images Contents

Image Upload Form

An image upload form is a form created by HTML code. Go to your root folder and create a “compressimage” folder. In this folder create a index.html file.

Inside the index file, add the following codes

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Compress Image - Honar Systems</title>
</head>
<body>
<form method='post' action='compress.php' enctype='multipart/form-data'>
    <input type='file' name='imagefile'>
    <input type='submit' value='Upload' name='upload'>
</form>
</body>
</html>

You can see our upload form in the body section. DO NOT forget the enctype attribute. We have to add this attribute when we want to upload a file.

As you see in action, we added a PHP file address. Then go to your folder and create “compress.php” file alongside the index file.

Compression PHP File

Open your compress.php file and add the following code inside it.

<?php
/**
 * Created by Honar Systems
 * Website: honarsystems.com
 */

//check if file uploaded
if (isset($_POST['upload'])) {
    //get uploaded file name. eg: myimage.jpg
    $uploaded_image_name = basename($_FILES['imagefile']['name']);
    $target_file = 'img/' . $uploaded_image_name;
    //get absolute path of file like C:/myhost/compressimage/img/myimage.jpg
    $image_absolute_path = $_SERVER['DOCUMENT_ROOT'] . pathinfo($_SERVER['PHP_SELF'])['dirname'] . '/img/' . $uploaded_image_name;
    //get absolute path of file with added compressed word like C:/myhost/compressimage/img/myimage_compressed.jpg
    $output_file = pathinfo($image_absolute_path)['dirname'] . '/' . basename($uploaded_image_name, '.' . pathinfo($uploaded_image_name, PATHINFO_EXTENSION)) . '_compressed.' . pathinfo($uploaded_image_name, PATHINFO_EXTENSION);
    //move uploaded file to img directory. you can change uploaded file name
    if (move_uploaded_file($_FILES["imagefile"]["tmp_name"], $target_file)) {
        //get uploaded image information like dimension, file type and etc
        $info = getimagesize($image_absolute_path);
        list($uploadWidth, $uploadHeight) = $info;

        if ($info['mime'] == 'image/jpeg') {
            $timage = imagecreatefromjpeg($image_absolute_path);
            if (imagejpeg($timage, $output_file, 60)) {
                echo "Hooray, uploaded image has been compressed";
            } else {
                echo "Sorry, there was an error compressing your file.";
            }
        } elseif ($info['mime'] == 'image/gif') {
            $img = imagecreatefromstring(file_get_contents($image_absolute_path));
            imagetruecolortopalette($img, false, 16);
            if (imagegif($img, $output_file)) {
                echo "Hooray, uploaded image has been compressed";
            } else {
                echo "Sorry, there was an error compressing your file.";
            }
        } elseif ($info['mime'] == 'image/png') {
            $timage = imagecreatefrompng($image_absolute_path);
            $targetImage = imagecreatetruecolor($uploadWidth, $uploadHeight);
            imagealphablending($targetImage, false);
            imagesavealpha($targetImage, true);
            imagecopyresampled($targetImage, $timage,
                0, 0,
                0, 0,
                $uploadWidth, $uploadHeight,
                $uploadWidth, $uploadHeight);

            if (imagepng($targetImage, $output_file, 9)) {
                echo "Hooray, uploaded image has been compressed";
            } else {
                echo "Sorry, there was an error compressing your file.";
            }
        }
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

What Is the PHP Code?

The first section is a comment and is our introduction.

if (isset($_POST['upload']))

Inside the if, we check if the upload button is submitted or not.

$uploaded_image_name = basename($_FILES['imagefile']['name']);

Here we get the uploaded file name and just the name, not the file path. For example, myimage.jpg. Remember that, imagefile in $_FILES must be exactly the same as in the HTML form.

$target_file = 'img/' . $uploaded_image_name;

Defining the target file name. Here go to your folder and create a “img” folder to store images. You can change the target file name to what you prefer in this line.

$image_absolute_path = $_SERVER['DOCUMENT_ROOT'] . pathinfo($_SERVER['PHP_SELF'])['dirname'] . '/img/' . $uploaded_image_name;

This line defines the absolute path of the uploaded file.

Keep in mind that, sometimes compression functions don’t work with file URLs. Then we have to use an absolute path.

$output_file = pathinfo($image_absolute_path)['dirname'] . '/' . basename($uploaded_image_name, '.' . pathinfo($uploaded_image_name, PATHINFO_EXTENSION)) . '_compressed.' . pathinfo($uploaded_image_name, PATHINFO_EXTENSION);

This code defines the compressed file’s absolute path.

if (move_uploaded_file($_FILES["imagefile"]["tmp_name"], $target_file))

Move the uploaded file from the temp folder to your img folder and check if it is done or not.

$info = getimagesize($image_absolute_path);
list($uploadWidth, $uploadHeight) = $info;

Get image information like dimension, and file type, and list them.

if ($info['mime'] == 'image/jpeg')
elseif ($info['mime'] == 'image/gif')
elseif ($info['mime'] == 'image/png')

Checks file types. If the file type is jpeg then do the following orders.

$timage = imagecreatefromjpeg($image_absolute_path);
if (imagejpeg($timage, $output_file, 60)) {
    echo "Hooray, uploaded image has been compressed";
} else {
    echo "Sorry, there was an error compressing your file.";
}

You can change the quality of the compressed image by changing 60 to whatever you prefer. This number will be between 0 to 100.

If the file type is gif then do the following orders.

$img = imagecreatefromstring(file_get_contents($image_absolute_path));
imagetruecolortopalette($img, false, 16);
if (imagegif($img, $output_file)) {
    echo "Hooray, uploaded image has been compressed";
} else {
    echo "Sorry, there was an error compressing your file.";
}

You can change the number of colors of the compressed image by changing 16 to whatever you prefer.

If the file type is png then do the following orders.

$timage = imagecreatefrompng($image_absolute_path);
$targetImage = imagecreatetruecolor($uploadWidth, $uploadHeight);
imagealphablending($targetImage, false);
imagesavealpha($targetImage, true);
imagecopyresampled($targetImage, $timage,
     0, 0,
     0, 0,
     $uploadWidth, $uploadHeight,
     $uploadWidth, $uploadHeight);

if (imagepng($targetImage, $output_file, 9)) {
    echo "Hooray, uploaded image has been compressed";
} else {
    echo "Sorry, there was an error compressing your file.";
}

You can change the quality of the compressed image by changing 9 to whatever you prefer. This number will be between 0 to 9. But remember that this number works and vice versa. It means that by increasing the number, the quality will be reduced.

Download the Compress Image Project in PHP

You can download the project from here.

Conclusion

To reach the image’s best performance besides the compression you have to know what size of the image should use in the place. For example, if your website logo is 1000px it means your logo is too much bigger than you need. To resize images we provided an article to resize an image by PHP.

You may like

Shopping Cart