Passed
Push — images-optim ( b62e9b )
by Arnaud
12:46 queued 09:01
created

Image::optimizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 1
dl 0
loc 30
ccs 10
cts 10
cp 1
crap 1
rs 9.552
c 0
b 0
f 0
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
        $assetSrcset = clone $asset;
34 1
        $srcset = '';
35 1
        foreach ($widths as $width) {
36 1
            if ($assetSrcset->getWidth() < $width) {
37 1
                break;
38
            }
39
            $assetTmp = clone $assetSrcset;
40
            $img = $assetTmp->resize($width);
41
            $srcset .= \sprintf('%s %sw, ', (string) $img, $width);
42
            unset($assetTmp);
43
        }
44 1
        rtrim($srcset, ', ');
45
        // add reference image
46 1
        if (!empty($srcset)) {
47
            $srcset .= \sprintf('%s %sw', (string) $assetSrcset, $assetSrcset->getWidth());
48
        }
49 1
        unset($assetSrcset);
50
51 1
        return $srcset;
52
    }
53
54
    /**
55
     * Converts an image asset to WebP format.
56
     *
57
     * @throws RuntimeException
58
     */
59 1
    public static function convertTopWebp(Asset $asset, int $quality): Asset
60
    {
61 1
        if ($asset['type'] !== 'image') {
62 1
            throw new RuntimeException(\sprintf('can\'t convert "%s" (%s) to WebP: it\'s not an image file.', $asset['path'], $asset['type']));
63
        }
64
65 1
        $assetWebp = clone $asset;
66 1
        $format = 'webp';
67 1
        $image = ImageManager::make($assetWebp['content']);
68 1
        $assetWebp['content'] = (string) $image->encode($format, $quality);
69
        $assetWebp['path'] = preg_replace('/\.'.$asset['ext'].'$/m', ".$format", $asset['path']);
70
        $assetWebp['ext'] = $format;
71
72
        return $assetWebp;
73
    }
74
75
    /**
76
     * Checks if an asset is an animated gif.
77
     */
78 1
    public static function isAnimatedGif(Asset $asset): bool
79
    {
80
        // an animated gif contains multiple "frames", with each frame having a header made up of:
81
        // * a static 4-byte sequence (\x00\x21\xF9\x04)
82
        // * 4 variable bytes
83
        // * a static 2-byte sequence (\x00\x2C)
84 1
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
85
86 1
        return $count > 1;
87
    }
88
89
    /**
90
     * Returns the dominant hex color of an image asset.
91
     *
92
     * @throws RuntimeException
93
     */
94 1
    public static function getDominantColor(Asset $asset): string
95
    {
96 1
        if ($asset['type'] !== 'image') {
97
            throw new RuntimeException(\sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
98
        }
99
100 1
        $assetColor = clone $asset;
101 1
        $assetColor = $assetColor->resize(100);
102 1
        $image = ImageManager::make($assetColor['content']);
103 1
        $color = $image->limitColors(1)->pickColor(0, 0, 'hex');
104 1
        $image->destroy();
105
106 1
        return $color;
107
    }
108
}
109