I got this method in my RelatedProduct class that gets related product image URL

public function getProductImageUrl($product)
    return $this->_storeManager->getStore()
                ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();

and when I use it in my template

<img src=" <?= $product->getProductImageUrl($relatedProduct) ?> ">

the image is too large, it takes up whole screen.

How can I set the size of the image to base image size, preferably without having to add css styling for it? Can I do it in some .xml file?


1 Answer 1


Here you have the full answer : https://magento.stackexchange.com/a/122570/33619
You have to use the view.xml file in your theme to set your different sizes and them call them in your template by their code.

In your view.xml

<image id="your_image_id" type="small_image">

In your template

<?php echo $block->getImage($product, 'your_image_id')->toHtml(); ?>

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