1

I want to resize image without loss quality in magento 2 i used code

 $imageResize = $this->_imageFactory->create();
 $imageResize->open($absolutePath);
 $imageResize->constrainOnly(TRUE);
 $imageResize->keepTransparency(TRUE);
 $imageResize->keepFrame(FALSE);
 $imageResize->keepAspectRatio(TRUE);
 $imageResize->resize($width, $height);
    //destination folder                
 $destination = $imageResized;
    //save image    
 $imageResize->save($destination);

when i resize it reduce the quality of image as compare to original image. How can i resize without losing quality?

1 Answer 1

1

You can resize product image using below code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$productImageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');

$productId = 1; ///  change your product ID
$width = 150; /// Change as per your requirement
$height = 150; /// Change as per your requirement
$image_type = 'thumbnail';

$product = $productRepository->getById($productId);
$resizedImage = $productImageHelper->init($product, $image_type)
                                   ->constrainOnly(TRUE)
                                   ->keepAspectRatio(TRUE)
                                   ->keepTransparency(TRUE)
                                   ->keepFrame(FALSE)
                                   ->resize($width, $height);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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