PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / spatie / image-optimizer / src / OptimizerChain.php

OptimizerChain.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at vendor/spatie/image-optimizer/src/OptimizerChain.php

125 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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