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