Skip to content

Commit

Permalink
Added image processor dependencies check
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnax committed Jun 8, 2023
1 parent 6a21116 commit 753061e
Showing 1 changed file with 67 additions and 29 deletions.
96 changes: 67 additions & 29 deletions Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Filesystem\Glob;
use Magento\Framework\Filesystem\Io\File;
use Magento\Framework\Image\AdapterFactory;
use Magento\Framework\Image\Factory;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -103,6 +104,10 @@ class Image extends AbstractHelper
*/
protected $_file;
protected $_appState;
/**
* @var AdapterFactory
*/
protected $adapterFactory;
/**
* @var string
*/
Expand All @@ -114,17 +119,32 @@ public function __construct(
Factory $imageFactory,
File $file,
State $appState,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
AdapterFactory $adapterFactory
) {
$this->_mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
$this->_imageFactory = $imageFactory;
$this->_file = $file;
$this->_storeManager = $storeManager;
$this->_appState = $appState;
$this->adapterFactory = $adapterFactory;

parent::__construct($context);
}

/**
* @return bool
*/
public function checkDependencies() {
try {
$this->adapterFactory->create();
return true;
} catch (Exception $e) {
$this->_logger->critical($e->getMessage());
return false;
}
}

public function adaptiveResize($size, $fileTemplate = null)
{
if (!empty($fileTemplate)) {
Expand All @@ -139,8 +159,9 @@ public function adaptiveResize($size, $fileTemplate = null)
}
$this->resize();
} catch (Exception $e) {
if ( State::MODE_DEVELOPER == $this->_appState->getMode()) {
$this->_logger->warning("Olegnax Core. Can\'t resize image, skipping. The file does not exist or not accessible: " . $e, [$this->_baseFile]);
if (State::MODE_DEVELOPER == $this->_appState->getMode()) {
$this->_logger->warning("Olegnax Core. Can\'t resize image, skipping. The file does not exist or not accessible: " . $e,
[$this->_baseFile]);
}
}

Expand Down Expand Up @@ -203,11 +224,38 @@ public function crop()
return $this;
}

/**
* @return string
*/
public function getBaseFile()
{
return $this->_baseFile;
}

/**
* Set filenames for base file and new file
*
* @param string $file
*
* @return Image
*/
public function setBaseFile($file)
{
if ($this->_fileExists($file)) {
$this->_baseFile = $file;
} elseif (State::MODE_DEVELOPER == $this->_appState->getMode()) {
$this->_logger->warning(__('Olegnax Core. The file does not exist or not accessible: ') . $file);
}

return $this;
}

/**
* First check this file on FS
* If it doesn't exist - try to download it from DB
*
* @param string $filename
*
* @return bool
*/
protected function _fileExists($filename)
Expand All @@ -223,7 +271,7 @@ private function getResizedFile()
$path_data = pathinfo($this->getBaseFile());

if (is_array($path_data) && !empty($path_data)) {
if(array_key_exists('extension', $path_data)){
if (array_key_exists('extension', $path_data)) {
$filename = str_replace(
[
'{dirname}',
Expand Down Expand Up @@ -268,31 +316,6 @@ private function getResizedFile()
return '';
}

/**
* @return string
*/
public function getBaseFile()
{
return $this->_baseFile;
}

/**
* Set filenames for base file and new file
*
* @param string $file
* @return Image
*/
public function setBaseFile($file)
{
if ($this->_fileExists($file)) {
$this->_baseFile = $file;
} elseif ( State::MODE_DEVELOPER == $this->_appState->getMode()) {
$this->_logger->warning(__('Olegnax Core. The file does not exist or not accessible: ') . $file);
}

return $this;
}

/**
* @return int
*/
Expand All @@ -303,6 +326,7 @@ public function getWidth()

/**
* @param int $width
*
* @return Image
*/
public function setWidth($width)
Expand Down Expand Up @@ -347,6 +371,7 @@ public function getHeight()

/**
* @param int $height
*
* @return $this
*/
public function setHeight($height)
Expand All @@ -373,6 +398,7 @@ public function getDestinationSubdir()

/**
* @param string $dir
*
* @return $this
*/
public function setDestinationSubdir($dir)
Expand All @@ -395,6 +421,7 @@ public function getQuality()
* Set image quality, values in percentage from 0 to 100
*
* @param int $quality
*
* @return $this
*/
public function setQuality($quality)
Expand Down Expand Up @@ -436,6 +463,7 @@ public function resize()

/**
* @param \Magento\Framework\Image $processor
*
* @return $this
*/
public function setImageProcessor($processor)
Expand Down Expand Up @@ -463,6 +491,7 @@ public function getUrl()

/**
* @param string $size
*
* @return $this
*/
public function setSize($size)
Expand Down Expand Up @@ -497,6 +526,7 @@ public function setSize($size)

/**
* @param $file
*
* @throws Exception
*/
public function removeResized($file)
Expand Down Expand Up @@ -539,6 +569,7 @@ public function removeResized($file)
/**
* @param $file
* @param array $attributes
*
* @return $this
* @throws Exception
*/
Expand Down Expand Up @@ -574,6 +605,7 @@ protected function _reset()

/**
* @param int[] $rgbArray
*
* @return $this
*/
public function setBackgroundColor(array $rgbArray)
Expand All @@ -584,6 +616,7 @@ public function setBackgroundColor(array $rgbArray)

/**
* @param bool $flag
*
* @return $this
*/
public function setConstrainOnly($flag)
Expand All @@ -594,6 +627,7 @@ public function setConstrainOnly($flag)

/**
* @param bool $keep
*
* @return $this
*/
public function setKeepAspectRatio($keep)
Expand All @@ -604,6 +638,7 @@ public function setKeepAspectRatio($keep)

/**
* @param bool $keep
*
* @return $this
*/
public function setKeepFrame($keep)
Expand All @@ -614,6 +649,7 @@ public function setKeepFrame($keep)

/**
* @param bool $keep
*
* @return $this
*/
public function setKeepTransparency($keep)
Expand Down Expand Up @@ -680,6 +716,7 @@ protected function setImageProperties()
* Retrieve image attribute
*
* @param string $name
*
* @return string
*/
protected function getAttribute($name)
Expand All @@ -689,6 +726,7 @@ protected function getAttribute($name)

/**
* @param bool $flag
*
* @return $this
*/
public function seCropOnly($flag)
Expand Down

0 comments on commit 753061e

Please sign in to comment.