0

Hello I would like to add a image to a phtml file. however it didn't get use to the magento code to get the image path

<img src="{{media url='/ca_flag.png'}}"/>

Somehow the image address was translate to something like this

www.domain.com/%7B%7Bmedia%20url='/ca_flag.png'%7D%7D

what should I use?

Thanks

2 Answers 2

7

For template files:

Images in Vendor/theme/Vendor_Module/web/images or app/code/Vendor/Module/view/frontend/web/images/

<img src="<?php echo $this->getViewFileUrl('Vendor_Module::images/image.png'); ?>" />

or if images are in Vendor/theme/web/images/

<img src="<?php echo $block->getViewFileUrl('images/image.png'); ?>" />

For CMS Static Blocks or Pages:

Images in Vendor/theme/Vendor_Module/web/images or app/code/Vendor/Module/view/frontend/web/images/

<img src="{{view url='Vendor_Module::images/image.png'}}"/>

or if images are in Vendor/theme/web/images/

<img src="{{view url='images/image.png'}}"/>
1
  • 1
    thanks! very informative
    – Paul Fan
    Commented Dec 13, 2019 at 7:28
2

Put your image inside app/design/frontend/Vendor/Theme/web/images and call it like -

<img src='<?php echo $this->getViewFileUrl('images/demo.png'); ?>' alt="Demo">
1
  • yes, your solution also works too, thanks!
    – Paul Fan
    Commented Dec 13, 2019 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.