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

ActiveLibrary

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

Easily optimize images using PHP

1.2.0(6y ago)277MITPHPPHP ^7.2

Since Jul 2Pushed 5y agoCompare

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

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

Easily optimize images using PHP
================================

[](#easily-optimize-images-using-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/157b8c125696a6d3616b466f83e8547b7dc3757b2b50d580ce14d8d99201dbe1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/image-optimizer)[![Build Status](https://camo.githubusercontent.com/2fa18205e390cbc216d825c2b99d494c1ca593611d3c8a29522c8f2ff4607fa6/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f696d6167652d6f7074696d697a65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/image-optimizer)[![Quality Score](https://camo.githubusercontent.com/447c28cac6ee486628ca8c8b106632be7f8b8f71ee1bdb610a74783f920c1521/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/image-optimizer)[![StyleCI](https://camo.githubusercontent.com/9bc9362d14f2672999627199410f7362ba4140b88372fc4def7405e522c2594e/68747470733a2f2f7374796c6563692e696f2f7265706f732f39363034313837322f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/96041872)[![Total Downloads](https://camo.githubusercontent.com/7c2123d3488c8f1af0fe5b2db42495b2700a68b3f77841ec5ae7620ca56f1a48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f696d6167652d6f7074696d697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/image-optimizer)

This package can optimize PNGs, JPGs, SVGs and GIFs by running them through a chain of various [image optimization tools](#optimization-tools). Here's how you can use it:

```
use Spatie\ImageOptimizer\OptimizerChainFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain->optimize($pathToImage);
```

The image at `$pathToImage` will be overwritten by an optimized version which should be smaller. The package will automatically detect which optimization binaries are installed on your system and use them.

Here are some [example conversions](#example-conversions) that have been done by this package.

Loving Laravel? Then head over to [the Laravel specific integration](https://github.com/spatie/laravel-image-optimizer).

Using WordPress? Then try out [the WP CLI command](https://github.com/TypistTech/image-optimize-command).

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

[](#installation)

You can install the package via composer:

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

### Optimization tools

[](#optimization-tools)

The package will use these optimizers if they are present on your system:

- [JpegOptim](http://freecode.com/projects/jpegoptim)
- [Optipng](http://optipng.sourceforge.net/)
- [Pngquant 2](https://pngquant.org/)
- [SVGO](https://github.com/svg/svgo)
- [Gifsicle](http://www.lcdf.org/gifsicle/)
- [Imagemin](https://github.com/imagemin/imagemin)

Here's how to install all the optimizers on Ubuntu:

```
sudo apt-get install jpegoptim
sudo apt-get install optipng
sudo apt-get install pngquant
sudo npm install -g svgo
sudo apt-get install gifsicle
sudo apt-get install webp
```

And here's how to install the binaries on MacOS (using [Homebrew](https://brew.sh/)):

```
brew install jpegoptim
brew install optipng
brew install pngquant
brew install svgo
brew install gifsicle
brew install webp
```

Which tools will do what?
-------------------------

[](#which-tools-will-do-what)

The package will automatically decide which tools to use on a particular image.

### JPGs

[](#jpgs)

JPGs will be made smaller by running them through [JpegOptim](http://freecode.com/projects/jpegoptim). These options are used:

- `-m85`: this will store the image with 85% quality. This setting [seems to satisfy Google's Pagespeed compression rules](https://webmasters.stackexchange.com/questions/102094/google-pagespeed-how-to-satisfy-the-new-image-compression-rules)
- `--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, meaning it can be downloaded using multiple passes of progressively higher details.

### PNGs

[](#pngs)

PNGs will be made smaller by running them through two tools. The first one is [Pngquant 2](https://pngquant.org/), a lossy PNG compressor. We set no extra options, their defaults are used. After that we run the image through a second one: [Optipng](http://optipng.sourceforge.net/). These options are used:

- `-i0`: this will result in a non-interlaced, progressive scanned image
- `-o2`: this set the optimization level to two (multiple IDAT compression trials)

### SVGs

[](#svgs)

SVGs will be minified by [SVGO](https://github.com/svg/svgo). SVGO's default configuration will be used, with the omission of the `cleanupIDs` plugin because that one is known to cause troubles when displaying multiple optimized SVGs on one page.

Please be aware that SVGO can break your svg. You'll find more info on that in this [excellent blogpost](https://www.sarasoueidan.com/blog/svgo-tools/) by [Sara Soueidan](https://twitter.com/SaraSoueidan).

### GIFs

[](#gifs)

GIFs will be optimized by [Gifsicle](http://www.lcdf.org/gifsicle/). These options will be used:

- `-O3`: this sets the optimization level to Gifsicle's maximum, which produces the slowest but best results

### WEBPs

[](#webps)

WEBPs will be optimized by [Cwebp](https://developers.google.com/speed/webp/docs/cwebp). These options will be used:

- `-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.

(Settings are original taken from [here](https://medium.com/@vinhlh/how-i-apply-webp-for-optimizing-images-9b11068db349))

Usage
-----

[](#usage)

This is the default way to use the package:

```
use Spatie\ImageOptimizer\OptimizerChainFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain->optimize($pathToImage);
```

The image at `$pathToImage` will be overwritten by an optimized version which should be smaller.

The package will automatically detect which optimization binaries are installed on your system and use them.

To keep the original image, you can pass through a second argument`optimize`:

```
use Spatie\ImageOptimizer\OptimizerChainFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain->optimize($pathToImage, $pathToOutput);
```

In that example the package won't touch `$pathToImage` and write an optimized version to `$pathToOutput`.

### Setting a timeout

[](#setting-a-timeout)

You can set the maximum of time in seconds that each individual optimizer in a chain can use by calling `setTimeout`:

```
$optimizerChain
    ->setTimeout(10)
    ->optimize($pathToImage);
```

In this example each optimizer in the chain will get a maximum 10 seconds to do it's job.

### Creating your own optimization chains

[](#creating-your-own-optimization-chains)

If you want to customize the chain of optimizers you can do so by adding `Optimizer`s manually to an `OptimizerChain`.

Here's an example where we only want `optipng` and `jpegoptim` to be used:

```
use Spatie\ImageOptimizer\OptimizerChain;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Pngquant;

$optimizerChain = (new OptimizerChain)
   ->addOptimizer(new Jpegoptim([
       '--strip-all',
       '--all-progressive',
   ]))

   ->addOptimizer(new Pngquant([
       '--force',
   ]))
```

Notice that you can pass the options an `Optimizer` should use to its constructor.

### Writing a custom optimizers

[](#writing-a-custom-optimizers)

Want to use another command line utility to optimize your images? No problem. Just write your own optimizer. An optimizer is any class that implements the `Spatie\ImageOptimizer\Optimizers\Optimizer` interface:

```
namespace Spatie\ImageOptimizer\Optimizers;

use Spatie\ImageOptimizer\Image;

interface Optimizer
{
    /**
     * Returns the name of the binary to be executed.
     *
     * @return string
     */
    public function binaryName(): string;

    /**
     * Determines if the given image can be handled by the optimizer.
     *
     * @param \Spatie\ImageOptimizer\Image $image
     *
     * @return bool
     */
    public function canHandle(Image $image): bool;

    /**
     * Set the path to the image that should be optimized.
     *
     * @param string $imagePath
     *
     * @return $this
     */
    public function setImagePath(string $imagePath);

    /**
     * Set the options the optimizer should use.
     *
     * @param array $options
     *
     * @return $this
     */
    public function setOptions(array $options = []);

    /**
     * Get the command that should be executed.
     *
     * @return string
     */
    public function getCommand(): string;
}
```

If you want to view an example implementation take a look at [the existing optimizers](https://github.com/spatie/image-optimizer/tree/master/src/Optimizers) shipped with this package.

You can easily add your optimizer by using the `addOptimizer` method on an `OptimizerChain`.

```
use Spatie\ImageOptimizer\ImageOptimizerFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain
   ->addOptimizer(new YourCustomOptimizer())
   ->optimize($pathToImage);
```

Logging the optimization process
--------------------------------

[](#logging-the-optimization-process)

By default the package will not throw any errors and just operate silently. To verify what the package is doing you can set a logger:

```
use Spatie\ImageOptimizer\OptimizerChainFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain
   ->useLogger(new MyLogger())
   ->optimize($pathToImage);
```

A logger is a class that implements `Psr\Log\LoggerInterface`. A good logging library that's fully compliant is [Monolog](https://github.com/Seldaek/monolog). The package will write to log which `Optimizers` are used, which commands are executed and their output.

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

[](#example-conversions)

Here are some real life example conversions done by this package.

### png

[](#png)

Original: Photoshop 'Save for web' | PNG-24 with transparency
40 KB

[![Original](https://camo.githubusercontent.com/9182e82bc25d6d6c556f292bf67326ff4fc0e4014d7a91772bd572e2f45208ab/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f6c6f676f2e706e67)](https://camo.githubusercontent.com/9182e82bc25d6d6c556f292bf67326ff4fc0e4014d7a91772bd572e2f45208ab/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f6c6f676f2e706e67)

Optimized
16 KB (40%)

[![Optimized](https://camo.githubusercontent.com/8299a6e17eb1bdd4728a4997ea9de538db0a4a69c86ae71ed539e217c51ac5af/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f6c6f676f2d6f7074696d697a65642e706e67)](https://camo.githubusercontent.com/8299a6e17eb1bdd4728a4997ea9de538db0a4a69c86ae71ed539e217c51ac5af/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f6c6f676f2d6f7074696d697a65642e706e67)

### jpg

[](#jpg)

Original: Photoshop 'Save for web' | quality 60, optimized
547 KB

[![Original](https://camo.githubusercontent.com/62c901aa8176eeaac911fb906b00837ea74e56f7ee1f76b35468c2075f0262e3/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652e6a7067)](https://camo.githubusercontent.com/62c901aa8176eeaac911fb906b00837ea74e56f7ee1f76b35468c2075f0262e3/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652e6a7067)

Optimized
525 KB (95%)

[![Optimized](https://camo.githubusercontent.com/ca89553aa61c6cfce5c410e9b2e836a1c8d6d7b8740f384cf3f6246290c104e5/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652d6f7074696d697a65642e6a7067)](https://camo.githubusercontent.com/ca89553aa61c6cfce5c410e9b2e836a1c8d6d7b8740f384cf3f6246290c104e5/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652d6f7074696d697a65642e6a7067)

credits: Jeff Sheldon, via [Unsplash](https://unsplash.com)

### svg

[](#svg)

Original: Illustrator | Web optimized SVG export
26 KB

[![Original](https://camo.githubusercontent.com/cba1dab4b3b8fa700fc0df84146bb2f0fb8cd93211ad940a235db3d52779adb2/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f67726170682e737667)](https://camo.githubusercontent.com/cba1dab4b3b8fa700fc0df84146bb2f0fb8cd93211ad940a235db3d52779adb2/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f67726170682e737667)

Optimized
20 KB (76%)

[![Optimized](https://camo.githubusercontent.com/0d21c20e4bef74cabb24f6483f1b0991a7c1751aeabf253cb771a0f980906eb3/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f67726170682d6f7074696d697a65642e737667)](https://camo.githubusercontent.com/0d21c20e4bef74cabb24f6483f1b0991a7c1751aeabf253cb771a0f980906eb3/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f67726170682d6f7074696d697a65642e737667)

### webp

[](#webp)

Original: WebPonize
528 KB

[![Original](https://camo.githubusercontent.com/b8b041cc28321fd8c7cbfbbea36678d42e715acfbc0c5256d455e7b98328d350/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652e77656270)](https://camo.githubusercontent.com/b8b041cc28321fd8c7cbfbbea36678d42e715acfbc0c5256d455e7b98328d350/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652e77656270)

Optimized
328 KB (89%)

[![Optimized](https://camo.githubusercontent.com/83d27b46940ef71620b5ce3010f0d13139a6bfc52e25230750d9bbd72225afa9/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652d6f7074696d697a65642e77656270)](https://camo.githubusercontent.com/83d27b46940ef71620b5ce3010f0d13139a6bfc52e25230750d9bbd72225afa9/68747470733a2f2f7370617469652e6769746875622e696f2f696d6167652d6f7074696d697a65722f6578616d706c65732f696d6167652d6f7074696d697a65642e77656270)

credits: Jeff Sheldon, via [Unsplash](https://unsplash.com)

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)

This package has been inspired by [psliwa/image-optimizer](https://github.com/psliwa/image-optimizer)

Emotional support provided by [Joke Forment](https://twitter.com/pronneur)

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

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 75.2% 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 ~31 days

Recently: every ~70 days

Total

26

Last Release

2446d ago

Major Versions

0.0.4 → 1.0.02017-07-05

PHP version history (3 changes)0.0.1PHP ^7.1

0.0.2PHP ^7.0

1.1.5PHP ^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 (179 commits)")[![ahtinurme](https://avatars.githubusercontent.com/u/282119?v=4)](https://github.com/ahtinurme "ahtinurme (18 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (7 commits)")[![johanleroux](https://avatars.githubusercontent.com/u/1549523?v=4)](https://github.com/johanleroux "johanleroux (5 commits)")[![xbing2002](https://avatars.githubusercontent.com/u/37572472?v=4)](https://github.com/xbing2002 "xbing2002 (5 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (4 commits)")[![n3vrax](https://avatars.githubusercontent.com/u/5805542?v=4)](https://github.com/n3vrax "n3vrax (2 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![jpuck](https://avatars.githubusercontent.com/u/15305396?v=4)](https://github.com/jpuck "jpuck (2 commits)")[![a3020](https://avatars.githubusercontent.com/u/1431100?v=4)](https://github.com/a3020 "a3020 (2 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (2 commits)")[![tangrufus](https://avatars.githubusercontent.com/u/2259834?v=4)](https://github.com/tangrufus "tangrufus (2 commits)")[![RobLui](https://avatars.githubusercontent.com/u/10846766?v=4)](https://github.com/RobLui "RobLui (1 commits)")[![rrelmy](https://avatars.githubusercontent.com/u/442683?v=4)](https://github.com/rrelmy "rrelmy (1 commits)")[![willemvb](https://avatars.githubusercontent.com/u/1336390?v=4)](https://github.com/willemvb "willemvb (1 commits)")[![breadlesscode](https://avatars.githubusercontent.com/u/5465112?v=4)](https://github.com/breadlesscode "breadlesscode (1 commits)")[![itsgoingd](https://avatars.githubusercontent.com/u/821582?v=4)](https://github.com/itsgoingd "itsgoingd (1 commits)")[![srmklive](https://avatars.githubusercontent.com/u/839335?v=4)](https://github.com/srmklive "srmklive (1 commits)")[![barryvdh](https://avatars.githubusercontent.com/u/973269?v=4)](https://github.com/barryvdh "barryvdh (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

spatieimage-optimizer

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/image-optimizer

Easily optimize images using PHP

2.9k71.3M109](/packages/spatie-image-optimizer)[spatie/image

Manipulate images with an expressive API

1.4k54.4M137](/packages/spatie-image)[spatie/db-dumper

Dump databases

1.2k25.9M69](/packages/spatie-db-dumper)[magento/community-edition

Magento 2 (Open Source)

12.1k52.1k10](/packages/magento-community-edition)[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)

PHPackages © 2026

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