I am using magneto2
and in controller file I try to call getImageUrl
function but unable to get Image url.
Please let me know how to get products image url in controller.
In console i an getting
500 request error
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI am using magneto2
and in controller file I try to call getImageUrl
function but unable to get Image url.
Please let me know how to get products image url in controller.
In console i an getting
500 request error
The way you can get image file in controller or in any class, you have to inject \Magento\Catalog\Api\ProductRepositoryInterfaceFactory
dependency into constructor of that class first.Like following
protected $_productRepositoryFactory;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory
) {
$this->_productRepositoryFactory = $productRepositoryFactory;
}
Now you can get images like following:
$product = $this->_productRepositoryFactory->create()->getById($item->getProductId());
$product->getData('image');
$product->getData('thumbnail');
$product->getData('small_image');
You will get images dir path you can append base media url before it to render.