The purpose of this library is to help automate the optimisation of images via the command line in PHP,
The library is PSR-4 compliant and the simplest way to install it is via composer, simply add:
composer require bensquire/php-image-optim
into your composer.json, then run 'composer install' or 'composer update' as required.
brew install Advancecomp # AdvPNG
brew install gifsicle
brew install guetzli
brew install jonof/kenutils/pngout
brew install jpeg # JPEGTran
brew install jpegoptim
brew install mozjpeg
brew install optipng
brew install pngcrush
brew install pngquant
brew install zopfli # Future
brew install svgo # Future
It's worth noting that mozJpeg is a fork of libjpeg-turbo and as such isn't a binary with it's own name, for example to use it in this library:
$tool = new \PHPImageOptim\Tools\Jpeg\MozJpeg();
$tool->setBinaryPath('/usr/local/opt/mozjpeg/bin/jpegtran');
This example demonstrates the optimisation of a PNG file, by chaining several commands together.
<?php
include('./vendor/autoload.php');
use PHPImageOptim\Tools\Png\AdvPng;
use PHPImageOptim\Tools\Png\OptiPng;
use PHPImageOptim\Tools\Png\PngCrush;
use PHPImageOptim\Tools\Png\PngOut;
use PHPImageOptim\Tools\Png\PngQuant;
$advPng = new AdvPng();
$advPng->setBinaryPath('/usr/local/bin/advpng');
$optiPng = new OptiPng();
$optiPng->setBinaryPath('/usr/local/bin/optipng');
$pngOut = new PngOut();
$pngOut->setBinaryPath('/usr/bin/pngout');
$pngCrush = new PngCrush();
$pngCrush->setBinaryPath('/usr/local/bin/pngcrush');
$pngQuant = new PngQuant();
$pngQuant->setBinaryPath('/usr/local/bin/pngquant');
$optim = new PHPImageOptim();
$optim->setImage('./tests/image/lenna.png');
$optim->chainCommand($pngQuant)
->chainCommand($advPng)
->chainCommand($optiPng)
->chainCommand($pngCrush)
->chainCommand($pngOut);
$optim->optimise();
composer php-cs-fixer
composer php-stan
composer tests
Add zopfli support?
Add svgo support?