There are minor changes in magento 2 to resize image, please use below code for resize the image in magento 2
For DirectoryList we can use this class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
use Magento\Framework\App\Filesystem\DirectoryList; public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\AdapterFactory $imageFactory, ) { $this->_filesystem = $filesystem; $this->_storeManager = $storeManager; $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->_imageFactory = $imageFactory; } public function imageResize(){ $image=”image.jpg”; $absPath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath().$image; $imageResized = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('resized/').$image; $imageResize = $this->_imageFactory->create(); $imageResize->open($absPath); $imageResize->constrainOnly(TRUE); $imageResize->keepTransparency(TRUE); $imageResize->keepFrame(FALSE); $imageResize->keepAspectRatio(true); $imageResize->resize(200,200); $dest = $imageResized ; $imageResize->save($dest); $resizedURL= $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$image; } |
where to add that code on file?
Add this code in Block file where we are getting the resized image.