Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. LiipImagineBundle
  5. Filters

Filters

Edit this page

Filters perform image transformation operations. While some filter set definitions may only require a single filter, more complex definitions often require many filters. Any number of filters can be chained to achieve the desired result.

Tip

You may need to define your own filter to meet your application's specific requirements. Reference the custom filters section for implementation details.

Built-in filters

A number of built-in filters are provided to fulfill the majority of common use-cases.

  • Size Filters
    • Thumbnails
    • Fixed size
    • Cropping Images
    • Relative Resize
    • Scale
    • Down Scale
    • Up Scale
  • Orientation Filters
    • Auto Rotate
    • Rotate
    • Flip
  • General Filters
    • Background
    • Grayscale
    • Interlace
    • Resample
    • Strip
    • Watermark

Custom filters

You can write your own filters to perform any image transformation operations. Custom filters need to implement the filter LoaderInterface (not to be confused with the data loader interface in the Liip\ImagineBundle\Binary\Loader namespace):

1
2
3
4
5
6
namespace Liip\ImagineBundle\Imagine\Filter\Loader;

interface LoaderInterface
{
    public function load(ImageInterface $image, array $options = []);
}

The LoaderInterface has the method load, which is provided an instance of ImageInterface and an array of options. It must return an ImageInterface.

Register it: automatically

By default, your filter will be automatically registered as it implements the LoaderInterface.

You will be able to reference and use your custom filter when defining filter sets in your configuration by using its Fully Qualified Class Name.

Register it: manually

If you want to give it a different name you need to configure a service and tag it liip_imagine.filter.loader.

To register a filter App\Service\MyCustomFilter as my_custom_filter, use the following configuration:

1
2
3
4
5
6
7
# app/config/services.yml

services:
    app.filter.my_custom_filter:
        class: App\Service\MyCustomFilter
        tags:
            - { name: "liip_imagine.filter.loader", loader: my_custom_filter }
1
2
3
4
5
<!-- app/config/services.xml -->

<service id="app.filter.my_custom_filter" class="App\Service\MyCustomFilter">
    <tag name="liip_imagine.filter.loader" loader="my_custom_filter" />
</service>

You can now reference and use your custom filter when defining filter sets in your configuration:

1
2
3
4
5
6
7
# app/config/config.yml

liip_imagine:
    filter_sets:
        my_special_style:
            filters:
                my_custom_filter: { }

Dynamic filters

It is possible to dynamically modify the configuration that will be applied to the image, by passing configuration as third parameter to applyFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace App\Service;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;

class ImageService
{
    private $cacheManager;
    private $dataManager;
    private $filterManager;

    public function __construct(CacheManager $cacheManager, DataManager $dataManager, FilterManager $filterManager) {
        $this->cacheManager  = $cacheManager;
        $this->dataManager   = $dataManager;
        $this->filterManager = $filterManager;
    }

    public function filter(int $width, int $height) {
        $filter = '...'; // Name of the `filter_set` in `config/packages/liip_imagine.yaml`
        $path = '...'; // Path of the image, relative to `/public/`

        if (!$this->cacheManager->isStored($path, $filter)) {
            $binary = $this->dataManager->find($filter, $path);

            $filteredBinary = $this->filterManager->applyFilter($binary, $filter, [
                'filters' => [
                    'thumbnail' => [
                        'size' => [$width, $height]
                    ]
                ]
            ]);

            $this->cacheManager->store($filteredBinary, $path, $filter);
        }
        return new RedirectResponse($this->cacheManager->resolve($path, $filter), Response::HTTP_MOVED_PERMANENTLY);
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Version:

    Table of Contents

    • Built-in filters
    • Custom filters
      • Register it: automatically
      • Register it: manually
    • Dynamic filters

    Symfony footer

    Avatar of Rob Meijer, a Symfony contributor

    Thanks Rob Meijer (@robmeijer) for being a Symfony contributor

    3 commits • 7 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony