|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Step\Optimize; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Util\ImageOptimizer as Optimizer; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Optimize images step. |
|
20
|
|
|
* |
|
21
|
|
|
* This step optimizes images in the build process by using the |
|
22
|
|
|
* `Optimizer` class to process image files. |
|
23
|
|
|
* It extends the `AbstractOptimize` class and implements the necessary |
|
24
|
|
|
* methods to handle image optimization. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Images extends AbstractOptimize |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function getName(): string |
|
32
|
|
|
{ |
|
33
|
1 |
|
return 'Optimizing images'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function init(array $options): void |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->type = 'images'; |
|
42
|
1 |
|
parent::init($options); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function setProcessor(): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->processor = Optimizer::create((int) $this->config->get('assets.images.quality')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function processFile(\Symfony\Component\Finder\SplFileInfo $file): string |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
1 |
|
$this->processor->optimize($file->getPathname()); |
|
60
|
|
|
} catch (\Exception $e) { |
|
61
|
|
|
$this->builder->getLogger()->error(\sprintf('Unable to optimize image "%s": "%s"', $file->getPathname(), $e->getMessage())); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $file->getContents(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function encode(?string $content = null): ?string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return base64_encode((string) $content); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function decode(?string $content = null): ?string |
|
79
|
|
|
{ |
|
80
|
1 |
|
return base64_decode((string) $content); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|