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?

0

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);

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.