| 1 |
<?php |
| 2 |
|
| 3 |
namespace Spatie\ImageOptimizer; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Psr\Log\LoggerInterface; |
| 7 |
use Symfony\Component\Process\Process; |
| 8 |
|
| 9 |
class OptimizerChain |
| 10 |
{ |
| 11 |
/* @var \Spatie\ImageOptimizer\Optimizer[] */ |
| 12 |
protected $optimizers = []; |
| 13 |
|
| 14 |
/** @var \Psr\Log\LoggerInterface */ |
| 15 |
protected $logger; |
| 16 |
|
| 17 |
/** @var int */ |
| 18 |
protected $timeout = 60; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->useLogger(new DummyLogger()); |
| 23 |
} |
| 24 |
|
| 25 |
public function getOptimizers(): array |
| 26 |
{ |
| 27 |
return $this->optimizers; |
| 28 |
} |
| 29 |
|
| 30 |
public function addOptimizer(Optimizer $optimizer) |
| 31 |
{ |
| 32 |
$this->optimizers[] = $optimizer; |
| 33 |
|
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
public function setOptimizers(array $optimizers) |
| 38 |
{ |
| 39 |
$this->optimizers = []; |
| 40 |
|
| 41 |
foreach ($optimizers as $optimizer) { |
| 42 |
$this->addOptimizer($optimizer); |
| 43 |
} |
| 44 |
|
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* Set the amount of seconds each separate optimizer may use. |
| 50 |
*/ |
| 51 |
public function setTimeout(int $timeoutInSeconds) |
| 52 |
{ |
| 53 |
$this->timeout = $timeoutInSeconds; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
public function useLogger(LoggerInterface $log) |
| 59 |
{ |
| 60 |
$this->logger = $log; |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function optimize(string $pathToImage, ?string $pathToOutput = null) |
| 66 |
{ |
| 67 |
if ($pathToOutput) { |
| 68 |
$check = copy($pathToImage, $pathToOutput); |
| 69 |
if ($check == false) { |
| 70 |
throw new InvalidArgumentException("Cannot copy file"); |
| 71 |
} |
| 72 |
$pathToImage = $pathToOutput; |
| 73 |
} |
| 74 |
$image = new Image($pathToImage); |
| 75 |
$this->logger->info("Start optimizing {$pathToImage}"); |
| 76 |
|
| 77 |
foreach ($this->optimizers as $optimizer) { |
| 78 |
$this->applyOptimizer($optimizer, $image); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
protected function applyOptimizer(Optimizer $optimizer, Image $image) |
| 83 |
{ |
| 84 |
if (! $optimizer->canHandle($image)) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$optimizerClass = get_class($optimizer); |
| 89 |
|
| 90 |
$this->logger->info("Using optimizer: `{$optimizerClass}`"); |
| 91 |
|
| 92 |
$optimizer->setImagePath($image->path()); |
| 93 |
|
| 94 |
$command = $optimizer->getCommand(); |
| 95 |
|
| 96 |
$this->logger->info("Executing `{$command}`"); |
| 97 |
|
| 98 |
$process = Process::fromShellCommandline($command); |
| 99 |
|
| 100 |
$process |
| 101 |
->setTimeout($this->timeout) |
| 102 |
->run(); |
| 103 |
|
| 104 |
if ( |
| 105 |
($tmpPath = $optimizer->getTmpPath()) && |
| 106 |
file_exists($tmpPath) |
| 107 |
) { |
| 108 |
unlink($tmpPath); |
| 109 |
} |
| 110 |
|
| 111 |
$this->logResult($process); |
| 112 |
} |
| 113 |
|
| 114 |
protected function logResult(Process $process) |
| 115 |
{ |
| 116 |
if (! $process->isSuccessful()) { |
| 117 |
$this->logger->error("Process errored with `{$process->getErrorOutput()}`"); |
| 118 |
|
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$this->logger->info("Process successfully ended with output `{$process->getOutput()}`"); |
| 123 |
} |
| 124 |
} |
| 125 |
|