get image url magento 2 code example
Example 1: magento 2 get product image
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\Product;
class myClass extends Template
{
public function getProductImage(Product $product){
$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
$imageBuilder = $objectManager->create(\Magento\Catalog\Block\Product\ImageBuilder::class);
return $imageBuilder->create($product, 'category_page_grid');
}
}
<?=$block->getProductImage($product)?>
Example 2: magento 2 get product image
$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageUrl = $helperImport->init($product, 'product_page_image_small')
->setImageFile($product->getSmallImage())
->resize(380)
->getUrl();
echo $imageUrl;