Passed
Push — master ( 315a8e...fda1a1 )
by Arnaud
07:50 queued 02:11
created

ImageOptimizer::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 1
dl 0
loc 40
ccs 39
cts 39
cp 1
crap 1
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 1
    public static function create(int $quality): OptimizerChain
36
    {
37 1
        return (new OptimizerChain())
38 1
            ->addOptimizer(new Jpegoptim([
39 1
                "--max=$quality",
40 1
                '--strip-all',
41 1
                '--all-progressive',
42 1
            ]))
43 1
            ->addOptimizer(new Pngquant([
44 1
                "--quality=$quality",
45 1
                '--force',
46 1
                '--skip-if-larger',
47 1
            ]))
48 1
            ->addOptimizer(new Optipng([
49 1
                '-i0',
50 1
                '-o2',
51 1
                '-quiet',
52 1
            ]))
53 1
            ->addOptimizer(new Svgo([
54 1
                '--disable={cleanupIDs,removeViewBox}',
55 1
            ]))
56 1
            ->addOptimizer(new Gifsicle([
57 1
                '-b',
58 1
                '-O3',
59 1
            ]))
60 1
            ->addOptimizer(new Cwebp([
61 1
                '-m 6',
62 1
                '-pass 10',
63 1
                '-mt',
64 1
                '-q $quality',
65 1
            ]))
66 1
            ->addOptimizer(new Avifenc([
67 1
                '-a cq-level=' . round(63 - $quality * 0.63),
68 1
                '-j all',
69 1
                '--min 0',
70 1
                '--max 63',
71 1
                '--minalpha 0',
72 1
                '--maxalpha 63',
73 1
                '-a end-usage=q',
74 1
                '-a tune=ssim',
75 1
            ]));
76
    }
77
}
78