Passed
Push — master ( 050960...df4a53 )
by Arnaud
05:06
created

Image::convertTopWebp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 16
ccs 6
cts 12
cp 0.5
crap 2.5
rs 9.9
c 0
b 0
f 0
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\Exception\RuntimeException;
17
use Intervention\Image\ImageManagerStatic as ImageManager;
18
19
class Image
20
{
21
    /**
22
     * Build the `srcset` attribute for responsive images.
23
     * e.g.: srcset="/img-480.jpg 480w, /img-800.jpg 800w".
24
     *
25
     * @throws RuntimeException
26
     */
27 1
    public static function buildSrcset(Asset $asset, array $widths): string
28
    {
29 1
        if ($asset['type'] !== 'image') {
30
            throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path']));
31
        }
32
33 1
        $srcset = '';
34 1
        $widthMax = 0;
35 1
        foreach ($widths as $width) {
36 1
            if ($asset['width'] < $width) {
37 1
                break;
38
            }
39 1
            $img = $asset->resize($width);
40 1
            $srcset .= \sprintf('%s %sw, ', (string) $img, $width);
41 1
            $widthMax = $width;
42
        }
43 1
        rtrim($srcset, ', ');
44
        // add reference image
45 1
        if (!empty($srcset) && ($asset['width'] != $widthMax)) {
46 1
            $srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']);
47
        }
48
49 1
        return $srcset;
50
    }
51
52
    /**
53
     * Checks if an asset is an animated gif.
54
     */
55 1
    public static function isAnimatedGif(Asset $asset): bool
56
    {
57
        // an animated gif contains multiple "frames", with each frame having a header made up of:
58
        // * a static 4-byte sequence (\x00\x21\xF9\x04)
59
        // * 4 variable bytes
60
        // * a static 2-byte sequence (\x00\x2C)
61 1
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
62
63 1
        return $count > 1;
64
    }
65
66
    /**
67
     * Returns the dominant hex color of an image asset.
68
     *
69
     * @throws RuntimeException
70
     */
71 1
    public static function getDominantColor(Asset $asset): string
72
    {
73 1
        if ($asset['type'] !== 'image') {
74
            throw new RuntimeException(\sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
75
        }
76
77 1
        $assetColor = clone $asset;
78 1
        $assetColor = $assetColor->resize(100);
79 1
        $image = ImageManager::make($assetColor['content']);
80 1
        $color = $image->limitColors(1)->pickColor(0, 0, 'hex');
81 1
        $image->destroy();
82
83 1
        return $color;
84
    }
85
86
    /**
87
     * Returns the value of "sizes" corresponding to the configured class.
88
     */
89 1
    public static function getSizes(string $class, array $config): string
90
    {
91 1
        $classArray = explode(' ', $class);
92 1
        foreach ($classArray as $class) {
93 1
            if (array_key_exists($class, $config)) {
94 1
                $result = $config[$class] . ', ';
95
            }
96
        }
97 1
        if (!empty($result)) {
98 1
            return trim($result, ', ');
99
        }
100
101
        return $config['default'];
102
    }
103
}
104