Passed
Push — feat/markdown-highlighter ( 1efac5...98434c )
by Arnaud
13:07 queued 08:45
created

Image::getSrcset()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 21
ccs 0
cts 14
cp 0
crap 42
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Assets;
15
16
use Cecil\Assets\Image\Optimizers\Cwebp;
17
use Intervention\Image\ImageManagerStatic as ImageManager;
18
use Spatie\ImageOptimizer\OptimizerChain;
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
class Image
26
{
27
    /**
28
     * Image Optimizer Chain.
29
     */
30 1
    public static function optimizer(int $quality): OptimizerChain
31
    {
32 1
        return (new OptimizerChain())
33 1
            ->addOptimizer(new Jpegoptim([
34 1
                "--max=$quality",
35 1
                '--strip-all',
36 1
                '--all-progressive',
37
            ]))
38 1
            ->addOptimizer(new Pngquant([
39 1
                "--quality=$quality",
40 1
                '--force',
41 1
                '--skip-if-larger',
42
            ]))
43 1
            ->addOptimizer(new Optipng([
44 1
                '-i0',
45
                '-o2',
46
                '-quiet',
47
            ]))
48 1
            ->addOptimizer(new Svgo([
49 1
                '--disable={cleanupIDs,removeViewBox}',
50
            ]))
51 1
            ->addOptimizer(new Gifsicle([
52 1
                '-b',
53
                '-O3',
54
            ]))
55 1
            ->addOptimizer(new Cwebp([
56 1
                '-m 6',
57
                '-pass 10',
58
                '-mt',
59
                '-q $quality',
60
            ]));
61
    }
62
63
    /**
64
     * Build the `srcset` attribute for responsive images.
65
     * ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w".
66
     */
67 1
    public static function buildSrcset(Asset $asset, array $widths): string
68
    {
69 1
        $srcset = '';
70 1
        foreach ($widths as $width) {
71 1
            if ($asset->getWidth() < $width) {
72 1
                break;
73
            }
74
            $img = $asset->resize($width);
75
            $srcset .= \sprintf('%s %sw, ', $img, $width);
76
        }
77 1
        rtrim($srcset, ', ');
78
        // add reference image
79 1
        if (!empty($srcset)) {
80
            $srcset .= \sprintf('%s %sw', (string) $asset, $asset->getWidth());
81
        }
82
83 1
        return $srcset;
84
    }
85
86
    /**
87
     * Converts an asset image to WebP.
88
     */
89 1
    public static function convertTopWebp(Asset $asset, int $quality): Asset
90
    {
91 1
        $assetWebp = clone $asset;
92 1
        $format = 'webp';
93 1
        $image = ImageManager::make($assetWebp['content']);
94 1
        $assetWebp['content'] = (string) $image->encode($format, $quality);
95 1
        $assetWebp['path'] = preg_replace('/\.'.$asset['ext'].'$/m', ".$format", $asset['path']);
96 1
        $assetWebp['ext'] = $format;
97
98 1
        return $assetWebp;
99
    }
100
101
    /**
102
     * Tests if an asset is an animated gif.
103
     */
104 1
    public static function isAnimatedGif(Asset $asset): bool
105
    {
106
        // an animated gif contains multiple "frames", with each frame having a header made up of:
107
        // * a static 4-byte sequence (\x00\x21\xF9\x04)
108
        // * 4 variable bytes
109
        // * a static 2-byte sequence (\x00\x2C)
110 1
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
111
112 1
        return $count > 1;
113
    }
114
}
115