Search code examples
symfonygrayscaleliipimaginebundle

how to apply grayscale filter on an image?


Could you please tell how to apply a grayscale filter on an image with LiipImagineBundle?

NB: I am using Symfony2

Thank,


Solution

  • 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: ~