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?
asked Nov 6, 2020 at 0:00
Magento_learner
1351 gold badge4 silver badges19 bronze badges
1 Answer 1
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"/>
answered Nov 6, 2020 at 0:53
Adarsh Khatri
8,4133 gold badges30 silver badges60 bronze badges
-
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– Magento_learner2020年11月06日 01:39:06 +00:00Commented Nov 6, 2020 at 1:39
-
use the second method from my answers.Adarsh Khatri– Adarsh Khatri2020年11月09日 00:11:57 +00:00Commented Nov 9, 2020 at 0:11
default