LiipImagineBundle icon indicating copy to clipboard operation
LiipImagineBundle copied to clipboard

CUstom data loader for using remote images

Open fdiedler opened this issue 2 years ago • 5 comments

Preconditions

  1. PHP 8.1
  2. Symfony 5.4
  3. Last version of Liips bundle
  4. GD library

Steps to reproduce

I try to create my own custom data loader to apply filters with remote images.

In the file services.yaml

app.imagine.cdn_data_loader:
        class: App\Domain\RemoteStreamLoader
        arguments:
            - '@liip_imagine'
        tags:
            - { name: 'liip_imagine.binary.loader', loader: 'cdn_data_loader' }

Then the file RemoteStreamLoader.php

use App\Domain;

use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
use Imagine\Image\ImagineInterface;

class RemoteStreamLoader implements LoaderInterface
{
    protected $imagine;

    public function __construct(ImagineInterface $imagine)
    {
        \dump($imagine); die();
        $this->imagine = $imagine;
    }

    public function find($path)
    {
        \dump("find"); die();
        // The http:// becomes http:/ in the url path due to how routing urls are converted
        // so we need to replace it by http:// in order to load the remote file
        $path = preg_replace('@\:/(\w)@', '://$1', $path);
        return $this->imagine->load(file_get_contents($path));
    }
}

Then my liips filter configuration :

liip_imagine:
    # valid drivers options include "gd" or "gmagick" or "imagick"
    driver: "gd"
    data_loader: "cdn_data_loader"

    # configure resolvers
    resolvers:
        # setup the default resolver
        default:
            # use the default web path
            web_path: ~

    filter_sets:
        # use the default cache configuration
        cache: ~

        # name our filter set "my_thumb_filter"
        thumbnail_filter:
            filters:
                # use and setup the "thumbnail" filter
                thumbnail:
                    # set the thumbnail size to "32x32" pixels
                    size: [32, 32]
                    # crop the input image, if required
                    mode: outbound

And then I use the filter in Twig view <img src="{{ ImageUrlPath | imagine_filter('thumbnail_filter') }}" class="rounded-circle" />

Expected result

The image does not appear and my custom data filter RemoteStreamLoader is not called

What is wrong ?

Thanks,

fdiedler avatar Mar 28 '22 16:03 fdiedler

hm, i would have expected this to work. but looking at https://github.com/liip/LiipImagineBundle/blob/1a04fac4dd9d318a3a42fedc81896d874b8a7e64/DependencyInjection/Configuration.php#L119 it seems to me that the default of the filter-set is always set and will win over the global default.

can you validate if your loader is used if you configure it in the thumbnail_filter? looking at the age of the commits, the global data_loader option was added later than the per-filter option in https://github.com/liip/LiipImagineBundle/commit/70df756d92987b9f31ab7b337b0cbe15739b1306 - but its weird, https://github.com/liip/LiipImagineBundle/blob/2.x/DependencyInjection/LiipImagineExtension.php#L122 has been there for a long time too.

if you confirm that this is the problem, i wonder how we should fix it. we could default the global loader to default and default to null the data_loader on the filter sets, and make sure the system then picks the global default if it is not overwritten in a filter set.

@peter-gribanov you changed something about the global data_loader in #1307 - do you happen to remember anything about the subject? does my take on this make sense or am i overlooking something?

dbu avatar Mar 29 '22 06:03 dbu

@dbu No, it does not work even if I put the data_loader in the thumbnail_filter section.

From this very old post (from mechanicalgux commented [on 27 Apr 2014]): https://github.com/liip/LiipImagineBundle/issues/328

In my service, I have removed the "liips_imagine.formats" argument because it seems that it does not exist anymore. I have this error :

You have requested a non-existent parameter "liip_imagine.formats". Did you mean this: "liip_imagine.loaders"?

Maybe this missing parameter is an issue ?

fdiedler avatar Mar 29 '22 09:03 fdiedler

@dbu no. I didn't change the data_loader. I just added its processing in the WebP section.

peter-gribanov avatar Mar 29 '22 16:03 peter-gribanov

Hello guys,

As @dbu mentioned, Liip\ImagineBundle always prioritizes the default loader if it has already been configured, which is my cas with Sylius.

The trick is to declare another time the default loader and use the allow_unresolvable_data_roots: true option.

It will force Liip to use your custom loader as the default won't be able to resolve images.

liip_imagine:
    loaders:
        default: # Safety: Liip always prioritizes default loader from Sylius
            filesystem:
                allow_unresolvable_data_roots: true
        google_cloud_storage:
            stream:
                wrapper: gaufrette://sylius_image/
    data_loader: google_cloud_storage

I hope this helps !

jeromin avatar Apr 03 '22 00:04 jeromin

Just chiming into this conversation briefly to mention that even if @jeromin's solution works, it must be doing so in an unintentional manner (I suspect by adding the config option it changes the order or priority of configuration loading). Regardless, the original defect described by @fdiedler remains.

I added the allow_unresolvable_data_roots: true option strictly to allow entering invalid or otherwise unresolvable local filesystem path(s) into the FileSystemLoader config without resulting in an exception being thrown (which is what would happen without the allow_unresolvable_data_roots: true option). This option, in and of itself, is not the solution to the described issue.

@dbu: If I remember correctly, @fdiedler's issue is a long-described problem you can find multiple issues involving different strategies to work around it. No one has taken the time to fix it (or wanted the responsibility of ensuring backward compatibility for all the hacks created to work around it over the years).

robfrawley avatar Apr 11 '22 19:04 robfrawley