Passed
Push — master ( 91d443...3f6036 )
by Arnaud
05:46
created

Image::buildSrcset()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.0163

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 12
c 1
b 1
f 0
nc 7
nop 2
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
crap 6.0163
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\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
        foreach ($widths as $width) {
35 1
            if ($asset['width'] < $width) {
36 1
                break;
37
            }
38 1
            $img = $asset->resize($width);
39 1
            $srcset .= sprintf('%s %sw, ', (string) $img, $width);
40
        }
41 1
        rtrim($srcset, ', ');
42
        // adds source image?
43 1
        if (!empty($srcset) && ($asset['width'] < max($widths))) {
44 1
            $srcset .= sprintf('%s %sw', (string) $asset, $asset['width']);
45
        }
46
47 1
        return $srcset;
48
    }
49
50
    /**
51
     * Returns the value of the "sizes" attribute corresponding to the configured class.
52
     */
53 1
    public static function getSizes(string $class, array $sizes = []): string
54
    {
55 1
        $result = '';
56 1
        $classArray = explode(' ', $class);
57 1
        foreach ($classArray as $class) {
58 1
            if (\array_key_exists($class, $sizes)) {
59 1
                $result = $sizes[$class] . ', ';
60
            }
61
        }
62 1
        if (!empty($result)) {
63 1
            return trim($result, ', ');
64
        }
65
66 1
        return $sizes['default'] ?? '100vw';
67
    }
68
69
    /**
70
     * Checks if an asset is an animated GIF.
71
     */
72 1
    public static function isAnimatedGif(Asset $asset): bool
73
    {
74
        // an animated GIF contains multiple "frames", with each frame having a header made up of:
75
        // 1. a static 4-byte sequence (\x00\x21\xF9\x04)
76
        // 2. 4 variable bytes
77
        // 3. a static 2-byte sequence (\x00\x2C)
78 1
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
79
80 1
        return $count > 1;
81
    }
82
83
    /**
84
     * Returns the dominant hexadecimal color of an image asset.
85
     *
86
     * @throws RuntimeException
87
     */
88 1
    public static function getDominantColor(Asset $asset): string
89
    {
90 1
        if ($asset['type'] !== 'image') {
91
            throw new RuntimeException(sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
92
        }
93
94 1
        $assetColor = clone $asset;
95 1
        $assetColor = $assetColor->resize(100);
96 1
        $img = ImageManager::make($assetColor['content']);
97 1
        $color = $img->limitColors(1)->pickColor(0, 0, 'hex');
98 1
        $img->destroy();
99
100 1
        return $color;
101
    }
102
103
    /**
104
     * Returns a Low Quality Image Placeholder (LQIP) as data URL.
105
     *
106
     * @throws RuntimeException
107
     */
108 1
    public static function getLqip(Asset $asset): string
109
    {
110 1
        if ($asset['type'] !== 'image') {
111
            throw new RuntimeException(sprintf('can\'t create LQIP of "%s": it\'s not an image file.', $asset['path']));
112
        }
113
114 1
        $assetLqip = clone $asset;
115 1
        $assetLqip = $assetLqip->resize(100);
116
117 1
        return (string) ImageManager::make($assetLqip['content'])->blur(50)->encode('data-url');
118
    }
119
}
120