DEV Community

Cover image for How to Compress Image Size in Laravel 11
Msh Sayket
Msh Sayket

Posted on

1

How to Compress Image Size in Laravel 11

In this article, I will show you How to Compress Image Size in Laravel 11 application.

We will use the spatie/image Composer package to compress image size in laravel 11. The spatie/image Composer package is a PHP library used for image manipulation. It allows users to resize, crop, and apply filters to images easily. With simple syntax, it supports various formats like JPEG, PNG, and GIF. It’s often used in web development to handle and edit images before displaying them on websites. The package is well-documented and integrates seamlessly with popular frameworks like Laravel. You Can Learn How to Send an Ajax POST Request to a Laravel Route

How to Compress Image Size in Laravel 11
How to Compress Image Size in Laravel 11
In this example, we will install the spatie/image Composer package. spatie/image provides methods to optimize image size using the load() and optimize() methods. We will create a simple form with the file input field. You can choose an image, and then you will see a preview of the original image.

Step for How to Compress Image Size in Laravel 11?

Step 1: Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install spatie/image Image Package

In the second step, we will install spatie/image for compress image. So, first, fire the command below in your CMD or terminal:

composer require spatie/image
Enter fullscreen mode Exit fullscreen mode

Now, we also need to install image optimization tools in your system, so let’s install it to your system as well:

Install all the optimizers on Ubuntu/Debian:

sudo apt-get install jpegoptim
sudo apt-get install optipng
sudo apt-get install pngquant // For PNG Image
sudo npm install -g svgo
sudo apt-get install gifsicle
sudo apt-get install webp
sudo apt-get install libavif-bin # minimum 0.9.3
Enter fullscreen mode Exit fullscreen mode

Read More

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay