M2如何获取产品图片(原图或者resize裁剪)

3.22K 浏览开发笔记

M2如何获取产品图片(原图或者resize裁剪)

1,只获取原始图片

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); $store = $objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore(); $imageUrl = $store->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();

2,resize裁剪图片

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); $_productImageHelper = $objectManager->get('MagentoCatalogHelperImage'); $resizedImage = $_productImageHelper->init($product, 'product_base_image')                                            ->constrainOnly(TRUE)                                            ->keepAspectRatio(TRUE)                                            ->keepTransparency(TRUE)                                            ->keepFrame(FALSE)                                            ->resize($width, $height);

product_base_image 代表是主图

product_small_image 代表是小图

product_thumbnail_image 代表是缩略图

0