3

Hello friends i want to fetch image path like var/www/magento/pub/media i wand to display image in admin grid i am using rendarar to get image image path by passing Product id. How this can be achieved ? please explain

thanks Adarsh Shukla

Himanshu
  • 1,761
  • 17
  • 34
Adarsh Shukla
  • 573
  • 1
  • 7
  • 25

3 Answers3

4

Add this below code in your file :

protected $_imageHelper;
protected $_productRepositoryFactory;
public function __construct(
    .........................
    \Magento\Catalog\Helper\Image $imageHelper,
    \Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory,
    .........................
)
{
    .........................
    $this->_imageHelper = $imageHelper;
    $this->_productRepositoryFactory = $productRepositoryFactory;
    .........................
}

public function yourFunction()
{
    $productID = 1;
    $product = $this->_productRepositoryFactory->create()->getById($productID);
    echo $this->_imageHelper->init($product,'image')->setImageFile($product->getImage())->getUrl();
}
Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96
2

You can get product image path from below code

    $product_id=20;  //enter product id here

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

    $product       = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

    $store         = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();

    $imageUrl      = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();

    $mediaUrl      =      $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

i Hope it help

Balwant Singh
  • 1,270
  • 2
  • 12
  • 28
  • 2
    sir, Thanks for reply but this gives url i want path like var/www/magento/pub/media/catalog – Adarsh Shukla Nov 20 '18 at 12:15
  • @AdarshShukla you mean media path? – Balwant Singh Nov 20 '18 at 12:20
  • yes i wand to display image in admin grid i am using rendarar to get image – Adarsh Shukla Nov 20 '18 at 12:22
  • okay then use below code for media url

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

    – Balwant Singh Nov 20 '18 at 12:25
  • @AdarshShukla check my updated code . $mediaUrl gives the path what you want – Balwant Singh Nov 20 '18 at 12:28
  • code gives o/p as following i wanted like var/www/magento http://192.168.2.215/mtheme/pub/media/catalog/product/m/b/mb01-blue-0.jpg – Adarsh Shukla Nov 20 '18 at 12:32
  • @AdarshShukla I think you asked diffrent question and you wanted diffrent things.

    Try this once.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $mediaUrl = $objectManager->get('Magento\Framework\Filesystem\DirectoryList'); $imageUrl = $store->getPath('media');

    – Balwant Singh Nov 20 '18 at 12:39
2

To get media absolute path you can try below code.

public function __construct(
\Magento\Framework\Filesystem\DirectoryList $directoryList

) {
    $this->directory = $directoryList;
}
public function getMediaAbsoluteUrl() {
   $mediaAbsoluteUrl = $this->directory->getPath('media');
}

I hope it'll work for you.

Ramkishan Suthar
  • 3,972
  • 5
  • 32
  • 56