Test Failed
Pull Request — master (#1664)
by Arnaud
08:39 queued 04:04
created

Image   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 37
dl 0
loc 99
ccs 32
cts 35
cp 0.9143
rs 10
c 1
b 1
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSrcset() 0 23 6
A isAnimatedGif() 0 9 1
A getDominantColor() 0 13 2
A getSizes() 0 13 4
A getLqip() 0 9 2
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 a Low Quality Image Placeholder (LQIP) as data URL.
88
     *
89 1
     * @throws RuntimeException
90
     */
91 1
    public static function getLqip(Asset $asset): string
92 1
    {
93 1
        if ($asset['type'] !== 'image') {
94 1
            throw new RuntimeException(\sprintf('can\'t create LQIP of "%s": it\'s not an image file.', $asset['path']));
95
        }
96
97 1
        $assetLqip = clone $asset;
98 1
        $assetLqip = $assetLqip->resize(100);
99
        return (string) ImageManager::make($assetLqip['content'])->blur(50)->encode('data-url');
100
    }
101
102
    /**
103
     * Returns the value of "sizes" corresponding to the configured class.
104
     */
105
    public static function getSizes(string $class, array $config): string
106
    {
107
        $classArray = explode(' ', $class);
108
        foreach ($classArray as $class) {
109
            if (array_key_exists($class, $config)) {
110
                $result = $config[$class] . ', ';
111
            }
112
        }
113
        if (!empty($result)) {
114
            return trim($result, ', ');
115
        }
116
117
        return $config['default'];
118
    }
119
}
120