3

In a Symfony 5 project E-Mails are created using the Mailer components TemplatedEmail() function. In the used twig template ($templatedEmail->htmlTemplate(..)) an image is embedded using

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg') }}">

to include a user-specific image. This works fine. When trying to pass that image to liip filters by

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg') | imagine_filter('v_watermark') }}">

the image deosn't render anymore. I was thinking about the resolver settings to be applied to make liip able to read from the non-standard target path - but didn't find any sensible setting. Documentation didn't lead me to any helpfull information. For any hints on how to make liip work with Mailer embedded images would help me a lot.

EDIT (thanks @ArleighHix):

I also tried nesting the filter inside the call like

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg' | imagine_filter('v_watermark')) }}">

which resulted in the following error:

Unable to find template "@v_images/user-1https:/127.0.0.1:8000/media/cache/resolve/v_watermark/v.jpg" (looked into: [SENSIBLE_LOCAL_PATH_IN_PROJECT]).

I am unsure what exactly happens but the path seems to be concatenated weirdly...

user3440145
  • 793
  • 10
  • 34
  • Have you tried nesting the filter inside the call to `email.image`? Like `email.image( ('path')|imagine_filter('v_watermark') )` – Arleigh Hix Apr 22 '21 at 16:33
  • 1
    I did now - resulting in this error: Unable to find template "@v_images/user-1https:/127.0.0.1:8000/media/cache/resolve/v_watermark/v.jpg" (looked into: [SENSIBLE_LOCAL_PATH_IN_PROJECT]). I am unsure what exactly happens but the path seems to be concatenated weirdly... – user3440145 Apr 22 '21 at 16:53
  • Add exactly what you tried to the question along with the error it produced, I'll upvote maybe someone else knows. – Arleigh Hix Apr 22 '21 at 17:29
  • Why not trying to use ``asset()`` https://symfony.com/doc/current/reference/twig_reference.html#asset to get the image path ? Couple it with the ``imagine_filter`` https://github.com/liip/LiipImagineBundle#example – vincent PHILIPPE Apr 23 '21 at 09:12
  • @vincentPHILIPPE I think asset only returns public pathes. The image used here is from a non public path and therefore embedded in the email. I could be wrong regrding the public pathes only thing thought... – user3440145 Apr 23 '21 at 10:40
  • @user3440145 You're right ! So combine it with absolut_url https://symfony.com/doc/current/templates.html#templates-link-to-assets (watch "If you need absolute URLs for assets..."). See https://symfony.com/doc/current/reference/twig_reference.html#absolute-url documentation – vincent PHILIPPE Apr 23 '21 at 10:45
  • @vincentPHILIPPE Using absolute_url() on asset() doesn't change the need for the asset to be stored in a public place. – user3440145 Apr 23 '21 at 12:24

1 Answers1

1

I think you should use absolute_url() combine with asset().

Base on this doc

If you need absolute URLs for assets, use the absolute_url() Twig

<img src="{{ absolute_url(asset('images/logo.png')) }}" alt="Symfony!"/>

<link rel="shortcut icon" href="{{ absolute_url('favicon.png') }}">

Solution 1

I think this one should work :

<img src="{{ email.image((absolute_url(asset('@v_images')) ~ /user-'~user.id~'/v.jpg) | imagine_filter('v_watermark')) }}">

Solution 2

If it don't work, you should add a gloabl variable with base url.

twig.yaml

twig:
  globals:
    app_base_url: '127.0.0.1:8000' // Put your base url here

And use it as :

<img src="{{ email.image(app_base_url ~ asset('@v_images') ~ /user-'~user.id~'/v.jpg) | imagine_filter('v_watermark')) }}">

Of course as you'll publish your website on the web, the base_url would change. You may use a service parameter '%app.base_url%'

Define it in you configuration files :

services_dev.yaml

parameters:
   app.base_url: 'https://127.0.0.1:8000/'

services_prod.yaml

parameters:
   app.base_url: 'https://your.website.url/'

And then update your twig.yaml as

twig:
  globals:
    app_base_url: '%app.base_url%'
vincent PHILIPPE
  • 975
  • 11
  • 26
  • 1
    Thanks for the approach. However using absolute_url() on asset() doesn't change the need for the asset to be stored in a public place - which in my case is not an option. – user3440145 Apr 23 '21 at 11:22