1

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

1
  • +1 for answer acceptance – Shoaib Munir Dec 18 '20 at 16:48
1

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
5
  • Man i tried but returns blank page...I tried your code in product page! – Jackom Dec 18 '20 at 12:24
  • check error log, you are probably getting an error with implementation – Shoaib Munir Dec 18 '20 at 12:43
  • Notice: Undefined property: Magento\Catalog\Block\Product\View\Interceptor::$imageHelper – Jackom Dec 18 '20 at 13:06
  • 1
    I found the solution man! I added: $imagehelper = $this-> helper('Magento\Catalog\Helper\Image'); So i replaced $this->imageHelper in foreach with $imagehelper and works! – Jackom Dec 18 '20 at 13:31
  • Great, good to hear that :) – Shoaib Munir Dec 18 '20 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.