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

up vote 1 down vote accepted

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.

  • Will This give me product image path by product id ? – Adarsh Shukla Nov 20 at 12:23
  • it will written absolute path of media directory as per you mentioned in comments. – Ramkishan Suthar Nov 20 at 12:25
  • is my aim is to get products image by passing product id i am trying to display that image to admin grid – Adarsh Shukla Nov 20 at 12:27
  • I am not able to get you, like why you need absolute url for this. If you just need to display product image then link which I have share in starting will work for you. – Ramkishan Suthar Nov 20 at 12:39
  • You just need to append $product->getImage() after $mediaUrl – Ramkishan Suthar Nov 20 at 12:40

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();
}
  • sir, Thanks for reply but this gives url i want path like var/www/magento/pub/media/catalog – Adarsh Shukla Nov 20 at 12:14
  • i wand to display image in admin grid i am using rendarar to get image – Adarsh Shukla Nov 20 at 12:21

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

  • 1
    sir, Thanks for reply but this gives url i want path like var/www/magento/pub/media/catalog – Adarsh Shukla Nov 20 at 12:15
  • @AdarshShukla you mean media path? – Balwant Singh Nov 20 at 12:20
  • yes i wand to display image in admin grid i am using rendarar to get image – Adarsh Shukla Nov 20 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 at 12:25
  • @AdarshShukla check my updated code . $mediaUrl gives the path what you want – Balwant Singh Nov 20 at 12:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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