_imageFactory = $imageFactory; $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->_storeManager = $storeManager; parent::__construct($context); } public function init($baseFile) { $this->_newFile = ''; $this->_baseFile = $baseFile; return $this; } public function getImageProcessor() { //if (!$this->_processor) { $filename = $this->_baseFile ? $this->_mediaDirectory->getAbsolutePath($this->_baseFile) : null; $this->_processor = $this->_imageFactory->create($filename); //} $this->_processor->keepAspectRatio($this->_keepAspectRatio); $this->_processor->keepFrame($this->_keepFrame); $this->_processor->keepTransparency($this->_keepTransparency); $this->_processor->constrainOnly($this->_constrainOnly); $this->_processor->backgroundColor($this->_backgroundColor); $this->_processor->quality($this->_quality); $this->_processor->resize($this->_width, $this->_height); return $this->_processor; } public function saveFile() { $filename = $this->_mediaDirectory->getAbsolutePath($this->_newFile); $this->getImageProcessor()->save($filename); //$this->_coreFileStorageDatabase->saveFile($filename); return $this; } protected function _fileExists($filename) { if ($this->_mediaDirectory->isFile($filename)) { return true; } else { return false; } } public function isCached() { if (is_string($this->_newFile)) { return $this->_fileExists($this->_newFile); } } public function resize($width,$height = null) { if($this->_baseFile){ $this->_width = $width; $this->_height = $height; $path = 'blog/cache/'.$width.'x'.$height; $this->_newFile = $path. '/' . $this->_baseFile; if(!$this->isCached()){ $this->saveFile(); } } return $this; } public function __toString() { $url = ""; if($this->_baseFile){ $url = $this->_storeManager->getStore()->getBaseUrl( \Magento\Framework\UrlInterface::URL_TYPE_MEDIA ) . $this->_newFile; } return $url; } }