The Wayback Machine - https://web.archive.org/web/20221014154345/https://github.com/bensquire/php-image-optim
Skip to content

bensquire/php-image-optim

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

php-image-optim

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?