setono / sylius-image-optimizer-plugin
Sylius plugin that optimizes your images
Installs: 28 206
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 2
Open Issues: 11
Type:sylius-plugin
Requires
- php: >=7.3
- ext-mbstring: *
- doctrine/common: ^2.13 || ^3.0
- doctrine/event-manager: ^1.1
- doctrine/orm: ^2.7
- doctrine/persistence: ^1.3 || ^2.0
- knplabs/knp-menu: ^3.1
- liip/imagine-bundle: ^2.3
- psr/container: ^1.0
- psr/log: ^1.1
- setono/doctrine-orm-batcher: ^0.6
- setono/doctrine-orm-batcher-bundle: ^0.3.1
- setono/kraken-io-bundle: ^1.0
- setono/kraken-io-php-sdk: ^1.1
- sylius/resource-bundle: ^1.6
- symfony/config: ^4.4 || ^5.0
- symfony/console: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/event-dispatcher: ^4.4 || ^5.0
- symfony/http-client-contracts: ^2.3
- symfony/http-foundation: ^4.4 || ^5.0.7
- symfony/lock: ^4.4 || ^5.0
- symfony/messenger: ^4.4 || ^5.0
- symfony/mime: ^4.4 || ^5.0
- symfony/routing: ^4.4 || ^5.0
- symfony/service-contracts: ^2.2
- thecodingmachine/safe: ^1.0
- twig/twig: ^2.14
- webimpress/safe-writer: ^2.0
Requires (Dev)
- kriswallsmith/buzz: ^1.1
- matthiasnoback/symfony-config-test: ^4.2
- matthiasnoback/symfony-dependency-injection-test: ^4.2
- nyholm/psr7: ^1.3
- phpunit/phpunit: ^9.4
- roave/security-advisories: dev-latest
- setono/code-quality-pack: ^1.5.2
- sylius/sylius: ~1.7.11
- symfony/debug-bundle: ^5.1
- symfony/dotenv: ^5.2
- symfony/http-client: ^5.2
- symfony/web-profiler-bundle: ^5.0.11
- dev-master / 1.0.x-dev
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-renovate/configure
- dev-dependabot/github_actions/actions/checkout-4
- dev-dependabot/github_actions/ramsey/composer-install-2
- dev-dependabot/composer/sylius/sylius-approx-1.7.11or-approx-1.10.0
- dev-dependabot/composer/setono/code-quality-pack-tw-1.5.2or-tw-2.0.0
- dev-dependabot/github_actions/shivammathur/setup-php-2.11.0
This package is auto-updated.
Last update: 2024-12-15 14:04:05 UTC
README
Optimize the images in your Sylius store! Works only with kraken.io at the moment, but will work with other vendors in the future.
Have you seen this message from Google page speed tools: "Serve images in next-gen formats", this is the plugin to use.
It will both optimize your jpegs, but also convert them to webp and serve the webp images to browsers that support webp (i.e. Chrome).
Installation
Step 1: Download the plugin
$ composer require setono/sylius-image-optimizer-plugin
This will also install the Kraken.io Bundle which is need for configuring the API keys.
Step 2: Enable the plugin
Then, enable the plugin by adding it to the list of registered plugins/bundles
in the config/bundles.php
file of your project:
Make sure you add it before SyliusGridBundle
, otherwise you'll get
You have requested a non-existent parameter "setono_sylius_image_optimizer.model.savings.class".
exception.
<?php # config/bundles.php return [ // ... Setono\SyliusImageOptimizerPlugin\SetonoSyliusImageOptimizerPlugin::class => ['all' => true], Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true], // ... ];
Step 3: Configure the plugin
Add the resources (image resources) that should be optimized. In the example below a product image is optimized and the filter sets that are optimized are the default frontend filter sets for products.
This is also the step where you need your API key and secret from kraken.io.
# .env.local
KRAKEN_API_KEY=YOUR API KEY
KRAKEN_API_SECRET=YOUR API SECRET
# config/packages/setono_kraken_io.yaml setono_kraken_io: api_key: "%env(KRAKEN_API_KEY)%" api_secret: "%env(KRAKEN_API_SECRET)%"
# config/packages/setono_sylius_image_optimizer.yaml imports: - { resource: "@SetonoSyliusImageOptimizerPlugin/Resources/config/app/config.yaml" } setono_sylius_image_optimizer: image_resources: sylius.product_image: # Run "php bin/console debug:config liip_imagine filter_sets" to find the filter sets configured in your app - sylius_shop_product_large_thumbnail - sylius_shop_product_thumbnail - sylius_shop_product_small_thumbnail - sylius_shop_product_tiny_thumbnail - sylius_shop_product_original
# config/routes/setono_sylius_image_optimizer.yaml setono_sylius_image_optimizer: resource: "@SetonoSyliusImageOptimizerPlugin/Resources/config/routes.yaml"
Step 4: Configure Symfony Messenger
If you haven't used Symfony Messenger before, you need to specify a default bus like so:
# config/packages/messenger.yaml framework: messenger: default_bus: setono_sylius_image_optimizer.command_bus
Step 4.1: Using asynchronous transport (optional, but recommended)
All commands in this plugin will extend the CommandInterface and all events will extend the EventInterface.
Therefore, you can route all commands and events easily by adding this to your Messenger config:
# config/packages/messenger.yaml framework: messenger: routing: # Route all command messages to the async transport # This presumes that you have already set up an 'async' transport 'Setono\SyliusImageOptimizerPlugin\Message\Command\CommandInterface': async 'Setono\SyliusImageOptimizerPlugin\Message\Event\EventInterface': async
Step 5: Extend image resources
The following example is for the product images. You should follow this procedure for all image resources you want to optimize.
<?php // src/Entity/Product/ProductImage.php declare(strict_types=1); namespace App\Entity\Product; use Doctrine\ORM\Mapping as ORM; use Setono\SyliusImageOptimizerPlugin\Model\OptimizableInterface; use Setono\SyliusImageOptimizerPlugin\Model\OptimizableTrait; use Sylius\Component\Core\Model\ProductImage as BaseProductImage; /** * @ORM\Table(name="sylius_product_image") * @ORM\Entity() */ final class ProductImage extends BaseProductImage implements OptimizableInterface { use OptimizableTrait; }
# config/packages/_sylius.yaml sylius_core: resources: product_image: classes: model: App\Entity\Product\ProductImage
Step 6: Update database
Last step is to create a diff for your current database schema and migrate that schema.
$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate
Usage
Now run this command to optimize your images:
$ php bin/console setono:sylius-image-optimizer:optimize
Testing
If you want to test this plugin you can setup ngrok to tunnel requests to your localhost:
- Download and install ngrok
- Run your local web server:
symfony serve --allow-http
(theallow-http
is important since ngrok always tunnels to the non secure localhost) - Run ngrok:
ngrok http 8000