【问题标题】:Get product image from Product Model Magento 2从产品模型 Magento 2 获取产品图像
【发布时间】:2016-06-09 17:00:58
【问题描述】:

我正在抓取一个产品对象:

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

我尝试使用图像助手抓取产品图像,但它不起作用。我也尝试过 getImage() 。任何机构都可以提供帮助吗?谢谢!

【问题讨论】:

    标签: magento2


    【解决方案1】:

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

    【讨论】:

    • 您将 imageID 值作为“product_thumbnail_image”传递。我还在 magento 中看到了“recently_viewed_products_grid_content_widget”。你能告诉我有多少种 imageID 吗?
    【解决方案2】:

    尝试使用 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();
    

    【讨论】:

    • 你不要使用$imagewidth$imageheight
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多