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