Passed
Push — toc-url ( 47c560...bce3ef )
by Arnaud
10:27 queued 05:58
created

Image   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 67.44%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 39
dl 0
loc 102
ccs 29
cts 43
cp 0.6744
rs 10
c 1
b 1
f 0
wmc 14

5 Methods

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