0

I want to resize only one catalog product images. Is it possible in magento2?

I already know the command for resizing catalog image:

php bin/magento catalog:images:resize

but i want to do this for only one product images

4
  • using command or programmatically ? Commented Apr 4, 2019 at 10:19
  • any way !! but i got the solution Commented Apr 4, 2019 at 10:56
  • How did you resize for only 1 product? could you share your answer @Rutvee Sojitra
    – aravind
    Commented Feb 3, 2020 at 4:28
  • Downvote. Happy you figured it out, but don't be so selfish to not share how....
    – gnicko
    Commented Apr 20, 2022 at 15:04

2 Answers 2

0

Magento does not have any command line. So you have to build the command yourself.

Take look on Magento\MediaStorage\Service\ImageResize::resizeFromThemes

1
  • unable to find resizeFromThemes in Imageresize Commented Apr 4, 2019 at 9:42
0

Try This in Block File :-

       protected $_filesystem ;
       protected $_imageFactory;
       public function __construct(            
            \Magento\Framework\Filesystem $filesystem,         
            \Magento\Framework\Image\AdapterFactory $imageFactory         
            ){         
                 $this->_filesystem = $filesystem;               
                 $this->_imageFactory = $imageFactory;         
            }

        public function resize($image, $width = null, $height = null)
        {
            $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('custom_module/posts/').$image;
            if (!file_exists($absolutePath)) return false;
            $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
            if (!file_exists($imageResized)) {

                $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);         
            } 
            $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;
            return $resizedURL;
      } 

Your Answer

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

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