How To Get Resize Image in Custom Module.

1
120
views
How to get Resize Image in Custom Module.
Advertisement

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.

<?php 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->_storeManager=$storeManager;
        $this->messageManager=$messageManager;
        $this->_response=$response;
        $this->_resourceConfig=$resourceConfig;
         $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_filesystem = $filesystem;
        $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->_imageFactory = $imageFactory;
    }
    public function imageResize(
    $src,
    $width=35,
    $height=35,
    $dir='yourdir/'
    ){
        $absPath = $this->_filesystem
        ->getDirectoryRead(DirectoryList::MEDIA)
        ->getAbsolutePath().$src;

        $imageResized = $this->_filesystem
        ->getDirectoryRead(DirectoryList::MEDIA)
        ->getAbsolutePath($dir).
        $this->getNewDirectoryImage($src);
        $imageResize = $this->_imageFactory->create();

        $imageResize->open($absPath);
        $imageResize->backgroundColor([255, 255, 255]);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(true);
        $imageResize->keepAspectRatio(true);

        $imageResize->resize($width,$height);
        $dest = $imageResized ;
        $imageResize->save($dest);
        $resizedURL= $this->_storeManager->getStore()
        ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).
        $dir.$this->getNewDirectoryImage($src);
        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.

1 COMMENT

  1. Thanks for sharing this post, i was looking for it, i have some products on my store and all the images has have same size, i want to resize all as default, also i have to customize products, i found this resource, it helped me in customizing products.