Question

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

Was it helpful?

Solution

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

  1. category_page_grid is used to get image which will be equal to the size in product list page
  2. product_page_image_large is used to get large size image
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top