How to get Magento 2 Product’s Image URL Programmatically

How can I retrieve Magento 2 product’s image URL from published/cache frontend.

After get Magento 2 product’s image url, you need to use “\Magento\Catalog\Helper\Image” to generate a public image url.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get(‘\Magento\Catalog\Helper\Image’);
$registry = $objectManager->get(‘\Magento\Framework\Registry’);
$product = $registry->registry(‘current_product’);
$imageUrl = $helperImport->init($product, ‘product_page_image_small’) ->setImageFile($product->getSmallImage())->getUrl();

Leave a Reply

avatar
  Subscribe  
Notify of