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

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

scratcher28/image-optimizer
===========================

Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.

2.0.6(3y ago)11761MITPHPPHP ^7.0|^8.0

Since Aug 3Pushed 3y agoCompare

[ Source](https://github.com/scratcher28/image-optimizer)[ Packagist](https://packagist.org/packages/scratcher28/image-optimizer)[ Fund](https://www.paypal.me/psliwa)[ GitHub Sponsors](https://github.com/psliwa)[ RSS](/packages/scratcher28-image-optimizer/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (2)Dependencies (4)Versions (24)Used By (1)

Image Optimizer (mod) [![Build Status](https://camo.githubusercontent.com/5d88b8f8c3ec2de9ed7b595452dc6da0c7df4d3289d2c793d6c0b353fd8912e7/68747470733a2f2f7472617669732d63692e6f72672f70736c6977612f696d6167652d6f7074696d697a65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/psliwa/image-optimizer)
=======================================================================================================================================================================================================================================================================================================================

[](#image-optimizer-mod-)

This library is handy and very easy to use optimizer for image files. It uses [optipng](http://optipng.sourceforge.net/), [pngquant](http://pngquant.org/), [jpegoptim](http://jpegclub.org/jpegtran/), [svgo](https://github.com/svg/svgo) and few more libraries, so before use it you should install proper libraries on your server. Project contains Vagrantfile that defines testing virtual machine with all libraries installed, so you can check Vagrantfile how to install all those stuff.

Thanks to ImageOptimizer and libraries that it uses, your image files can be **10%-70% smaller**.

Installation
============

[](#installation)

Using composer:

```
composer require ps/image-optimizer

```

Basic usage
===========

[](#basic-usage)

```
$factory = new \ImageOptimizer\OptimizerFactory();
$optimizer = $factory->get();

$filepath = /* path to image */;

$optimizer->optimize($filepath);
//optimized file overwrites original one
```

Configuration
=============

[](#configuration)

By default optimizer does not throw any exception, if file can not be optimized or optimizing library for given file is not installed, optimizer will not touch original file. This behaviour is ok when you want to eventually optimize files uploaded by user. When in your use case optimization fault should cause exception, `ignore_errors` option was created especially for you.

This library is very smart, you do not have to configure paths to all binaries of libraries that are used by ImageOptimizer, library will be looking for those binaries in few places, so if binaries are placed in standard places, it will be found automatically.

Supported options:

- `ignore_errors` (default: true)
- `single_optimizer_timeout_in_seconds` (default: 60) - useful when you want to have control how long optimizing lasts. For example in some cases optimizing may not be worth when it takes big amount of time. Pass `null` in order to turn off timeout.
- `output_filepath_pattern` (default: `%basename%/%filename%%ext%`) - destination where optimized file will be stored. By default it overrides original file. There are 3 placehoders: `%basename%`, `%filename%` (without extension and dot) and `%ext%` (extension with dot) which will be replaced by values from original file.
- `execute_only_first_png_optimizer` (default: true) - execute the first successful or all `png` optimizers
- `execute_only_first_jpeg_optimizer` (default: true) - execute the first successful or all `jpeg` optimizers
- `optipng_options` (default: `array('-i0', '-o2', '-quiet')`) - an array of arguments to pass to the library
- `pngquant_options` (default: `array('--force')`)
- `pngcrush_options` (default: `array('-reduce', '-q', '-ow')`)
- `pngout_options` (default: `array('-s3', '-q', '-y')`)
- `advpng_options` (default: `array('-z', '-4', '-q')`)
- `gifsicle_options` (default: `array('-b', '-O5')`)
- `jpegoptim_options` (default: `array('--strip-all', '--all-progressive')`)
- `jpegtran_options` (default: `array('-optimize', '-progressive')`)
- `svgo_options` (default: `array('--disable=cleanupIDs')`)
- `custom_optimizers` (default `array()`)
- `optipng_bin` (default: will be guessed) - you can enforce paths to binaries, but by default it will be guessed
- `pngquant_bin`
- `pngcrush_bin`
- `pngout_bin`
- `advpng_bin`
- `gifsicle_bin`
- `jpegoptim_bin`
- `jpegtran_bin`
- `svgo_bin`

You can pass array of options as first argument of `ImageOptimizer\OptimizerFactory` constructor. Second argument is optionally `Psr\LoggerInterface`.

```
$factory = new \ImageOptimizer\OptimizerFactory(array('ignore_errors' => false), $logger);
```

Supported optimizers
====================

[](#supported-optimizers)

- default (`smart`) - it guess file type and choose optimizer for this file type
- `png` - chain of optimizers for png files, by default it uses `pngquant` and `optipng`. `pngquant` is lossy optimization
- `jpg` - first of two optimizations will be executed: `jpegtran` or `jpegoptim`
- `gif` - alias to `gifsicle`
- `pngquant` - [homepage](http://pngquant.org/)
- `optipng` - [homepage](http://optipng.sourceforge.net/)
- `pngcrush` - [homepage](http://pmt.sourceforge.net/pngcrush/)
- `pngout` - [homepage](http://www.jonof.id.au/kenutils)
- `advpng` - [homepage](http://advancemame.sourceforge.net/doc-advpng.html)
- `jpegtran` - [homepage](http://jpegclub.org/jpegtran/)
- `jpegoptim` - [homepage](http://freecode.com/projects/jpegoptim)
- `gifsicle` - [homepage](http://www.lcdf.org/gifsicle/)
- `svgo` - [homepage](https://github.com/svg/svgo)

You can obtain concrete optimizer by passing his name to `ImageOptimizer\OptimizerFactory`::`get` method:

```
//default optimizer is `smart`
$optimizer = $factory->get();

//png optimizer
$pngOptimizer = $factory->get('png');

//jpegoptim optimizer etc.
$jpgOptimizer = $factory->get('jpegoptim');
```

Custom optimizers
=================

[](#custom-optimizers)

You can easily define custom optimizers:

```
$factory = new \ImageOptimizer\OptimizerFactory(array('custom_optimizers' => array(
    'some_optimizier' => array(
        'command' => 'some_command',
        'args' => array('-some-flag')
    )
)), $logger);
```

And then usage:

```
$customOptimizer = $factory->get('some_optimizier');
```

I got "All optimizers failed to optimize the file"
==================================================

[](#i-got-all-optimizers-failed-to-optimize-the-file)

Probably you don't have required optimazers installed. Let's have a look at `Vagrantfile` file in order to see an example how to install those commands.

In order to see all intermediate errors, you can use logger (be default `NullLogger` is used, so logs are not available):

```
class StdoutLogger extends \Psr\Log\AbstractLogger {
    public function log($level, $message, array $context = array()) {
        echo $message."\n";
    }
}

$factory = new \ImageOptimizer\OptimizerFactory(array(), new StdoutLogger());

$factory->get()->optimize('yourfile.jpg');

// and have a look at stdout
```

License
=======

[](#license)

**MIT**

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 70.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 ~142 days

Recently: every ~286 days

Total

23

Last Release

1172d ago

Major Versions

1.2.2 → 2.0.02019-07-13

1.2.3 → 2.0.22020-01-11

PHP version history (2 changes)2.0.0PHP ^7.1

2.0.5PHP ^7.0|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5d737ae125f717a0462dbab0ba2e5b24b1745b7a21c0344cc1109a305e112cec?d=identicon)[scratcher28](/maintainers/scratcher28)

---

Top Contributors

[![psliwa](https://avatars.githubusercontent.com/u/438063?v=4)](https://github.com/psliwa "psliwa (33 commits)")[![umpirsky](https://avatars.githubusercontent.com/u/208957?v=4)](https://github.com/umpirsky "umpirsky (2 commits)")[![niels3W](https://avatars.githubusercontent.com/u/13118004?v=4)](https://github.com/niels3W "niels3W (2 commits)")[![SamarRizvi](https://avatars.githubusercontent.com/u/1614414?v=4)](https://github.com/SamarRizvi "SamarRizvi (2 commits)")[![scratcher28](https://avatars.githubusercontent.com/u/778716?v=4)](https://github.com/scratcher28 "scratcher28 (2 commits)")[![budnieswski](https://avatars.githubusercontent.com/u/5929538?v=4)](https://github.com/budnieswski "budnieswski (1 commits)")[![onEXHovia](https://avatars.githubusercontent.com/u/3332033?v=4)](https://github.com/onEXHovia "onEXHovia (1 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (1 commits)")[![hellofarhan](https://avatars.githubusercontent.com/u/4665031?v=4)](https://github.com/hellofarhan "hellofarhan (1 commits)")[![Jean85](https://avatars.githubusercontent.com/u/6729988?v=4)](https://github.com/Jean85 "Jean85 (1 commits)")[![lkorth](https://avatars.githubusercontent.com/u/1163999?v=4)](https://github.com/lkorth "lkorth (1 commits)")

---

Tags

imagecompressionimage-optimizeroptimizationoptipngjpegoptim

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[ps/image-optimizer

Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.

9341.7M25](/packages/ps-image-optimizer)[spatie/image-optimizer

Easily optimize images using PHP

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

PHP Benchmarking Framework

2.0k13.0M627](/packages/phpbench-phpbench)[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k38.3M217](/packages/liip-imagine-bundle)[spatie/image

Manipulate images with an expressive API

1.4k54.4M138](/packages/spatie-image)[typisttech/image-optimize-command

Easily optimize images using WP CLI

1722.0k](/packages/typisttech-image-optimize-command)

PHPackages © 2026

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