ZF Image Optimiser

Tue 15 Oct 2019

I've created an image optimiser composer package. The source is also available on github! The package is inspired by spatie's package. My goal was setting the package up with configuration over convention in mind. This makes it an easier package to work with in Zend projects.

Currently there are 3 methods you could use to optimise images.

  • Service object
  • Commandline
  • InputFilter

Service Object

The service object is the main part in this package. Both the input filter and the command are proxies to the service object.

The service object is setup to be flexible. Allowing multiple adapters to be setup, each with their own unique criteria. They are highly extensible allowing for easy customisation or room for other optimising programs.

The criteria are build up from standard Zend validators. This way, future validators and custom validators can be hooked into this service.

I've build the service with as much standard components as possible. There is even a logger included. The logs will contain the program output aswell.

By default you don't have to provide a logger. If you want to provide one it will have to implement the Zend\Log\LoggerInterface to be complient. The sample below shows getting the service through the service manager and one where we provide the logger and build it.

class SomeFactory implements FactoryInterface
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
        /** @var ImageOptimiserService $imageOptimiserService */
        $imageOptimiserService = $container->get(ImageOptimiserService::class);

        # or... provide your own LoggerInterface

        /** @var Logger $logger */
        $logger = $container->get(Logger::class);
        /** @var Stream $writer */
        $logger->addWriter(new Stream('php://output'));
        /** @var ImageOptimiserService $imageOptimiserService */
        $imageOptimiserService = $container->build(ImageOptimiserService::class, ['logger' => $logger]);

        # ... other factory stuff


CLI support will add a whole other dimension to a package like this. You might consider using the CLI when you want to optimise an existing gallery for example.

You can do so by running the following command from the root directory of your project. The commandline interface will be setup with an stdout logger, making all processing visible.

php ./public/index.php image-optimiser --image='./public/images/test.png' 


InputFilter support makes it a breeze. Easy to implement and no custom logic needed. You can simply add this to your chain of filters in any Zend\InputFilter\FileInput filter chain.

        'type'       => FileInput::class,
        'name'       => 'image',
        'required'   => false,
        'filters'    => [
            ['name' => ImageOptimiser::class],
        'validators' => [...]


There are a ton of ideas that could be added to this package. Currently the package is fully functional for the goals I set myself. There is room for improvement and already have some good ideas to expand this package. A few being...

  • target option on both InputFilter and service for creating a new file instead of overwriting
  • directory support in CLI to optimise all images within a certain folder
  • Finely tweaked default settings that we dist
  • Other image optimiser adapters
  • Unit tests