Magento 2 - get cache url of media gallery images of product
-
15-04-2021 - |
質問
This is my code for get cache url of images:
$images = $_product->getMediaGalleryImages();
foreach($images as $child){
echo '"'.$child->getUrl().'"';
}
Result expected: pub/media/catalog/product/cache/1cd59219f95f9c415e6b76e445689a097/J/K/JK791735.jpg
Result obtained: pub/media/catalog/product/J/K/JK791735.jpg
How can i do? Thanks
解決
You can get cached images from a product object with this:
foreach($product->getMediaGalleryImages() as $k=>$image){
$thumbnailImage = $this->imageHelper->init($product, 'category_page_grid')
->setImageFile($image->getFile())
->getUrl();
$largeImage = $this->imageHelper->init($product, 'product_page_image_large')
->setImageFile($image->getFile())
->getUrl();
}
where $this->imageHelper
is the instance of \Magento\Catalog\Helper\Image
category_page_grid
is used to get image which will be equal to the size in product list pageproduct_page_image_large
is used to get large size image
所属していません magento.stackexchange