|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ImageOptimizer; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Spatie\ImageOptimizer\Exceptions\OptimizerNotInstalledException; |
|
7
|
|
|
use Symfony\Component\Process\Process; |
|
8
|
|
|
|
|
9
|
|
|
class OptimizerChain |
|
10
|
|
|
{ |
|
11
|
|
|
/* @var \Spatie\ImageOptimizer\Optimizer[] */ |
|
12
|
|
|
protected $optimizers = []; |
|
13
|
|
|
|
|
14
|
|
|
/* @var \Spatie\ImageOptimizer\Optimizer[] */ |
|
15
|
|
|
protected $optimized = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
18
|
|
|
protected $logger; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int */ |
|
21
|
|
|
protected $timeout = 60; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->useLogger(new DummyLogger()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getOptimizers(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->optimizers; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function addOptimizer(Optimizer $optimizer) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->optimizers[] = $optimizer; |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setOptimizers(array $optimizers) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->optimizers = []; |
|
43
|
|
|
|
|
44
|
|
|
foreach ($optimizers as $optimizer) { |
|
45
|
|
|
$this->addOptimizer($optimizer); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/* |
|
52
|
|
|
* Set the amount of seconds each separate optimizer may use. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setTimeout(int $timeoutInSeconds) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->timeout = $timeoutInSeconds; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function useLogger(LoggerInterface $log) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->logger = $log; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function optimize(string $pathToImage, string $pathToOutput = null) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($pathToOutput) { |
|
|
|
|
|
|
71
|
|
|
copy($pathToImage, $pathToOutput); |
|
72
|
|
|
|
|
73
|
|
|
$pathToImage = $pathToOutput; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$image = new Image($pathToImage); |
|
77
|
|
|
|
|
78
|
|
|
$this->logger->info("Start optimizing {$pathToImage}"); |
|
79
|
|
|
|
|
80
|
|
|
foreach ($this->optimizers as $optimizer) { |
|
81
|
|
|
$this->applyOptimizer($optimizer, $image); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->optimized; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function applyOptimizer(Optimizer $optimizer, Image $image) |
|
88
|
|
|
{ |
|
89
|
|
|
if (! $optimizer->canHandle($image)) { |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$optimizerClass = get_class($optimizer); |
|
94
|
|
|
|
|
95
|
|
|
$this->logger->info("Using optimizer: `{$optimizerClass}`"); |
|
96
|
|
|
|
|
97
|
|
|
$optimizer->setImagePath($image->path()); |
|
98
|
|
|
|
|
99
|
|
|
$command = $optimizer->getCommand(); |
|
100
|
|
|
|
|
101
|
|
|
$this->logger->info("Executing `{$command}`"); |
|
102
|
|
|
|
|
103
|
|
|
$process = Process::fromShellCommandline($command); |
|
104
|
|
|
|
|
105
|
|
|
$process |
|
106
|
|
|
->setTimeout($this->timeout) |
|
107
|
|
|
->run(); |
|
108
|
|
|
|
|
109
|
|
|
$this->logResult($process, $optimizer); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function logResult(Process $process, Optimizer $optimizer) |
|
113
|
|
|
{ |
|
114
|
|
|
if (! $process->isSuccessful()) { |
|
115
|
|
|
$optimized = false; |
|
116
|
|
|
|
|
117
|
|
|
$this->logger->error("Process errored with `{$process->getErrorOutput()}`"); |
|
118
|
|
|
|
|
119
|
|
|
if (strpos($process->getErrorOutput(), $optimizer->binaryName().': '.strtolower(Process::$exitCodes[127])) !== false) { |
|
120
|
|
|
throw new OptimizerNotInstalledException('Optimized not installed!'); |
|
121
|
|
|
|
|
122
|
|
|
return; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
$optimized = true; |
|
126
|
|
|
|
|
127
|
|
|
$this->logger->info("Process successfully ended with output `{$process->getOutput()}`"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->setOptimizerStatus($optimizer, $optimized); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function setOptimizerStatus(Optimizer $optimizer, $status) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->optimized[$optimizer->binaryName()] = $status; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: