LiipImagineBundle icon indicating copy to clipboard operation
LiipImagineBundle copied to clipboard

Runtime config - add optional filter in main config

Open sabat24 opened this issue 5 years ago • 0 comments

My use case. I have got a simple thumbnail filter:

basic_thumbnail:
            filters:
                thumbnail:
                    size:          [570, 380]
                    mode:          outbound
                    allow_upscale: true

I also allow my users to select the area of main image from which thumbnail will be generated. User can select an area respecting thumbnail ratio or ignore that possibility. In twig I generate thumbnails as follows:

{% set runtimeConfig = imageCropFilter %}
<img src="{{ asset(news, 'imageFile')|imagine_filter('basic_thumbnail', runtimeConfig) }}"

Problem with such option is that the new filter is merged to the original, which leads to situation where my additional crop filter is applied after creating of thumbnail, which is wrong (wrong in my use case to be clear).

I can change my config to something like that

basic_thumbnail:
            filters:
                crop: <--
                thumbnail:
                    size:          [570, 380]
                    mode:          outbound
                    allow_upscale: true

leaving crop filter empty. In this case merge will apply my runtime config in correct order. However in this case I will receive an exception from image/box.php constructor that width and heigh can't be empty.

I can resolve the problem by commenting out that line

throw new InvalidArgumentException(sprintf('Length of either side cannot be 0 or negative, current size is %sx%s', $width, $height));

and then adding following lines to crop method

if (empty($width) && empty($height)) {
     return $this;
}

Such approach allows me to create an optional crop filter, which will be ignored if no runtime config will be applied.

All in all I ask about a feature to set such optional filters out of the box to define order of filters which may be added later.

sabat24 avatar Apr 20 '20 12:04 sabat24