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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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(); ?> |
1 | <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