I have uploaded some rectangle images, but I would like to show them as a square image. So Magento should add on the right and left side some white space. How can I arrange this?
Hi @alexander_jorda
You need to create custom module for your requirement and you can use below code for resize images according to your requirement:
protected $imageFactory; public function __construct( \Magento\Framework\Image\Factory $imageFactory ) { $this->imageFactory = $imageFactory; } public function resizeImageToSquare($imagePath, $imageName){ $image = $this->imageFactory->create($imagePath . '/' . $imageName); $image->keepTransparency(true); $image->constrainOnly(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor([255, 255, 255]); $biggestDimension = ($image->getOriginalWidth() > $image->getOriginalHeight() ? 'width' : 'height'); if($biggestDimension == 'width'){ $image->resize($image->getOriginalWidth(), $image->getOriginalWidth()); }else{ $image->resize($image->getOriginalHeight(), $image->getOriginalHeight()); } $image->save($imagePath .”_resized”, $imageName); return $this; }
This code help you to resize images.
If issue resolve, please click on 'Kudos' & Accept as Solution!
Thanks for the code! Where should I put this?
@Bhanu Periwal I do not know how I should use this? Where do I need to add this file? I found also another solution from Mageplaza, which is similar to your code. This is the title of the post:
"How to change product image size in Magento 2"
If you fill this in Google, you will find the post. But I do not know how it works. Can you help me with this?