0

This question already has an answer here:

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?

marked as duplicate by Khoa TruongDinh, Rakesh Jesadiya, Marius magento2 Feb 6 '17 at 7:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

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">
    <width>152</width>
    <height>190</height>
</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.