{"id":11752,"date":"2020-06-12T11:51:54","date_gmt":"2020-06-12T06:21:54","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11752"},"modified":"2020-10-09T19:00:17","modified_gmt":"2020-10-09T13:30:17","slug":"magento-2-resize-product-image","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-resize-product-image\/","title":{"rendered":"Magento 2 &#8211; How to Resize Product Image"},"content":{"rendered":"<p>In this post I am going to explain how to resize product image in Magento 2.<\/p>\n<p>Using the below code snippet you can resize the product image in the template (.phtml) file.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$productId = '12';\r\n$_product = $block-&gt;getProductById($productId);\r\n\r\n$imageHelper = $this-&gt;helper('Magento\\Catalog\\Helper\\Image');\r\n\r\n$imgWidth = 350;\r\n$imgHeight = 400;\r\n$imageId = 'category_page_grid';\r\n\r\n$productImage = $imageHelper-&gt;init($_product, $imageId)\r\n\t\t\t\t-&gt;constrainOnly(TRUE)\r\n\t\t\t\t-&gt;keepAspectRatio(TRUE)\r\n\t\t\t\t-&gt;keepTransparency(TRUE)\r\n\t\t\t\t-&gt;keepFrame(TRUE)\r\n\t\t\t\t-&gt;resize($imgWidth, $imgHeight);\r\n\r\n$productImageUrl = $productImage-&gt;getUrl();\r\n\r\n?&gt;<\/pre>\n<pre class=\"lang:default decode:true \">&lt;img class=\"product-image-photo\" src=\"&lt;?php echo $productImageUrl; ?&gt;\" alt=\"&lt;?php echo $_product-&gt;getTitle();?&gt;\" \/&gt;<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1779042074\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>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. &lt;?php $productId = &#8217;12&#8217;; $_product = $block-&gt;getProductById($productId); $imageHelper = $this-&gt;helper(&#8216;Magento\\Catalog\\Helper\\Image&#8217;); $imgWidth = 350; $imgHeight = 400; $imageId = &#8216;category_page_grid&#8217;; $productImage = $imageHelper-&gt;init($_product, $imageId)&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-resize-product-image\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11815,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11752"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11752"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11752\/revisions"}],"predecessor-version":[{"id":11765,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11752\/revisions\/11765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11815"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}