4

I'm trying to add an image to my first module can someone tell me how I cando this I am new to Magento

this is phtml file

<h2>Hello world</h2>

module is in

app/code/Test/Helloworld

enter image description here

This is my directory structure of Helloworld Module

7
  • Let me know if you have any issue Aug 19, 2017 at 16:26
  • I add images like above but its not working sir. i cleared my cache but not working
    – Hemal
    Aug 19, 2017 at 16:34
  • What is the error or what is display in image URL? Aug 19, 2017 at 16:48
  • nothing show just text
    – Hemal
    Aug 19, 2017 at 16:53
  • is there any other solution
    – Hemal
    Aug 19, 2017 at 17:19

4 Answers 4

5

For example your image at:

app/code/Vendor/Module/view/frontend/web/images/demo.jpg

Use $block->getViewFileUrl() function to get image url in phtml file like this

echo $block->getViewFileUrl('Vendor_Module::images/demo.jpg'); 
0
1

Use Module image : $block->getViewFileUrl('Vendor_Module::images/test.jpg');

Use Theme image : $block->getViewFileUrl('images/test.jpg');

0

If you using phtml file add an image, CSS and js use $block->getViewFileUrl() and if you working on block use this{{view url="images/IMG_7300.JPG"}}

0

in case if u have enabled opcache in your php.ini u will not see immediately the result. Also the images from the module are not added into pub/static folder. therefore to clear opcache do:

systemctl restart php<<version.minor>>-fpm i.e. systemctl restart php7.4-fpm

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.