Passed
Push — converter ( 50b3ae...1a12d8 )
by Arnaud
03:16
created

Image::getSizes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 3
rs 10
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
    public static function buildSrcset(Asset $asset, array $widths): string
28
    {
29
        if ($asset['type'] !== 'image') {
30 1
            throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path']));
31
        }
32 1
33 1
        $srcset = '';
34 1
        foreach ($widths as $width) {
35
            if ($asset->getWidth() < $width) {
36
                break;
37
            }
38 1
            $img = $asset->resize($width);
39 1
            $srcset .= \sprintf('%s %sw, ', (string) $img, $width);
40
        }
41
        rtrim($srcset, ', ');
42
        // add reference image
43 1
        if (!empty($srcset)) {
44
            $srcset .= \sprintf('%s %sw', (string) $asset, $asset->getWidth());
45
        }
46
47
        return $srcset;
48 1
    }
49
50
    /**
51 1
     * 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
        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
        $assetWebp = clone $asset;
62
        $format = 'webp';
63
        $image = ImageManager::make($assetWebp['content']);
64
        $assetWebp['content'] = (string) $image->encode($format, $quality);
65
        $assetWebp['path'] = preg_replace('/\.'.$asset['ext'].'$/m', ".$format", $asset['path']);
66
        $assetWebp['ext'] = $format;
67 1
68
        return $assetWebp;
69 1
    }
70 1
71 1
    /**
72 1
     * Checks if an asset is an animated gif.
73
     */
74
    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 1
        // * a static 4-byte sequence (\x00\x21\xF9\x04)
78
        // * 4 variable bytes
79 1
        // * a static 2-byte sequence (\x00\x2C)
80
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
81
82
        return $count > 1;
83 1
    }
84
85
    /**
86
     * Returns the dominant hex color of an image asset.
87
     *
88
     * @throws RuntimeException
89 1
     */
90
    public static function getDominantColor(Asset $asset): string
91 1
    {
92 1
        if ($asset['type'] !== 'image') {
93 1
            throw new RuntimeException(\sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
94 1
        }
95
96
        $assetColor = clone $asset;
97
        $assetColor = $assetColor->resize(100);
98
        $image = ImageManager::make($assetColor['content']);
99
        $color = $image->limitColors(1)->pickColor(0, 0, 'hex');
100
        $image->destroy();
101
102
        return $color;
103
    }
104 1
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 1
        $classArray = explode(' ', $class);
111
        foreach ($classArray as $class) {
112 1
            if (array_key_exists($class, $config)) {
113
                $result = $config[$class].', ';
114
            }
115
        }
116
117
        return trim($result, ', ');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
118
    }
119
}
120