I want to resize category & product images for custom widget and I prefer Magento default function so I found following code to do so but I think it's for Magento 1
$imageObj = new Varien_Image ( $imageUrl );
$imageObj=$imageObj->constrainOnly ( true )
->keepAspectRatio ( true )
->keepFrame ( false )
->quality ( $quality )
->resize ( $width, $height )
->save ( $imageResized );
So I wish to know that does Magento 2 provide any function to resize,crop,scale any images including category,product etc.??
Best Answer
You can get all image related methods for category and product from helper file inside Magento_Catalog module core file,
vendor/magento/module-catalog/Helper/Image.php
List of methods are
resize()
List item
keepAspectRatio()
keepTransparency()
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
<img src="<?php echo $_imagehelper->init($_product, 'thumbnail')->keepAspectRatio(true)->resize('400', '400'); ?>" />
<img src="<?php echo $_imagehelper->init($_product, 'product_base_image')->keepAspectRatio(true)->resize('400', '400'); ?>" />
Inside Block file,
Call inside template file
$block->resize('img.jpg',300,300);