LiipImagineBundle icon indicating copy to clipboard operation
LiipImagineBundle copied to clipboard

Default image when filter fails

Open redflo opened this issue 9 months ago • 1 comments

Probably this is already possible, but i did not find it in documentation. Problem: Users can upload data: images, PDF documents, videos. After upload, i try to show a thumbnail of the image. This works well for images and PDF files (most of the time), but not for videos. I tried to set a "default_image" in the filter, but this does not help. My guess is, that default_image only works if the file is not found, but when the image processor cannot generate a image - what then? default_image parameter is not documented at all. So just guessing here!

Here is my config:

liip_imagine:
    twig:
        mode: "lazy"
    # valid drivers options include "gd" or "gmagick" or "imagick"
    driver: "imagick"
    # configure data loaders
    loaders:
    #    sql_binary_data_loader:
       uploads:
           filesystem:
               data-root: '%app.upload-dir%'
               allow_unresolvable_data_roots: true
    #    chain:
    #        chain:
    #            loaders:
    #                - sql
    #                - uploads
    resolvers:
        upload_thumbs:
            web_path: ~
    # configure custom data loader as default
    data_loader: sql_binary_data_loader
    # configure cache resolvers
    cache: sql_binary_cache
    # your filter sets are defined here
    filter_sets:
        # the name of the "filter set"
        sql_thumb:
            # adjust the image quality to 75%
            quality: 75
            # generate jpeg
            format: jpeg
            # list of transformations to apply (the "filters")
            filters:
                thumbnail: 
                    size: [120, 120]
                    mode: inset
                    allow_upscale: true
                background:
                    color: '#ffffff'
        upload_thumb:
            default_image: "images/thumbnail-dummy.png"
            data_loader: uploads
            cache: upload_thumbs
            # adjust the image quality to 75%
            quality: 75
            # generate jpeg
            format: jpeg
            # list of transformations to apply (the "filters")
            filters:
                thumbnail: 
                    size: [120, 120]
                    mode: inset
                    allow_upscale: true
                background:
                    color: '#ffffff'

The sql loader and cache things just put the images in sql - not my intention, it was a requirement - dont ask.

In Twig (live component):

<img alt="{% trans %}Preview not available{% endtrans %}" src="{{ uploadedFilenames[key]['newFilename'] | imagine_filter('upload_thumb') }}">

redflo avatar Feb 25 '25 23:02 redflo

this functionality has been added 11 years ago according to git history. and its indeed not mentioned at all in the documentation.

i searched the code and found that the controller falls back to default image if the image is not loadable: https://github.com/liip/LiipImagineBundle/blob/3faccde327f91368e51d05ecad49a9cd915abd81/Controller/ImagineController.php#L165

the part about failing on nonexisting filter might or might not make sense, depends on whether you want to debug issues. and your case probably falls into the RuntimeException case?

if you want to improve on this, i would be happy to review a pull request. if you can identify some clear exception in your case, you could catch that along with the NotLoadableException. imho we could additionally / alternatively inject to the controller a debug flag (that defaults to true to keep BC) and the default configuration for that would be taken from the kernel.debug value. if debug is true, we keep throwing those exceptions. if debug is false, we would return the default url in all cases (as i think thats what you would want in production, plus maybe a warning or info level log message for the exception)

do you want to work on this? if you do it would also be nice to add a bit of documentation about the functionality.

dbu avatar Feb 26 '25 09:02 dbu