In this post I am going to explain how to resize product image in Magento 2.
Using the below code snippet you can resize the product image in the template (.phtml) file.
<?php
$productId = '12';
$_product = $block->getProductById($productId);
$imageHelper = $this->helper('Magento\Catalog\Helper\Image');
$imgWidth = 350;
$imgHeight = 400;
$imageId = 'category_page_grid';
$productImage = $imageHelper->init($_product, $imageId)
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->keepFrame(TRUE)
->resize($imgWidth, $imgHeight);
$productImageUrl = $productImage->getUrl();
?><img class="product-image-photo" src="<?php echo $productImageUrl; ?>" alt="<?php echo $_product->getTitle();?>" />
Thats it. Enjoy Magento 2!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!



Leave a Reply