0

I am trying to get URL of small_image.

Magento 2 offers a helper helper('Magento\Catalog\Helper\Image').

I used this code to load small_image

<?php echo $this->helper('Magento\Catalog\Helper\Image')->init($block->getProductForThumbnail(), 'small_image')
        ->setImageFile($block->getProductForThumbnail()->getImage())
        ->resize(165, 165)->getUrl(); ?>

But this code throws me this error.

 Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Model\Product\Image::loadImageInfoFromCache() must be of the type string, null given, called in /Users/suman/the-webster/vendor/magento/module-catalog/Model/Product/Image.php on line 612 and defined in /Users/suman/the-webster/vendor/magento/module-catalog/Model/Product/Image.php on line 892

How to get small_image

1 Answer 1

1

So remembering in m2 you can set image dimensions in xml

app/design/frontend/Magento/luma/etc/view.xml file.

<image id="category_page_grid" type="small_image">
    <width>240</width>
    <height>300</height>
</image>
<image id="category_page_list" type="small_image">
    <width>240</width>
    <height>300</height>
</image>

You can use below code.

//$image = 'category_page_grid' or 'category_page_list';
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

$productImage = $_imagehelper->init($_product, $image)->resize(165)->getUrl();

    <img src="<?php echo $productImage; ?>" />

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.