How To Get Resize Image in Custom Module

When you are working on Custom Module with Image field in Magento 2. You want different size images. But you don’t need to resize and overwrite the image every time it’s requested. The following approach saves the resized image to a “cache” folder so it returns the image from the cache. This method is defined on a helper class so, you can call it from any class, block or template.

To get Resize Image in Custom Module you need to Follow some simple steps which are listed below.

Step 1: You need to create helper class file Image.php at Vender\Module\Helper\Image.php and past below code.

namespace Vender\Module\Helper;
use Magento\Framework\Filesystem;
use Magento\Framework\Url;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Filesystem\DirectoryList;
class Image extends \Magento\Framework\App\Helper\AbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\ResponseInterface $response,
        \Magento\Framework\App\Config\Storage\WriterInterface $resourceConfig, \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory
        ) {
            $this->scopeConfig = $scopeConfig;
            $this->_responseFactory = $responseFactory;
            $this->_url = $url;
            $this->_filesystem = $filesystem;
            $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
            $this->_imageFactory = $imageFactory;

    public function imageResize(
        $absPath = $this->_filesystem

        $imageResized = $this->_filesystem
        $imageResize = $this->_imageFactory->create();

        $imageResize->backgroundColor([255, 255, 255]);
        $imageResize->quality(100); //You can set quality according to your need.

        $dest = $imageResized ;
        $resizedURL= $this->_storeManager->getStore()
        return $resizedURL;


    public function getNewDirectoryImage($src){
        $segments = array_reverse(explode('/',$src));
        $first_dir = substr($segments[0],0,1);
        $second_dir = substr($segments[0],1,1);
        return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];

Step 2: Using below code you can call above imageResize() method from any class, block or templete.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imgpath = $objectManager->create('Vender\Module\Helper\Image')->imageResize('IMAGE_PATH','50','50','YOUR_DIR_NAME/');

To call imageResize() method first you need to create Instance of Vender\Module\Helper\Image class using with ObjectManager. To call imageResize() method you need to pass 4 parameter with this method which is imageResize($src’,$width,$height,$dir).

  • $src: Add Image Path for Ex: “Your_Uploaded_Image_Folder/a/b/abc.png”
  • $width: It should be number for Ex: “50”
  • $height: It should be number for Ex: “50”
  • $dir: Add directory name which you want to save and get resize image.

That’s it, we hope this Magento post helped you to get Resize Image in Custom Module in Magento 2.

Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.

Recent Articles
Copyright © 2022 All rights reserved.
Ads OFF toggle_off