ekino / tiny-png-sonata-media-bundle
Tiny Png integration with SonataMedia by Ekino
Installs: 460
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 14
Forks: 4
Open Issues: 4
Type:symfony-bundle
Requires
- php: ^7.2 || ^8.0
- doctrine/orm: ^2.6
- sonata-project/admin-bundle: ^3.1
- sonata-project/media-bundle: ^3.0
- sonata-project/notification-bundle: ^3.1
- symfony/framework-bundle: ^4.4 || ^5.3
- symfony/translation: ^4.4 || ^5.3
- tinify/tinify: ^1.5
Requires (Dev)
- ekino/phpstan-banned-code: ^0.3 || ^0.4
- friendsofphp/php-cs-fixer: ^2.12
- phpstan/phpstan-phpunit: ^0.12
- phpunit/phpunit: ^8.5
- symfony/phpunit-bridge: ^5.0
Suggests
- liip/monitor-bundle: ^2.16
Conflicts
- sonata-project/admin-bundle: <3.27
This package is auto-updated.
Last update: 2024-12-06 08:09:04 UTC
README
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!
Resources
Purpose
Automatize image optimization through tinyPNG service. You can only use the client or get the full process with sonata media and sonata notification.
Installation
Step 1: add dependency
$ composer require ekino/tiny-png-sonata-media-bundle
Step 2: register the bundle
Symfony 2 or 3:
<?php // app/AppKernel.php public function registerBundles() { $bundles = [ // ... new Ekino\TinyPngSonataMediaBundle\EkinoTinyPngSonataMediaBundle(), // ... ]; }
Symfony 4:
<?php // config/bundles.php return [ // ... Ekino\TinyPngSonataMediaBundle\EkinoTinyPngSonataMediaBundle::class => ['all' => true], // ... ];
Step 3: configure the bundle
ekino_tiny_png_sonata_media: tiny_png_api_key: ~ # required providers: [] # default max_compression_count_by_month: 500 # default value defined in config
Step 4: define the sonata notification queue for asynchronous behaviour
Usage
Use the tinyPng client
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);
Full process with sonata media & notification
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.
Suggest install LiipMonitorBundle
This bundle provides a service using liip/monitor-bundle to check the count of compressions made this month.
Note
- 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.