0

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?

0

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"/>
  • I have the image URL and because I want to resize my gallery images and not just one product image. is there any way to do that? – Magento_learner Nov 6 '20 at 1:39
  • use the second method from my answers. – Adarsh Khatri Nov 9 '20 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.