Search code examples
phptwigsymfony4

How to add image in the twig template?


It start getting bit annoying now. My images are inside public/images/ folder. I am trying to add an image to the my TemplatedEmail

In my twig template, I have tried everything I found in the document:

<img src="{{ asset('images/logos/coffee.jpg') }}" alt="Symfony!"/>
<img src="{{ absolute_url(asset('images/logos/coffee.jpg')) }}" alt="Symfony!"/>

<img src="{{asset('build/images/1.0e73ff35.jpg')}}" title="my image" alt="My image">

In the email I am getting like this:

Email

I have tried following:

<img src="{{ asset('../../public/images/Tux.png') }}">


<img src="{{ asset('images/basketball.jpg') }}" alt="first"/>


<img style="display: block;" src="{{ asset('images/Tux.png') }}" alt="...">

Can anybody please help me how to add image in the template ?

source

Thank You.


Solution

  • I had a similar issue before. You can either pass embeded method described in the documentation.

    ->embed(fopen(/path/to/image, 'r'), 'logo')
    <img src="cid:logo"> ... <img src="cid:new-cover-image">
    

    Another method I tried is:

    In your twig.yaml file, add your image path

    paths:
      '%kernel.project_dir%/public/images': images
    

    Then, in your twig file

    <img src="{{ email.image('@images/logo.png') }}" alt="Logo">