How to get image in phtml file and CMS block in magento 2

In this blog, we will learn how to get image in phtml and CMS blocks/pages.
We will get image from theme or from module/extension in Magento 2.

For template file (in phtml):

You need to put image file in following path:

Theme: app/design/frontend/[vendor]/[theme]/Vendor_Module/web/images/
OR
Module: app/code/[Vendor]/[Module]/view/frontend/web/images/

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

OR If images are in general theme Vendor/theme/web/images/ then use

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

For CMS Static Blocks or Pages:

You need to put image file in following path:
Theme: app/design/frontend/[vendor]/[theme]/Vendor_Module/web/images/
OR
Module: app/code/[Vendor]/[Module]/view/frontend/web/images/

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

OR If images are in general theme Vendor/theme/web/images/ then use

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

we hope this blog is useful for you. let me know in comment if you have any questions. thanks!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *