Passed
Push — master ( 49b6d3...2fceaa )
by Arnaud
05:36
created

Image   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 92.68%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 37
c 2
b 2
f 0
dl 0
loc 100
ccs 38
cts 41
cp 0.9268
rs 10
wmc 16

5 Methods

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