ChangedOutputOptimizer.php
1.04 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
<?php
declare(strict_types=1);
namespace ImageOptimizer;
class ChangedOutputOptimizer implements WrapperOptimizer
{
private $outputPattern;
private $optimizer;
public function __construct(string $outputPattern, Optimizer $optimizer)
{
$this->outputPattern = $outputPattern;
$this->optimizer = $optimizer;
}
public function optimize(string $filepath): void
{
$fileInfo = pathinfo($filepath);
$outputFilepath = str_replace(
['%basename%', '%filename%', '%ext%'],
[$fileInfo['dirname'], $fileInfo['filename'], isset($fileInfo['extension']) ? '.'.$fileInfo['extension'] : ''],
$this->outputPattern
);
if($outputFilepath !== $filepath) {
copy($filepath, $outputFilepath);
$filepath = $outputFilepath;
}
$this->optimizer->optimize($filepath);
}
public function unwrap(): Optimizer
{
return $this->optimizer instanceof WrapperOptimizer ? $this->optimizer->unwrap() : $this->optimizer;
}
}