I'm trying to link to pub/media product images from inside an extension's .phtml so I can reuse them rather than duplicate them inside the extension. I tried...

<img src="<?php echo $block->getUrl('pub/media/catalog/product/t/e/')?>test.jpg"/>

For some reason it strips the "e/" when I view the source...

<img src="http://dev:8888/pub/media/catalog/product/t/test.jpg">

If I move test.jpg to "product/t" it works, but I need to access them from the correct location. I'm currently using Magento 2.1.9 but will be upgrading to 2.2 soon.

  • Are you fetching product image dynamically? I mean to say from product object? – Prashant Valanda Apr 8 '18 at 9:41
  • No. I just want to link to images from within the .phtml as easily as I could from within the module. I found this as a working solution <img src="<?php echo $block->getUrl("pub/media/myfolder/")?>image.png"/> but in my case the last folder is stripped. – SilkSpin Apr 8 '18 at 10:00

Use below code:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')

<img src="<?php echo $mediaUrl.'myfolder/image.png';?>"/>

Above is not standard way as per magento standard use below code above code just for your quick feasibility test purpose

In your respected block of phtml file add below code:

protected $_storeManager;

public function __construct( 
    \Magento\Store\Model\StoreManagerInterface $storeManager,
) {
    $this->_storeManager = $storeManager;

public function getMediaUrl()
    $mediaUrl = $this->_storeManager
    return $mediaUrl;

add below code to your phtml file:

<?php $mediUrl = $block->getMediaUrl(); ?>
<img src="<?php echo $mediUrl.'myfolder/image.png';?>"/>
  • The top example works great, thanks. The 2nd example is returning HTTP ERROR 500. Could you also please tell me briefly the reason why all this is needed for something as simple as getting the URL of the media folder to help me understand. It seems like overkill. – SilkSpin Apr 8 '18 at 10:41
  • can you please add your block code in question – Prashant Valanda Apr 8 '18 at 10:47
  • Sorry, have I misunderstood this line "In your respected block of phtml file add below code"? I've included all the code at the top of my main.phtml file. Should the "protected $_storeManager;" code go somewhere else? The 1st example worked fine in the .phtml. – SilkSpin Apr 8 '18 at 11:00
  • yes that code you need to add in your block file – Prashant Valanda Apr 8 '18 at 11:02
  • I've just created Block/Display.php and put your code in. Then in view/frontend/layout I've added a new XML file. Inside the referenceContainer I've added <block class="Vendor\Module\Block\Display" name="images_from_media" template="Vendor_Module::main.phtml" />. On the page where the image should appear the $mediUrl part of the path is missing. – SilkSpin Apr 8 '18 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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