PHPackages                             spatie/laravel-image-optimizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. spatie/laravel-image-optimizer

ActiveLibrary[Image &amp; Media](/categories/media)

spatie/laravel-image-optimizer
==============================

Optimize images in your Laravel app

1.8.3(2mo ago)1.3k6.5M—0%116[1 PRs](https://github.com/spatie/laravel-image-optimizer/pulls)20MITPHPPHP ^8.0CI passing

Since Jul 7Pushed 1mo ago30 watchersCompare

[ Source](https://github.com/spatie/laravel-image-optimizer)[ Packagist](https://packagist.org/packages/spatie/laravel-image-optimizer)[ Docs](https://github.com/spatie/laravel-image-optimizer)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-laravel-image-optimizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (30)Used By (20)

Optimize images in your Laravel app
===================================

[](#optimize-images-in-your-laravel-app)

[![Latest Version on Packagist](https://camo.githubusercontent.com/69290d0530b07f27d7cf53e128d4ef5eea4e62e025fdbdec966ad2d89efdda17/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-image-optimizer)[![Tests](https://github.com/spatie/laravel-image-optimizer/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-image-optimizer/actions/workflows/run-tests.yml/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/b205bcaf922a135c0bcc64c6edc21e1cb4dd2fc00fd6e704531293e344b05da5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-image-optimizer)

This package is the Laravel 6.0 and up specific integration of [spatie/image-optimizer](https://github.com/spatie/image-optimizer). It can optimize PNGs, JPGs, SVGs and GIFs by running them through a chain of various [image optimization tools](https://github.com/spatie/image-optimizer#optimization-tools). The package will automatically detect which optimization binaries are installed on your system and use them.

Here's how you can use it:

```
use ImageOptimizer;

// the image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimize($pathToImage);

// if you use a second parameter the package will not modify the original
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);
```

You don't like facades you say? No problem! Just resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container:

```
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);
```

The package also contains [a middleware](https://github.com/spatie/laravel-image-optimizer/blob/master/README.md#using-the-middleware) to automatically optimize all images in an request.

Don't use Laravel you say? No problem! Just use the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer) directly.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/4c2cb8045d5a89c0d404218c57f05e0c97aeb3108d1d19cf4e820890832cf5f1/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d696d6167652d6f7074696d697a65722e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-image-optimizer)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-image-optimizer
```

The package will automatically register itself.

The package uses a bunch of binaries to optimize images. To learn which ones on how to install them, head over to the [optimization tools section](https://github.com/spatie/image-optimizer#optimization-tools) in the readme of the underlying image-optimizer package. That readme also contains info on [what these tools will do to your images](https://github.com/spatie/image-optimizer#which-tools-will-do-what).

The package comes with some sane defaults to optimize images. You can modify that configuration by publishing the config file.

```
php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider"
```

This is the contents of the `config/image-optimizer` file that will be published:

```
use Spatie\ImageOptimizer\Optimizers\Svgo;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Cwebp;

return [
    /**
     * When calling `optimize` the package will automatically determine which optimizers
     * should run for the given image.
     */
    'optimizers' => [

        Jpegoptim::class => [
            '-m85', // set maximum quality to 85%
            '--strip-all',  // this strips out all text information such as comments and EXIF data
            '--all-progressive'  // this will make sure the resulting image is a progressive one
        ],

        Pngquant::class => [
            '--force' // required parameter for this package
        ],

        Optipng::class => [
            '-i0', // this will result in a non-interlaced, progressive scanned image
            '-o2',  // this set the optimization level to two (multiple IDAT compression trials)
            '-quiet' // required parameter for this package
        ],

        Svgo::class => [
            '--disable=cleanupIDs' // disabling because it is known to cause trouble
        ],

        Gifsicle::class => [
            '-b', // required parameter for this package
            '-O3' // this produces the slowest but best results
        ],

        Cwebp::class => [
            '-m 6', // for the slowest compression method in order to get the best compression.
            '-pass 10', // for maximizing the amount of analysis pass.
            '-mt', // multithreading for some speed improvements.
            '-q 90', //quality factor that brings the least noticeable changes.
        ],
    ],

    /**
     * The maximum time in seconds each optimizer is allowed to run separately.
     */
    'timeout' => 60,

    /**
     * If set to `true` all output of the optimizer binaries will be appended to the default log.
     * You can also set this to a class that implements `Psr\Log\LoggerInterface`.
     */
    'log_optimizer_activity' => false,
];
```

If you want to automatically optimize images that get uploaded to your application add the `\Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class` in the http kernel.

```
// app/Http/Kernel.php
protected $middlewareAliases = [
   ...
   'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class,
];
```

Usage
-----

[](#usage)

You can resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container:

```
// the image will be replaced with an optimized version which should be smaller
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);

// if you use a second parameter the package will not modify the original
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage);
```

### Using the facade

[](#using-the-facade)

```
use ImageOptimizer;

// the image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimize($pathToImage);

// if you use a second parameter the package will not modify the original
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);
```

You don't like facades you say? No problem! Just resolve a configured instance of `Spatie\ImageOptimizer\OptimizerChain` out of the container:

```
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);
```

### Using the middleware

[](#using-the-middleware)

All images that in requests to routes that use the `optimizeImages`-middleware will be optimized automatically.

```
Route::middleware('optimizeImages')->group(function () {
    // all images will be optimized automatically
    Route::post('upload-images', 'UploadController@index');
});
```

### Adding your own optimizers

[](#adding-your-own-optimizers)

To learn how to create your own optimizer read the ["Writing custom optimizers" section](https://github.com/spatie/image-optimizer#writing-a-custom-optimizers) in the readme of the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer#writing-a-custom-optimizers) package.

You can add the fully qualified classname of your optimizer as a key in the `optimizers` array in the config file.

Example conversions
-------------------

[](#example-conversions)

Here are some [example conversions](https://github.com/spatie/image-optimizer#example-conversions) that were made by the optimizer.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

The idea of a middleware that optimizes all files in a request is taken from [approached/laravel-image-optimizer](https://github.com/approached/laravel-image-optimizer).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

73

—

ExcellentBetter than 100% of packages

Maintenance87

Actively maintained with recent releases

Popularity69

Solid adoption and visibility

Community46

Growing community involvement

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 64.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~121 days

Recently: every ~281 days

Total

27

Last Release

86d ago

Major Versions

0.0.2 → 1.0.02017-07-13

PHP version history (5 changes)0.0.1PHP ^7.1

0.0.2PHP ^7.0

1.4.0PHP ^7.2

1.6.4PHP ^7.2|^8.0

1.7.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (153 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (18 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (9 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![fatonsopa](https://avatars.githubusercontent.com/u/9337943?v=4)](https://github.com/fatonsopa "fatonsopa (4 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (4 commits)")[![yaayes](https://avatars.githubusercontent.com/u/20878658?v=4)](https://github.com/yaayes "yaayes (3 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (3 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (2 commits)")[![tehwave](https://avatars.githubusercontent.com/u/4569897?v=4)](https://github.com/tehwave "tehwave (2 commits)")[![ockle](https://avatars.githubusercontent.com/u/2755069?v=4)](https://github.com/ockle "ockle (2 commits)")[![NicolasMica](https://avatars.githubusercontent.com/u/1624196?v=4)](https://github.com/NicolasMica "NicolasMica (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![ians88](https://avatars.githubusercontent.com/u/3274148?v=4)](https://github.com/ians88 "ians88 (1 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (1 commits)")[![JadoJodo](https://avatars.githubusercontent.com/u/534276?v=4)](https://github.com/JadoJodo "JadoJodo (1 commits)")[![Lloople](https://avatars.githubusercontent.com/u/5665466?v=4)](https://github.com/Lloople "Lloople (1 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")

---

Tags

imagelaraveloptimizeperformancephpspatielaravel-image-optimizer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-laravel-image-optimizer/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-image-optimizer/health.svg)](https://phpackages.com/packages/spatie-laravel-image-optimizer)
```

###  Alternatives

[spatie/image

Manipulate images with an expressive API

1.4k54.4M138](/packages/spatie-image)[spatie/image-optimizer

Easily optimize images using PHP

2.9k71.3M109](/packages/spatie-image-optimizer)[tomatophp/filament-media-manager

Manage your media files using spatie media library with easy to use GUI for FilamentPHP

14543.9k3](/packages/tomatophp-filament-media-manager)[spatie/pixelmatch-php

Compare images using PHP

5783.7k9](/packages/spatie-pixelmatch-php)[spatie/laravel-og-image

Generate OG images for your Laravel app

305.2k](/packages/spatie-laravel-og-image)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
