3

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

0

1 Answer 1

4

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, 2020 at 12:24
  • check error log, you are probably getting an error with implementation Dec 18, 2020 at 12:43
  • Notice: Undefined property: Magento\Catalog\Block\Product\View\Interceptor::$imageHelper
    – Jackom
    Dec 18, 2020 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, 2020 at 13:31
  • Great, good to hear that :) Dec 18, 2020 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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