This is a work in progress, so if you'd like something implemented please feel free to ask for it or contribute to help us!
Automatize image optimization through tinyPNG service. You can only use the client or get the full process with sonata media and sonata notification.
$ composer require ekino/tiny-png-sonata-media-bundle
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = [
// ...
new Ekino\TinyPngSonataMediaBundle\EkinoTinyPngSonataMediaBundle(),
// ...
];
}
<?php
// config/bundles.php
return [
// ...
Ekino\TinyPngSonataMediaBundle\EkinoTinyPngSonataMediaBundle::class => ['all' => true],
// ...
];
ekino_tiny_png_sonata_media:
tiny_png_api_key: ~ # required
providers: [] # default
max_compression_count_by_month: 500 # default value defined in config
Client can be used directly to optimize images through tinyPNG API. However, image optimization should not be done synchronously as it takes time.
If you know what you are doing, you can use the ekino.tiny_png_sonata_media.tinify.client
and its optimize
method:
<?php
$client->optimize($inputPath, $outputPath, $overwrite);
This bundle listen doctrine events (postPersist & postUpdate) on media entity. As soon as the media's provider is in
the whitelist (defined in configuration), it will publish a sonata notification message
(type: ekino.tiny_png_sonata_media.optimize_image
) to be handled by a consumer
(Ekino\TinyPngSonataMediaBundle\Consumer\OptimizeImageConsumer
). This consumer will contact tinyPNG API for
optimization, replace it on the server and update media size in database.
This bundle provides a service using liip/monitor-bundle to check the count of compressions made this month.
- Only Sonata\MediaBundle\Filesystem\Local adapter is supported for now.
- Only png, jpg & jpeg files extensions are handled by this bundle as the tinyPNG only handle those ones.
- Regeneration of thumbnails after optimization is not yet supported.