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).
$ composer require setono/sylius-image-optimizer-plugin
This will also install the Kraken.io Bundle which is need for configuring the API keys.
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],
// ...
];
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"
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
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
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
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
Now run this command to optimize your images:
$ php bin/console setono:sylius-image-optimizer:optimize
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