Development

Image compression with PHP

Using PHPs inbuilt image compression function imagejpeg i did a test to find out is it worth compressing images? and is there a big difference?

The usage is simple for imagejpeg you need to pass a jpeg in, specify its output and then a value for its quality (0 – 100).

imagejpeg does however require that the input is created from a PHP  image create function. Its silly but here is a function to make it all simple:

function comp($in, $out, $qual)
{
    $img = imagecreatefromjpeg($in);   // load the image-to-be-saved
    imagejpeg($img, $out, $qual);
}

to compress an image

comp('full.jpg', '90.jpeg', 90);

The file size decreased a lot the lower the quality value got. 45 was 3 times smaller, whilst 75 saw the file size cut in half. Even 85 saw over 150 KB in file size saving. Its pretty evident that imagejpeg cuts file size.

The quality of the images surprised me, there was very minor change, given my starting jpg file was just a phone image the file quality degraded as you pixel peeped anyway.

The original image on the far left, quality at 70 in the middle and quality 45 on the far right. You cant notice any difference.

Again given that the starting original image was already somewhat compressed (VSCO) maybe imagejpeg could really squish them down. But it doesn’t really matter if the image was compressed to begin with because it got an even better compression.

Imagejpeg is worth using, even if you use a quality level like 80

full left, 45 right

I will attach all the compressed images below for you to see.

45
50
55
60
65
70
75
80
85
90
full
Share

Recent Posts

Kennington reservoir drained drone images

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

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago