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

1.85K 浏览开发笔记

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
【腾讯云】境外1核2G服务器低至2折,半价续费券限量免费领取!
【腾讯云】618采购季来袭!爆款云服务器18元起,参与活动享多重好礼