VichUploaderBundle: Option 'imagine_pattern' generates a wrong link

Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 4.0.6
VichUploaderBundle version 1.8.2
LiipImagineBundle version 2.0.x-dev
SonataAdminBundle version 3.33.0

I am using VichUploaderBundle + LiipImagineBundle + SonataAdminBundle.

config/packages/vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mappings:
        user_img:
            uri_prefix:         /img/user
            upload_destination: "%kernel.root_dir%/../public/img/user"
            namer: Vich\UploaderBundle\Naming\HashNamer
            directory_namer: Vich\UploaderBundle\Naming\SubdirDirectoryNamer
            delete_on_update:   true
            delete_on_remove:   true

config/packages/imagine.yaml

liip_imagine:
    resolvers:
        default:
            web_path: ~

    filter_sets:
        cache: ~
        my_thumb:
            quality: 75
            filters:
                thumbnail: { size : [120, 90], mode : outbound }
                background: { size : [122, 92], position : center, color : '#eee' }

If I use this code somewhere in a template:

<img src="{{ vich_uploader_asset(object, 'imageFile') | imagine_filter('my_thumb') }}" />

it works perfectly and generates a correct link to thumbnail, like this:

http://example.com/media/cache/my_thumb/img/user/78/78c737520d6620f766d5a636da9afa13e89bf47f.jpg

However if I use the VichImageType with the option ‘imagine_pattern’ it generates a link without uri_prefix, like this:

http://example.com/media/cache/my_thumb/78/78c737520d6620f766d5a636da9afa13e89bf47f.jpg

It’s the same entity in both cases. I use the VichImageType in SonataAdminBundle, but since the official documentation says:

When defining fields in your admin classes you can use any of the standard Symfony field types and configure them as you would normally

it should make no difference.

I found the “problem”, which was located in the file Vich\UploaderBundle\Form\Type\VichImageType. If you change the line 64, then everything works fine:

before $path = $this->storage->resolvePath($object, $form->getName(), null, true);
after $path = $this->storage->resolveUri($object, $form->getName(), null, true);

Thanx

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 43 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@BlackWiCKED stay with 1.9

@c33s Also @murtukov said he doesn’t sure the issue was resolved.

Your example does not work because you specify different directories as data root for liip_imagine and vich_uploader. Specifying vich_uploader.mappings.stamp_image.upload_destination to %kernel.project_dir%/var/data/images should fix the issue. Also you can add %kernel.project_dir%/var/data/images/stamps to liip loader data root. Replacing resolvePath with resolveUri looks like a fix because you specified empty uri_prefix and added %kernel.project_dir%/public as liip loader data root, but this is a hack.

I am preparing a PR where old behavior is also worked but deprecated and is described why, please be patient

I’m busy in the near future, I’ll do later

After experiencing the same issue myself, I decided to revert changes done in 514425c. A new tag was released for branch 1.8, while master can be used as ^1.10@dev (for now)

@garak @ossinkine @c33s

Hi guys, it’s been a long time since I opened this issue. Sorry for a poor feedback from my side, I didn’t have enough time for this issue. Now I took time to read all the messages above and all related issues.

What is the current state? Well if I now create a new Symfony4 project and install following bundles: VichUploaderBundle 1.8.2 LiipImagineBundle 2.0.x-dev I would be able to use resolvers in Twig templates: {{ vich_uploader_asset(user, 'imageFile') | imagine_filter('my_thumb') }}

as well as in controllers/services:

$relativePath = $this->vichService->asset($user, 'imageFile');
$link = $this->liip->getBrowserPath($relativePath, 'my_thumb');

Both would generate the same correct link: http://example.com/media/cache/my_thumb/img/user/78/78c737520d6620f766d5a636da9afa13e89bf47f.jpg

Here is my configuration:

vich_uploader:
    db_driver: orm

    mappings:
        user_img:
            uri_prefix:         /img/user
            upload_destination: '%kernel.project_dir%/public/img/user'
            namer:              Vich\UploaderBundle\Naming\HashNamer
            directory_namer:    Vich\UploaderBundle\Naming\SubdirDirectoryNamer

liip_imagine:
    resolvers:
        default:
            web_path: ~

    filter_sets:
        cache: ~
        my_thumb:
            quality: 75
            filters:
                thumbnail: { size: [255, 400], mode: inset, allow_upscale: false }

Everything seems to work fine, however, if i try to use VichImageType in my Sonata Admin classes, it generates links without the prefix. Example:

$formMapper
    ->with('Avatar')
        ->add('imageFile', VichImageType::class, [
            'required' => false,
            'label' => 'Avatar',
            'download_uri' => false,
            'imagine_pattern' => 'my_thumb',
        ])
    ->end();

The code above would generate the following link: http://example.com/media/cache/my_thumb/78/78c737520d6620f766d5a636da9afa13e89bf47f.jpg As you can see the /img/user part isn’t there

Conclusion: For me the problem remains the same: I can’t use VichImageType, because it generates links without uri_prefix because it uses resolvePath instead of resolveUri

So if you revert 514425c it would solve the problem and I would be able to generate links in templates, controllers and sonata classes.

Thanx.

i will update the ticket with infos & create a PR as soon as i can. my current workload doesn’t allow it yet. i assume it will be end of the month.

@murtukov vich_uploader_asset(object, 'imageFile') returns public path for browser, but imagine_filter('my_thumb') receives local relative path in your data root. So vich_uploader_asset(object, 'imageFile') | imagine_filter('my_thumb') is not make sense and http://example.com/media/cache/my_thumb/img/user/78/78c737520d6620f766d5a636da9afa13e89bf47f.jpg looks right.