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