_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 resize($width, $height = null) { if ($this->_baseFile){ $path = 'blog/cache/' . $width . 'x' . $height; $this->_newFile = $path. '/' . $this->_baseFile; if (true || !$this->fileExists($this->_newFile)) { $this->resizeBaseFile($width, $height); } } return $this; } protected function resizeBaseFile($width, $height) { if (!$this->fileExists($this->_baseFile)) { $this->_baseFile = null; return $this; } $processor = $this->_imageFactory->create( $this->_mediaDirectory->getAbsolutePath($this->_baseFile) ); $processor->keepAspectRatio($this->_keepAspectRatio); $processor->keepFrame($this->_keepFrame); $processor->keepTransparency($this->_keepTransparency); $processor->constrainOnly($this->_constrainOnly); $processor->backgroundColor($this->_backgroundColor); $processor->quality($this->_quality); $processor->resize($width, $height); $newFile = $this->_mediaDirectory->getAbsolutePath($this->_newFile); $processor->save($newFile); unset($processor); return $this; } protected function fileExists($filename) { return $this->_mediaDirectory->isFile($filename); } public function __toString() { $url = ""; if ($this->_baseFile){ $url = $this->_storeManager->getStore()->getBaseUrl( \Magento\Framework\UrlInterface::URL_TYPE_MEDIA ) . $this->_newFile; } return $url; } }