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.
saveCopyzoom_out_map<?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->quality(100); //You can set quality according to your need.
$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.
saveCopyzoom_out_map$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.
AI-Powered Recommended Articles
How to Resize Product Images Programmatically in Magento 2
Learn how to resize product images programmatically in Magento 2 for faster loading times and better performance.
How to Resize Specific SKU Images in Magento 2 Using CSV
Learn how to resize specific product SKU images in Magento 2 using CSV to efficiently manage product images.
How to Add Magento Ui Image Uploader on Store Configuration in Magento 2
Enable image uploads in the store configuration section of Magento 2 for a more versatile admin experience.
Magento 2 Add Image on Admin UI Grid
Learn how to add product images to the admin grid in Magento 2 to enhance product visibility in the backend.
Magento 2 Add Image Uploader on Admin UI Form
Learn how to add an image uploader field on admin UI forms in Magento 2 for better media handling in backend.
How to Get Salable Quantity in Magento 2
Learn how to retrieve the salable quantity of a product in Magento 2 to ensure accurate inventory tracking and display on your site.