liip / LiipImagineBundle

Symfony Bundle to assist in image manipulation using the imagine library
http://liip.ch
MIT License
1.66k stars 378 forks source link

Symfony 4 configuration #1074

Closed tacman closed 2 years ago

tacman commented 6 years ago
Q A
Bug Report? no
Feature Request? no
BC Break Report? no
Imagine Bundle Version 2.0-dev

I'm sure this is something simple, but I'm stuck with the Symfony4 setup. I'm running with bin/console server:run, and I can access my image (http://127.0.0.1:8000/photos/Facebook-temp/ZACA5563-21.thumb.jpg) , but the thumbnail doesn't work (http://127.0.0.1:8000/media/cache/resolve/squared_thumbnail/photos/Facebook-temp/ZACA5563-21.thumb.jpg)

and instead gives the error:

Source image for path "photos/Facebook-temp/ZACA5563-21.thumb.jpg" could not be found

I'm sure it's something in the setup. Symfony4 uses /public instead of /web, and I think root_dir has been changed to project_dir. My imagine.yaml file looks like this:

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

     resolvers:
       profile_photos:
          web_path:
            web_root: "%kernel.project_dir%/public"
            cache_prefix: "media/cache"

     # define your filter sets under this option
     filter_sets:

         # an example thumbnail transformation definition
         # https://symfony.com/doc/current/bundles/LiipImagineBundle/basic-usage.html#create-thumbnails
         squared_thumbnail:

             # set your image quality defaults
             jpeg_quality:          85

I keep playing around with different paths and settings, but can't figure out what I'm doing wrong.

broiniac commented 6 years ago

I believe you have found a bug. I managed to reproduce your issue on Symfony 3.4. The problem is resolvers node using web_path with web_root and cache_prefix - they do not change even when you set them to be something else. You can see it using debug:config LiipImagineBundle command.

As a result I'm using public-based directory for everything but cached thumbnails are still generated in web-based directory. Simplest temporary fix will be symlinking web and public directories together.

robfrawley commented 6 years ago

@broiniac I don't understand why you are running into this. Please see this video of this bundle configuration on both Symfony 3.4 and 4.0 with both the default web_root and cache_prefix and custom ones. No issues configuring them whatsoever, and web is correctly used for Symfony 3.4 while public is used for 4.0. https://youtu.be/iuOUiS8NFuQ

robfrawley commented 6 years ago

@broiniac Can you clarify what you are doing to cause the issue to occur? I cannot duplicate under any circumstances. Thanks!

broiniac commented 6 years ago

@robfrawley Maybe this is the problem, since I'm using public on sf3.4. I can't change web_root parameter in my app configuration - it's omitted.

robfrawley commented 6 years ago

@broiniac Did you view the video I linked above? You can manually set the liip_imagine.resolvers.web_path.web_root configuration option to public instead of web on Symfony 3.4. If it is being "ignored" you must be setting it in two different locations, where one is overwriting the other (or you've found a bug I cannot reproduce).

Can you create a minimal fork of the Symfony standard edition with this bundle showcasing the bug you are describing?

broiniac commented 6 years ago

@robfrawley Ok, I've just noticed that I'm using 1.9.1 version. I will update to 2.* and try again. I did see video after your last comment - thank you for creating it. I bet it was time consuming. Sorry for that.

EDIT: Just switched to 2.0 (dev) - problem still occur.

Here is my config:

liip_imagine:
    loaders:
        default:
            filesystem:
                bundle_resources:
                    enabled: true
                data_root: "%kernel.project_dir%/public"
    resolvers:
        default:
            web_path:
                web_root: "%kernel.project_dir%/public" # not working for me
                cache_prefix: "media/cache"

default_image: /assets/images/empty.png

php bin/console d:config LiipImagineBundle:

Current configuration for "LiipImagineBundle"
=============================================

liip_imagine:
    loaders:
        default:
            filesystem:
                bundle_resources:
                    enabled: true
                    access_control_type: blacklist
                    access_control_list: {  }
                data_root:
                    - /home/broiniac/projects/company_name/portal/public
                locator: filesystem_insecure
    resolvers:
        default:
            web_path:
                web_root: /home/broiniac/projects/company_name/portal/app/../web
                cache_prefix: media/cache
    default_image: /assets/images/empty_gallery.png
    driver: gd
    cache: default
    cache_base_path: ''
    data_loader: default
    controller:
        filter_action: 'liip_imagine.controller:filterAction'
        filter_runtime_action: 'liip_imagine.controller:filterRuntimeAction'
        redirect_response_code: 301
    filter_sets: {  }
    enqueue: false

PHP 7.2.4 Sf 3.4.7

EDIT2: Ok, It is project-specific problem for sure. I've just created clean project on Sf3.4 - no issue there. Clean 4.0 version is issueless as well. New project created using Symfony Installer for Sf3.3 - also no issue. I'm working on rather old app created in times of Sf2.4 and upgraded since then. Maybe it's the problem - some forgotten option in forgotten file on the other side of the project. Dunno :(

Thank you so much for all the time you've use on this issue! Especially for creating video - I bet it was time consuming. I promise to get back to this thread and reply after issue will be resolved in my project.

TinghuiWang commented 6 years ago

What actually fixed the issue is the data_root in lisp_imagine.loaders.default.filesystem. The liip_imagine.resolvers.default.web_path does not resolves the error reported in command line about FileSystemLocator.php.

ChuGui commented 6 years ago

Hi everyone. I got kind of the same issue. I'm using SF4 with LiipImagineBundle, trying to make it works. PHP 7.1.9, "liip/imagine-bundle": "^2.0", I've tried many config of imagine.yaml in config/packages. But it seems that liip_imagine doesn't create any cache file anywhere in my project. What am I doing wrong? Here is my config:

`liip_imagine:

valid drivers options include "gd" or "gmagick" or "imagick"

driver: "gd"

resolvers:
    my_resolver:
        web_path:
            # use %kernel.project_dir%/web for Symfony prior to 4.0.0
            web_root: "%kernel.project_dir%/public"
            cache_prefix: "media/cache"
# define your filter sets under this option
filter_sets:

    my_thumb:
        cache: my_resolver
        # adjust the image quality to 75%
        quality: 75

        filters:

            # create a thumbnail: set size to 120x90 and use the "outbound" mode
            # to crop the image when the size ratio of the input differs
            thumbnail: { size: [120, 90] }

loaders:
    default:
        filesystem:
            bundle_resources:
                enabled: true
            data_root: "img/uploads" #where my originals files are stored`

This is the way I called the filter in twig:

src="{{ asset('img/uploads/'~ photo.image)| imagine_filter('my_thumb') }}"

My originals files are strored in public/img/uploads.

and this is how it's called in my html: src="http://localhost/my_project/public/media/cache/resolve/my_thumb/my_project/public/img/uploads/5b0d1617c3d2c816294354.jpg"

Puerco-Potter commented 5 years ago

Hi everyone. I got kind of the same issue. I'm using SF4 with LiipImagineBundle, trying to make it works. PHP 7.1.9, "liip/imagine-bundle": "^2.0", I've tried many config of imagine.yaml in config/packages. But it seems that liip_imagine doesn't create any cache file anywhere in my project. What am I doing wrong? Here is my config:

`liip_imagine:

valid drivers options include "gd" or "gmagick" or "imagick"

driver: "gd"

resolvers:
    my_resolver:
        web_path:
            # use %kernel.project_dir%/web for Symfony prior to 4.0.0
            web_root: "%kernel.project_dir%/public"
            cache_prefix: "media/cache"
# define your filter sets under this option
filter_sets:

    my_thumb:
        cache: my_resolver
        # adjust the image quality to 75%
        quality: 75

        filters:

            # create a thumbnail: set size to 120x90 and use the "outbound" mode
            # to crop the image when the size ratio of the input differs
            thumbnail: { size: [120, 90] }

loaders:
    default:
        filesystem:
            bundle_resources:
                enabled: true
            data_root: "img/uploads" #where my originals files are stored`

This is the way I called the filter in twig:

src="{{ asset('img/uploads/'~ photo.image)| imagine_filter('my_thumb') }}"

My originals files are strored in public/img/uploads.

and this is how it's called in my html: src="http://localhost/my_project/public/media/cache/resolve/my_thumb/my_project/public/img/uploads/5b0d1617c3d2c816294354.jpg"

I have the same problem, the file won't generate and the url looks the same

adrienlamotte commented 5 years ago

@ChuGui @Puerco-Potter if you follow the link of your non-generated image, for ex : http://localhost/my_project/public/media/cache/resolve/my_thumb/my_project/public/img/uploads/5b0d1617c3d2c816294354.jpg

Do you have any error message ?

Puerco-Potter commented 5 years ago

@ChuGui @Puerco-Potter if you follow the link of your non-generated image, for ex : http://localhost/my_project/public/media/cache/resolve/my_thumb/my_project/public/img/uploads/5b0d1617c3d2c816294354.jpg

Do you have any error message ?

I get the following error: NotFoundHttpException Source image for path "my_project/public/img/uploads/5b0d1617c3d2c816294354.jpg" could not be found

adrienlamotte commented 5 years ago

@Puerco-Potter what driver do you use (GD, Imagick, ...) ? In any case, check in your phpinfo() your driver's image type support. You need to have the support of PNG and I guess JPG at least.

Puerco-Potter commented 5 years ago

Before anything, I notice that If I go to the url : http://localhost/my_project/public/media/cache/resolve/my_thumb/img/uploads/5b0d1617c3d2c816294354.jpg The image is generated and stored and I can see it...

How I have to change my config so it won't add /my_proyect/public to the route?

broiniac commented 5 years ago

The image is generated and stored and I can see it...

How do you generate url to image? Are you using '...'|imagine_filter('filter_name')?

How I have to change my config so it won't add /my_proyect/public to the route?

That strictly depends on your webserver configuration (and probably system settings). It's out of the scope of this project probably, but here are some helpful links: Windows + Apache (XAMPP): https://backups.nl/geen-categorie/configuring-virtual-hosts-on-xampp-how-to-run-multiple-domains/ Linux + NginX: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Puerco-Potter commented 5 years ago

The image is generated and stored and I can see it...

How do you generate url to image? Are you using '...'|imagine_filter('filter_name')?

Yes, and pretty much the default configuration like it appears in the symfony documentation.

How I have to change my config so it won't add /my_proyect/public to the route?

Just to be clear: I talk about the second "/my_proyect/public". Url generated in the html: http://localhost/my_project/public/media/cache/resolve/my_thumb/my_project/public/img/uploads/image.jpg" Url that works: http://localhost/my_project/public/media/cache/resolve/my_thumb/img/uploads/image.jpg"

In any case, check in your phpinfo() your driver's image type support. You need to have the support of PNG and I guess JPG at least.

Don't know if still relevant but, GD, and it have those formats enabled...

Puerco-Potter commented 5 years ago

I finally resolved it changing the twig code from this: {{ asset('img/uploads/'~ photo.image)| imagine_filter('my_thumb') }} to this: src="{{ ('img/uploads/'~ photo.image)| imagine_filter('my_thumb') }}"

May be my own ignorance, but I put it here for any person with the same problem that ends here...

delirehberi commented 5 years ago

This issue at still occur when want to change resolvers.default.web_path.web_roout config in a Sylius project.

I try to find what should be caused this situation and I found this logic in DependencyInjection.php

$rootNode
            ->beforeNormalization()
                ->ifTrue(function ($v) {
                    return
                        empty($v['loaders']) ||
                        empty($v['loaders']['default']) ||
                        empty($v['resolvers']) ||
                        empty($v['resolvers']['default']);
                })
                ->then(function ($v) {
                    if (empty($v['loaders'])) {
                        $v['loaders'] = [];
                    }

                    if (false === is_array($v['loaders'])) {
                        throw new \LogicException('Loaders has to be array');
                    }

                    if (false === array_key_exists('default', $v['loaders'])) {
                        $v['loaders']['default'] = ['filesystem' => null];
                    }

                    if (empty($v['resolvers'])) {
                        $v['resolvers'] = [];
                    }
                    if (false === is_array($v['resolvers'])) {
                        throw new \LogicException('Resolvers has to be array');
                    }

                    if (false === array_key_exists('default', $v['resolvers'])) {
                        $v['resolvers']['default'] = ['web_path' => null];
                    }

                    return $v;
                })
            ->end();

I think something wrong in there, because this issue gone away if i add a resolver node to root node like that:

->arrayNode('resolvers')
                    ->children()
                        ->arrayNode('default')
                            ->children()
                            ->arrayNode('web_path')
                                ->children()
                                ->scalarNode('web_root')->end()
                                ->scalarNode('cache_prefix')->end()
                                ->end()
                            ->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()

but of course, I can't send a pull request because it is not an absolute solution and I wasn`t understood why used beforeNormalization method in here.

After that, my coworker tried to move the configuration to liipimagine.yaml from the _sylius.yaml and this issue went away.

I think this is caused by Symfony file loader sort related issue or not. Anyway, I was wanna share this thing. Maybe someone finds a real cause and fix that.