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

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

webdev0x1/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.

01PHP

Since Jul 13Pushed 10mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Image Optimizer [![Build Status](https://github.com/flynsarmy/image-optimizer/actions/workflows/ci-php.yml/badge.svg)](https://github.com/flynsarmy/image-optimizer/actions/workflows/ci-php.yml/badge.svg)
===========================================================================================================================================================================================================

[](#image-optimizer-)

Fork of [ps/image-optimizer](https://github.com/psliwa/image-optimizer) with many improvements. See [Changelog](CHANGELOG.md) for details.

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 flynsarmy/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');
```

Checking which optimizers are installed on your system
======================================================

[](#checking-which-optimizers-are-installed-on-your-system)

The following code will run a `which` on each executeable to determine if they are installed on your system and return a list of true/false results.

```
$factory = new \ImageOptimizer\OptimizerFactory();
print_r($factory->checkOptimizers());
```

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

17

—

LowBetter than 6% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3106a6b0cc263a58b0a03d49cbab1b3ec464aae1ba0fef16a5c98d73554c5b4d?d=identicon)[chandirasekaranid](/maintainers/chandirasekaranid)

---

Top Contributors

[![psliwa](https://avatars.githubusercontent.com/u/438063?v=4)](https://github.com/psliwa "psliwa (39 commits)")[![Flynsarmy](https://avatars.githubusercontent.com/u/334808?v=4)](https://github.com/Flynsarmy "Flynsarmy (21 commits)")[![webdev0x1](https://avatars.githubusercontent.com/u/82759103?v=4)](https://github.com/webdev0x1 "webdev0x1 (4 commits)")[![ThibaultVlacich](https://avatars.githubusercontent.com/u/334940?v=4)](https://github.com/ThibaultVlacich "ThibaultVlacich (4 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)")[![umpirsky](https://avatars.githubusercontent.com/u/208957?v=4)](https://github.com/umpirsky "umpirsky (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)")[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (1 commits)")[![hellofarhan](https://avatars.githubusercontent.com/u/4665031?v=4)](https://github.com/hellofarhan "hellofarhan (1 commits)")[![evertharmeling](https://avatars.githubusercontent.com/u/308513?v=4)](https://github.com/evertharmeling "evertharmeling (1 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (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)")

### Embed Badge

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

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

###  Alternatives

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k23](/packages/bkwld-croppa)[goat1000/svggraph

Generates SVG graphs

132849.6k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)[netresearch/rte-ckeditor-image

Image support in CKEditor for the TYPO3 ecosystem - by Netresearch

63991.3k4](/packages/netresearch-rte-ckeditor-image)[humanmade/tachyon-plugin

Rewrites WordPress image URLs to use Tachyon

87338.5k2](/packages/humanmade-tachyon-plugin)

PHPackages © 2026

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