How is the right way of resizing images in Magento 2.
$image->getData('url');
I have the url of the image, how to resize it using the url? Is there an API / helper that does that?
This should help you if you have $_product
object:
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$image = 'category_page_grid'; //from theme's etc/view.xml
//if you want aspect ration
//$aspect_ratio = true; $image_width = 200, $image_height = 300;
if($aspect_ratio) {
$productImage = $_imagehelper->init($_product, $image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
} else {
$productImage = $_imagehelper->init($_product, $image)->resize($image_width, $image_height);
$productImageUrl = $productImage->getUrl();
}
If you have just got URL
then using img style would be enough:
<img src="<?= $img->getData('url');?>" width="200"/>