Image Compression on Laravel
20240218-124725.webp
Category : Image Compression
Published a month ago by BonBon

Data usage continues to grow while the cost of file storage continuously decreases. However, by using image compression you can reduce both your data usage and storage needs, as it lets you store image information in a much smaller size!

In this tutorial, you're going to learn how to compress uploaded images in Laravel using the Intervention/image package.

Prerequisites

  • Knowledge of PHP
  • PHP 8
  • Composer installed globally
  • Postman or curl
  • Prior experience with Laravel and some command-line experience

What is image compression?

According to Wikipedia, image compression is:

a type of data compression applied to images to reduce their cost of storage and transmission.

To further break it down, compressing a digital image reduces the number of bytes without degrading the image's quality. Reducing a file's size reduces both storage and transmission bandwidth requirements.

There are two forms of image compression: lossless and lossy.

What is lossy and lossless compression?

Lossless compression An algorithm that allows an image to be reduced in size without losing quality. Lossless compression comes in handy when the original image data is essential for referential or editing purposes.

Lossy compression can greatly compress images but information will be lost about the image due to the sizing being reduced during compression, For example: the width x height of the image is reduced. This isn't an issue for images like display pictures, and this method is used in numerous technologies and products like Meta and its multiple services. When building a platform that will be accessed by many users, it’s most recommended to use lossy compression against lossless.

The importance of reducing storage costs of data/image storage has always been a smart business move. This drives a lot of businesses to use third-party servers for storage to reduce the load on production servers because of two key reasons:

  • Reduced storage costFacebook spends millions of dollars on storage, yearly, in order to store information, a bulk of which is images. Image upload, over time, builds into a large consumption of storage allocated to run the application and in turn, requires more storage in order for the application to run smoothly. By implementing an image compression algorithm, the cost is cut down as image sizes are significantly reduced. Image upload. With compression, the need is reduced as images can be spliced into half or less their initial storage size.
  • Reduced load time: Assets like images require more time to load in comparison to JSON or text files. This contributes to an increase in page load time and slow responses. Compressed images help solve this problem as they require less load time. Here is an article that explains further using examples to show the significant drop in performance for an application that doesn’t compress images.

How does Laravel handle image compression?

In Laravel, images are read and manipulated using popular libraries such as GD and iMagick, and other libraries depending on the image type and the functionality you want to have.

The GD library allows you to get an image's type and file size, manipulate an image's pixels, and even generate a new image. iMagick can create, edit, and compose images and has support for a wide range of images, including JPG, PNG, GIF, and SVG.

The Intervention package combines these two popular libraries and makes image compression easier to understand and implement in a Laravel project.

Scaffold a Laravel application

For the sake of this tutorial, we will be using the Intervention PHP library and the Intervention/image package with Laravel to upload an image and compress it.

 
NVexl Project
Vexl Project was established in September 2022 as a photography, videography and web development freelancer.
Quick Link
Follow us