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

6

3 Answers 3

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.

5
  • Will This give me product image path by product id ? Nov 20, 2018 at 12:23
  • it will written absolute path of media directory as per you mentioned in comments. Nov 20, 2018 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 Nov 20, 2018 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. Nov 20, 2018 at 12:39
  • You just need to append $product->getImage() after $mediaUrl Nov 20, 2018 at 12:40
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();
}
2
  • 1
    sir, Thanks for reply but this gives url i want path like var/www/magento/pub/media/catalog Nov 20, 2018 at 12:14
  • i wand to display image in admin grid i am using rendarar to get image Nov 20, 2018 at 12:21
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

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

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.