bensquire / php-image-optim
A library to aid in image optimisation
Installs: 40 560
Dependents: 1
Suggesters: 0
Security: 0
Stars: 86
Watchers: 9
Forks: 19
Open Issues: 9
Requires
- php: >=7.3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: 9.*
README
The purpose of this library is to help automate the optimisation of images via the command line in PHP,
Installation:
Library
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.
Binaries
MacOS
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');
Example
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();
Tooling
Fix common coding inconsistencies
composer php-cs-fixer
Find coding issues
composer php-stan
Run unit tests
composer tests
TODO:
Add zopfli support?
Add svgo support?