Could you please tell how to apply a grayscale filter on an image with LiipImagineBundle
?
NB: I am using Symfony2
Thank,
Could you please tell how to apply a grayscale filter on an image with LiipImagineBundle
?
NB: I am using Symfony2
Thank,
This worked for me:
<?php
namespace AppBundle\Imagine\Filter\Loader;
use Imagine\Filter\Advanced\Grayscale;
use Imagine\Image\ImageInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
/**
* Apply a grayscale effect to image.
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = [])
{
$filter = new Grayscale();
return $filter->apply($image);
}
}
Just define a service like this one:
services:
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
And then a filter:
liip_imagine:
filter_sets:
grayscale:
filters:
grayscale: ~
There is no built-in grayscale filter in LiipImagineBundle, so you could create a custom filter loader by 3 steps:
1) At first, you should create GrayscaleFilterLoader class, it should implement the Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface as like as any other custom filter:
namespace AppBundle\Imagine\Filter\Loader;
...
/**
* Class GrayscaleFilterLoader
* @package AppBundle\Imagine\Filter\Loader
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* @param ImageInterface $image
* @param array $options
* @return ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$image->effects()->grayscale();
return $image;
}
}
2) Then, you should register it in the service container and add the liip_imagine.filter.loader tag to it, the loader tag would be the name of the filter:
#app/config/services.yml
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
3) Finally, configure the filter set with it:
#app/config/config.yml
your_filter_set_name:
filters:
...
grayscale: {}
That's it!