1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Step; |
10
|
|
|
|
11
|
|
|
use Spatie\ImageOptimizer\OptimizerChainFactory; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Images Optimization. |
16
|
|
|
*/ |
17
|
|
|
class OptimizeImages extends AbstractStep |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function init($options) |
23
|
|
|
{ |
24
|
|
|
if (false === $this->builder->getConfig()->get('optimize.images.enabled')) { |
25
|
|
|
$this->process = false; |
26
|
|
|
|
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
if ($options['dry-run']) { |
30
|
|
|
$this->process = false; |
31
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
if (is_dir($this->builder->getConfig()->getOutputPath())) { |
35
|
|
|
$this->process = true; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function process() |
43
|
|
|
{ |
44
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', 'Optimizing images']); |
45
|
|
|
|
46
|
|
|
$optimizerChain = OptimizerChainFactory::create(); |
47
|
|
|
|
48
|
|
|
$data = Finder::create() |
49
|
|
|
->files() |
50
|
|
|
->in($this->builder->getConfig()->getOutputPath()) |
51
|
|
|
->name('/\.('.implode('|', $this->builder->getConfig()->get('optimize.images.ext')).')$/') |
52
|
|
|
->sortByName(true); |
53
|
|
|
|
54
|
|
|
$count = 0; |
55
|
|
|
$max = count($data); |
56
|
|
|
$optimized = 0; |
57
|
|
|
|
58
|
|
View Code Duplication |
if ($max <= 0) { |
|
|
|
|
59
|
|
|
$message = 'No images'; |
60
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
61
|
|
|
|
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
66
|
|
|
foreach ($data as $file) { |
67
|
|
|
$count++; |
68
|
|
|
|
69
|
|
|
$sizeBefore = $file->getSize(); |
70
|
|
|
$optimizerChain->optimize($file->getPathname()); |
71
|
|
|
$sizeAfter = $file->getSize(); |
72
|
|
|
|
73
|
|
|
$subpath = \Cecil\Util::getFS()->makePathRelative( |
74
|
|
|
$file->getPath(), |
75
|
|
|
$this->builder->getConfig()->getOutputPath() |
76
|
|
|
); |
77
|
|
|
$subpath = trim($subpath, './'); |
78
|
|
|
$path = $subpath ? $subpath.'/'.$file->getFilename() : $file->getFilename(); |
79
|
|
|
|
80
|
|
|
$message = sprintf( |
81
|
|
|
'%s: %s Ko -> %s Ko', |
82
|
|
|
$path, |
83
|
|
|
ceil($sizeBefore / 1000), |
84
|
|
|
ceil($sizeAfter / 1000) |
85
|
|
|
); |
86
|
|
|
if ($sizeAfter < $sizeBefore) { |
87
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]); |
88
|
|
|
$optimized++; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
View Code Duplication |
if ($optimized == 0) { |
|
|
|
|
92
|
|
|
$message = 'Nothing to do (not optimizers installed?)'; |
93
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.