Magento – Magento 2 : How to get product get images for thumbnail

magento2product-imagesthumbnail

enter image description here

Friend please help me how to get product images and thumbnail images.

Best Answer

You need to inject the ImageBuilder class in our block :

protected $_imageBuilder;

public function __construct(
    ........
    \Magento\Catalog\Block\Product\ImageBuilder $_imageBuilder
    ........
){
    ........ 
    $this->_imageBuilder=$_imageBuilder;
    ........
}

public function getImage($product, $imageId, $attributes = [])
{
    return $this->_imageBuilder->setProduct($product)
            ->setImageId($imageId)
            ->setAttributes($attributes)
            ->create();
}

=> This is default image type :

$imageType = 'product_base_image';  //For getting the base image
$imageType = 'product_small_image';  //For getting the small image
$imageType = 'product_thumbnail_image';   //For getting the thumbnail image

Now, get thumbnail image in phtml file :

<?php
    $imageType = 'product_thumbnail_image'; //for thumbnail image
    $image = $block->getImage($_item, $imageType);
?>

<img src = "<?php echo $image->getImageUrl(); ?>" />

Hope, It may be helpful for you !!

Related Topic