0

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
0

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.

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.