Source of file ImageOptimiserService.php
Size: 4,613 Bytes - Last Modified: 2021-12-24T06:51:20+00:00
/var/www/docs.ssmods.com/process/src/code/ImageOptimiserService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?php use Symfony\Component\Process\Process; /** * Class ImageOptimiserService */ class ImageOptimiserService implements ImageOptimiserInterface { /** * @var Config_ForClass */ protected $config; /** * @var Psr\Log\LoggerInterface */ protected $logger; /** * @param \Psr\Log\LoggerInterface $logger * @param Config_ForClass $config */ public function __construct(Psr\Log\LoggerInterface $logger = null, Config_ForClass $config = null) { $this->config = $config ?: Config::inst()->forClass(__CLASS__); if ($logger) { $this->logger = $logger; } } /** * @param \Psr\Log\LoggerInterface $logger */ public function setLogger($logger) { $this->logger = $logger; } /** * @param $filename * @return mixed|void */ public function optimiseImage($filename) { if (file_exists($filename)) { $size = @getimagesize($filename); if (!is_array($size)) { if (null !== $this->logger) { $this->logger->error("Error reading file when attempting to optimize"); } return; } list($width, $height, $type, $attr) = $size; $commands = $this->getCommands($filename, $type = $this->getImageType($type)); foreach ($commands as $command) { try { $process = $this->execCommand($command); $successful = in_array($process->getExitCode(), $this->config->get('successStatuses')); if (null !== $this->logger && (!$successful || $this->config->get('debug'))) { // Do this so the log isn't treated as a web request in raven $requestMethod = $_SERVER['REQUEST_METHOD']; unset($_SERVER['REQUEST_METHOD']); $logType = $successful ? 'info' : 'error'; $this->logger->$logType( "SilverStripe \"$type\" optimisation $logType", array( 'command' => $command, 'exitCode' => $process->getExitCode(), 'output' => $process->getOutput(), 'errorOutput' => $process->getErrorOutput() ) ); $_SERVER['REQUEST_METHOD'] = $requestMethod; } } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->error( "SilverStripe \"$type\" optimisation exception", array( 'exception' => $e ) ); } } } } } /** * Returns a text version of IMAGETYPE_* constants * @param $type * @return bool|string */ protected function getImageType($type) { switch ($type) { case IMAGETYPE_JPEG: return 'jpg'; case IMAGETYPE_PNG: return 'png'; case IMAGETYPE_GIF: return 'gif'; default: return false; } } /** * Gets all enabled commands for the file and type of image * @param $filename * @param $type * @return array */ protected function getCommands($filename, $type) { $commands = $this->config->get('availableCommands'); if (!$type || !isset($commands[$type])) { return array(); } $command = array(); foreach ((array)$this->config->get('enabledCommands') as $commandType) { if (isset($commands[$type][$commandType])) { $command[] = sprintf( $commands[$type][$commandType], rtrim($this->config->get('binDirectory'), '/'), escapeshellarg($filename), $this->config->get('optimisingQuality') ); } } return $command; } /** * Executes the specified command * @param $command * @return \Symfony\Component\Process\Process */ private function execCommand($command) { $process = new Process($command); $process->run(); return $process; } } |