PHPackages                             xbing2002/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. xbing2002/laravel-image-optimizer

ActiveLibrary

xbing2002/laravel-image-optimizer
=================================

Optimize images in your Laravel app

1.4.0(7y ago)038MITPHPPHP ^7.2

Since Jul 7Pushed 6y agoCompare

[ Source](https://github.com/xbing2002/laravel-image-optimizer)[ Packagist](https://packagist.org/packages/xbing2002/laravel-image-optimizer)[ Docs](https://github.com/spatie/laravel-image-optimizer)[ RSS](/packages/xbing2002-laravel-image-optimizer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (16)Used By (0)

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)[![Build Status](https://camo.githubusercontent.com/06c5f337492f4d53164b104c29b1c7e87cacb03175c19ad32a96ae5eda7330f7/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d696d6167652d6f7074696d697a65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-image-optimizer)[![StyleCI](https://camo.githubusercontent.com/618ee55048342f916c9da519369bf466db7a93c15dd167efcf622163bb9bf47e/68747470733a2f2f7374796c6563692e696f2f7265706f732f39363536333538392f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/96563589)[![Total Downloads](https://camo.githubusercontent.com/b205bcaf922a135c0bcc64c6edc21e1cb4dd2fc00fd6e704531293e344b05da5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-image-optimizer)

This package is the Laravel 5.4 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.

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

[](#installation)

You can install the package via composer:

```
composer require xbing2002/laravel-image-optimizer dev-master
```

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;

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 know to cause troubles
        ],

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

    /**
     * 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 $routeMiddleware = [
   ...
   '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](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  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, Samberstraat 69D, 2060 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).

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

[](#support-us)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

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

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 78.1% 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 ~46 days

Recently: every ~67 days

Total

14

Last Release

2629d ago

Major Versions

0.0.2 → 1.0.02017-07-13

PHP version history (3 changes)0.0.1PHP ^7.1

0.0.2PHP ^7.0

1.4.0PHP ^7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/c2f55cab968f5834b12199bc6e2901321d23f62f5f90b7f8fab8f44adbef7d2d?d=identicon)[xbing2002](/maintainers/xbing2002)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (89 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)")[![xbing2002](https://avatars.githubusercontent.com/u/37572472?v=4)](https://github.com/xbing2002 "xbing2002 (3 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)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (2 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (1 commits)")[![atymic](https://avatars.githubusercontent.com/u/50683531?v=4)](https://github.com/atymic "atymic (1 commits)")[![bosz](https://avatars.githubusercontent.com/u/6331902?v=4)](https://github.com/bosz "bosz (1 commits)")[![ians88](https://avatars.githubusercontent.com/u/3274148?v=4)](https://github.com/ians88 "ians88 (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")[![tomopongrac](https://avatars.githubusercontent.com/u/25442485?v=4)](https://github.com/tomopongrac "tomopongrac (1 commits)")

---

Tags

spatielaravel-image-optimizer

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-image-optimizer

Optimize images in your Laravel app

1.3k6.5M42](/packages/spatie-laravel-image-optimizer)[spatie/image

Manipulate images with an expressive API

1.4k54.4M137](/packages/spatie-image)[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/laravel-tags

Add tags and taggable behaviour to your Laravel app

1.7k10.3M85](/packages/spatie-laravel-tags)[spatie/laravel-failed-job-monitor

Get notified when a queued job fails

1.0k2.6M4](/packages/spatie-laravel-failed-job-monitor)[spatie/laravel-searchable

Pragmatically search through models and other sources

1.4k1.5M23](/packages/spatie-laravel-searchable)

PHPackages © 2026

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