I have written code for resizing any products images in Magento 2 according to your desired width and height. I will describe how to resize images In Magento2, Update product image in Magento2, Remove white frame in Magento 2, change product image dimensions Magento 2 such as needing to resize a custom image and display it on the site in special sized areas.
For Magento 2, you can use the following code to resize your images in Magento 2
Step 1: You need to create helper class file Image.php at Vender\Module\Helper\Image.php and the 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='resized/'
){
$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 template.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imgpath = $objectManager->create('Vender\Module\Helper\Image')->imageResize('IMAGE_PATH','50','50','YOUR_DIR_NAME/');
Now I am going to explain the methods I have used
1. getDirectoryRead()
2. getAbsolutePath()
3. backgroundColor()
4. constrainOnly()
5. keepTransparency()
6. keepFrame()
7. keepAspectRatio()
Magento 2 – Properly remove white image from frame
< ?php foreach ($this->getGalleryImages() as $_image): ?>
•
helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56); ?>" width="56" height="56" alt="< ?php echo $this->htmlEscape($_image->getLabel()) ?>" />
< ?php endforeach; ?>
If you want resizing image in magento 2 and you want further information about this then read our blog : HOW TO RESIZE IMAGES IN MAGENTO 2?