I want to resize category & product images for custom widget and I prefer Magento default function so I found following code to do so but I think it's for Magento 1

$imageObj = new Varien_Image ( $imageUrl );
$imageObj=$imageObj->constrainOnly ( true )
->keepAspectRatio ( true )
->keepFrame ( false )
->quality ( $quality )
->resize ( $width, $height )
->save ( $imageResized );

So I wish to know that does Magento 2 provide any function to resize,crop,scale any images including category,product etc.??

1 Answer 1


You can get all image related methods for category and product from helper file inside Magento_Catalog module core file,


List of methods are

  • resize()

  • List item

  • keepAspectRatio()

  • setQuality()
  • keepFrame()
  • keepTransparency()

    $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

<img src="<?php echo $_imagehelper->init($_product, 'thumbnail')->keepAspectRatio(true)->resize('400', '400'); ?>" />

this is to use in version 2.2. the image type was changed

<img src="<?php echo $_imagehelper->init($_product, 'product_base_image')->keepAspectRatio(true)->resize('400', '400'); ?>" />

For custom Image Resize section

Inside Block file,

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

    // pass imagename, width and height
    public function resize($image, $width = null, $height = null)
        $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('custom_module/posts/').$image;

        $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;         
        //create image factory...
        $imageResize = $this->_imageFactory->create();         
        //destination folder                
        $destination = $imageResized ;    
        //save image      

        $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;
        return $resizedURL;

Call inside template file $block->resize('img.jpg',300,300);

  • 1
    That is for product images only I guess, I also want to resize custom images and category images as well so any idea regarding that?? Commented Sep 23, 2016 at 9:54
  • please check updated answer Commented Sep 23, 2016 at 10:11
  • Well nice work around, But I simply want to pass the image url in function, I tried with above code but didn't worked for me can you help please?? Commented Sep 23, 2016 at 10:28
  • you have to modify content under $absolutePath in resize block and check it, currently we have display image saved location from folder, you have to change here. OR just set image url for $absolutePath and check Commented Sep 23, 2016 at 10:32
  • Didn't work for me Commented Sep 23, 2016 at 11:22

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.