Categories: Development

Compress image if certain size in PHP

If you are downloading and archiving images majority of the time you can compress them heavily to be able to store more. For reasons such as: never viewing the image again, it was posted with too much size or you do not need to zoom, compressing images makes a lot of sense.

Problem is how can this be done in an automated environment? Because if one image has already been heavily compressed if that gets compressed again it could become worthless.

Compress based on image size

The solution is to compress or degrade quality based on the images original size. Within PHP we can easily get the iamges size in bytes which gets converted to Kilobytes as that is more readable. Now if the image file size is above 1000 Kilobytes which is 1 Megabyte a heavy compress can be applied.

If the file size is something lower such as 800 Kilobytes we can give it a slightly reduced compression value and so forth until a lower limit is reached for when no compression needs to be applied.

Here is a simple and easy to use class:

<?php
class imageComp
{
    private $image;

    public function setImage($image)//Sets the image to compress
    {
        if (exif_imagetype($image) == IMAGETYPE_JPEG) {
            $this->image = $image;
        } else {
            throw new Exception("Error not an image");
        }
    }

    public function getSize()//Returns image filesize
    {
        $image = $this->image;
        if (exif_imagetype($image) == IMAGETYPE_JPEG) {
            return filesize($image) / 1024;//Size as KB
        } else {
            throw new Exception("Error not an image");
        }
    }

    public function compress()//Filter and compress based on filesize
    {
        $image = $this->image;
        $size = filesize($image) / 1024;//Size as KB
        if ($size >= 1000) {//Greater or equal to 1 MB
            $img = imagecreatefromjpeg($image);
            imagejpeg($img, $image, 34);
            return "$image was over 1000 KB";
        } elseif ($size >= 800) {//Greater or equal 800 KB
            $img = imagecreatefromjpeg($image);
            imagejpeg($img, $image, 36);
            return "$image was over 800 KB";
        } elseif ($size >= 600) {//Greater or equal 600 KB
            $img = imagecreatefromjpeg($image);
            imagejpeg($img, $image, 38);
            return "$image was over 600 KB";
        } elseif ($size >= 400) {//Greater or equal 400 KB
            $img = imagecreatefromjpeg($image);
            imagejpeg($img, $image, 40);
            return "$image was over 400 KB";
        } else {
            return "$image was under 400 KB";
        }
    }
}

Call and use like:

<?php
$comp = new imageComp();
$comp->setImage('image.jpg');//Image to use
echo $comp->compress();
echo $comp->getSize();

Do note that imagejpeg() isnt real compression rather a reduction in quality. It would be best to use a framework such as ImageMagick.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago