Data Loaders

2.0 version
Maintained

Data Loaders

A number of built-in data loaders are available:

Other Data Loaders

  • Doctrine PHPCR-ODM: You can include the CmfMediaBundle alone if you just want to use the images but no other Symfony CMF features.

Custom Data Loader

You can easily define your own, custom data loaders to allow you to retrieve you image data from any imaginable backend. Creating a custom data loader begins by creating a class that implements the LoaderInterface, as shown below.

1
2
3
4
interface LoaderInterface
{
    public function find($path);
}

As defined in LoaderInterface, the only required method is one named find, which is provided a relative image path as its singular parameter, and subsequently provides an instance of BinaryInterface in return.

The following is a template for creating your own data loader. You must provide the implementation for the find method to create a valid data loader.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace AppBundle\Imagine\Binary\Loader;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
use Liip\ImagineBundle\Model\Binary;

class MyCustomDataLoader implements LoaderInterface
{
    /**
     * @param mixed $path
     *
     * @return BinaryInterface
     */
    public function find($path)
    {
        $data = /** @todo: implement logic to read image data */
        $mime = /** @todo: implement logic to determine image mime-type */

        // return binary instance with data
        return new Binary($data, $mime);
    }
}

Once you have defined your custom data loader, you must define it as a service and tag it with liip_imagine.binary.loader.

Note

For more information on the Service Container, reference the official Symfony Service Container documentation.

To register AppBundle\Imagine\Binary\Loader\MyCustomDataLoader with the name my_custom_data_loader, you would use the following configuration.

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    # app/config/services.yml
    
    services:
        imagine.data.loader.my_custom:
            class: AppBundle\Imagine\Binary\Loader\MyCustomDataLoader
            arguments:
                - "@liip_imagine"
                - "%liip_imagine.formats%"
            tags:
                - { name: "liip_imagine.binary.loader", loader: my_custom_data_loader }
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    <!-- app/config/services.xml -->
    
    <service id="imagine.data.loader.my_custom" class="AppBundle\Imagine\Binary\Loader\MyCustomDataLoader">
        <tag name="liip_imagine.data.loader" loader="my_custom_data_loader" />
        <argument type="service" id="liip_imagine" />
        <argument type="parameter" id="liip_imagine.formats" />
    </service>
    

You can set your custom data loader by adding it to the configuration as the new default loader:

1
2
3
4
# app/config/config.yml

liip_imagine:
    data_loader: my_custom_data_loader

Alternatively, you can only set the custom data loader for just a specific filter set:

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

liip_imagine:
    filter_sets:
        my_special_style:
            data_loader: my_custom_data_loader
            filters:
                # your filters

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.