从Product Model Magento 2获取产品图片

时间:2016-06-09 17:00:58

标签: magento2

我正在抓取产品对象:

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

我试图用图片助手抓住产品图片,但它无法正常工作。我也试过了getImage()。任何人都可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:6)

尝试使用imageHelper,如下所示:

$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();

答案 1 :(得分:5)

从Magento 2的PHTML文件中的Product获取产品图像

$_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(); ?>