2

enter image description here

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

9
  • you need to get Thumbnail product URL from Product collection? Jul 16, 2018 at 19:19
  • so sir complete code given i have new developer so very problem understating. in magento code so please given on this code
    – pushpendra
    Jul 16, 2018 at 19:23
  • I can not understand your question. Can you please specify where you want to show that thumbnail image? Jul 16, 2018 at 19:25
  • wait i can explain
    – pushpendra
    Jul 16, 2018 at 19:25
  • Yes. please explain in details. Jul 16, 2018 at 19:27

1 Answer 1

7

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 !!

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.