Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get original size image url in magento 2.0

I am pretty new in Magento 2.0 and I have been struggling to find out solution. My first question, which I didn't figure out yet, how to reach specific function? Beacuse I noticed that many people use those in Magento 1.+:

Mage::helper('cms/page')->

or

Mage::getModel('catalog/product_media_config')  

(e.g Get original image url Magento (1.6.1.0))

but I can't use them in Magento 2.0. If that kind of using is not available in last version anymore, what can I use it as an alternative method to reach functions?

As for another question, I am unable to get original sized image in grid.phtml (catalog listing). Here is how to get images:

<?php 
echo $block->getImage($_item, $image)->getImageUrl(); 
echo $block->getImage($_item, $image)->getWidth(); 
echo $block->getImage($_item, $image)->getHeight();
?>

And the result like that:

http://192.168.1.4/magento/pub/media/catalog/product/cache/1/small_image/240x300/beff4985b56e3afdbeabfc89641a4582/t/h/thumbnail_1.jpg240300

As I mentioned above, I want to get original sized image url instead of small_image. I hope I explained my problems. If anyone has any idea, please let me know it. Thank you!

like image 840
Semih Gokceoglu Avatar asked Dec 18 '25 20:12

Semih Gokceoglu


2 Answers

Use the below function to get the Media base url for a particular store:

function getMediaBaseUrl() {

$om = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $om->get('Magento\Store\Model\StoreManagerInterface');

$currentStore = $storeManager->getStore();
return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}

Using getMediaBaseUrl() function we can find image url in Magento2:

echo $block->getMediaBaseUrl() .'catalog/product'. $_product->getImage();
like image 150
VIPIN A ROY Avatar answered Dec 20 '25 17:12

VIPIN A ROY


1) Quick Solution by directly writing media directory path

echo $block->getUrl().'pub/media/catalog/product' . $_product->getImage();

2) Getting media directory path using StoreManager class object

namespace YourNamespace\YourModule\Block;
class YourModule extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;

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

    public function getMediaBaseUrl()
    {
        return $this->_storeManager
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    }
}

And then in your module's template (.phtml) file, you can write as:

echo $block->getMediaBaseUrl() . $_product->getImage();
like image 41
Mukesh Chapagain Avatar answered Dec 20 '25 16:12

Mukesh Chapagain