Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get product image from Product Model Magento 2

Tags:

magento2

I am grabbing a product object with:

    $productId = 9184;
    $objectManagerProd = \Magento\Framework \App\ObjectManager::getInstance(); 
    $currentproduct = $objectManagerProd->create('Magento\Catalog\Model\Product')->load($productId);

I have tried to grab the product image with the image helper but it is not working. I have tried getImage() as well. Any body can help? Thanks!

like image 249
Tyler Nichol Avatar asked Dec 05 '22 00:12

Tyler Nichol


2 Answers

Get product image from Product in PHTML file for Magento 2

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper  = $_objectManager->get('\Magento\Catalog\Helper\Image');
<?php $image_url = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl(); ?>
like image 110
Tyler Nichol Avatar answered Jan 28 '23 17:01

Tyler Nichol


try using imageHelper like following :

$objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
$helperImport   = $objectManager->get('\Magento\Catalog\Helper\Image');

    $imageUrl = $helperImport('Magento\Catalog\Helper\Image')
        ->init($currentproduct, 'product_page_image_large')
        ->setImageFile($currentproduct->getFile())
        ->getUrl();
like image 45
Emizen Tech Avatar answered Jan 28 '23 17:01

Emizen Tech