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
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
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.
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();
}
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