-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathOptimizeImageService.php
More file actions
135 lines (112 loc) · 3.94 KB
/
OptimizeImageService.php
File metadata and controls
135 lines (112 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Lemming\Imageoptimizer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LogLevel;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class OptimizeImageService implements LoggerAwareInterface
{
use LoggerAwareTrait;
public const BINARY_NOT_FOUND = 'The Binary was not found in $PATH. $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'binSetup\'] may help you.';
private string $command;
/**
* @var array<int, string>
*/
private array $output = [];
private ExtensionConfiguration $extensionConfiguration;
/**
* DI does NOT work in Install Tool context!
*/
public function __construct()
{
$this->extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
}
/**
* Perform image optimization
*/
public function process(
string $file,
?string $extension = null,
bool $fileIsUploaded = false,
bool $testMode = false
): bool {
$this->reset();
if (! file_exists($file)) {
return false;
}
$configuration = $this->extensionConfiguration->get('imageoptimizer');
if ($extension === null) {
$pathInfo = pathinfo($file);
if (isset($pathInfo['extension'])) {
$extension = $pathInfo['extension'];
}
}
$extension = strtolower((string) $extension);
if ($extension === 'jpeg') {
$extension = 'jpg';
}
$when = $fileIsUploaded === true ? 'Upload' : 'Processing';
$on = $extension . 'On' . $when;
if (! isset($configuration[$on])) {
return false;
}
if ((bool) $configuration[$on] === false && $testMode === false) {
return false;
}
$binaryName = $configuration[$extension . 'Binary'];
$binary = CommandUtility::getCommand(escapeshellcmd($binaryName));
if (! is_string($binary)) {
if (! $testMode) {
$this->logger->log(LogLevel::ERROR, self::BINARY_NOT_FOUND, [
'file' => $file,
'fileExtension' => $extension,
'binary' => $binaryName,
]);
}
throw new BinaryNotFoundException('Binary ' . $binaryName . ' not found', 1488631746);
}
$parametersOn = $extension . 'ParametersOn' . $when;
$parameters = (string) $configuration[$parametersOn];
$parameters = (string) preg_replace('/[^A-Za-z0-9-%: =]/', '', $parameters);
$parameters = (string) preg_replace('/%s/', escapeshellarg($file), $parameters);
$this->command = $binary . ' ' . $parameters . ' 2>&1';
$returnValue = 0;
CommandUtility::exec($this->command, $this->output, $returnValue);
$executionWasSuccessful = $returnValue === 0;
if (! $testMode) {
$this->logger->log(
$executionWasSuccessful ? LogLevel::INFO : LogLevel::ERROR,
$executionWasSuccessful ? 'Optimization was successful' : 'Optimization failed',
[
'file' => $file,
'fileExtension' => $extension,
'fileIsUploaded' => $fileIsUploaded ? 1 : 0,
'command' => $this->command,
'returnValue' => $returnValue,
'output' => $this->output,
]
);
}
GeneralUtility::fixPermissions($file);
return $executionWasSuccessful;
}
public function getCommand(): string
{
return $this->command;
}
/**
* @return array<int, string>
*/
public function getOutput(): array
{
return $this->output;
}
protected function reset(): void
{
$this->command = '';
$this->output = [];
}
}