Passed
Push — refactor ( ca8638 )
by Arnaud
05:32
created

ImageOptimizer::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 1
dl 0
loc 40
rs 9.408
c 0
b 0
f 0
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\Util;
15
16
use Spatie\ImageOptimizer\OptimizerChain;
17
use Spatie\ImageOptimizer\Optimizers\Avifenc;
18
use Spatie\ImageOptimizer\Optimizers\Cwebp;
19
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
20
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
21
use Spatie\ImageOptimizer\Optimizers\Optipng;
22
use Spatie\ImageOptimizer\Optimizers\Pngquant;
23
use Spatie\ImageOptimizer\Optimizers\Svgo;
24
25
/**
26
 * Image Optimizer class.
27
 *
28
 * This class provides a method to create an image optimizer chain with various optimizers.
29
 */
30
class ImageOptimizer
31
{
32
    /**
33
     * Image Optimizer Chain.
34
     */
35
    public static function create(int $quality): OptimizerChain
36
    {
37
        return (new OptimizerChain())
38
            ->addOptimizer(new Jpegoptim([
39
                "--max=$quality",
40
                '--strip-all',
41
                '--all-progressive',
42
            ]))
43
            ->addOptimizer(new Pngquant([
44
                "--quality=$quality",
45
                '--force',
46
                '--skip-if-larger',
47
            ]))
48
            ->addOptimizer(new Optipng([
49
                '-i0',
50
                '-o2',
51
                '-quiet',
52
            ]))
53
            ->addOptimizer(new Svgo([
54
                '--disable={cleanupIDs,removeViewBox}',
55
            ]))
56
            ->addOptimizer(new Gifsicle([
57
                '-b',
58
                '-O3',
59
            ]))
60
            ->addOptimizer(new Cwebp([
61
                '-m 6',
62
                '-pass 10',
63
                '-mt',
64
                '-q $quality',
65
            ]))
66
            ->addOptimizer(new Avifenc([
67
                '-a cq-level=' . round(63 - $quality * 0.63),
68
                '-j all',
69
                '--min 0',
70
                '--max 63',
71
                '--minalpha 0',
72
                '--maxalpha 63',
73
                '-a end-usage=q',
74
                '-a tune=ssim',
75
            ]));
76
    }
77
}
78