• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 4629580332

pending completion
4629580332

Pull #1664

github

GitHub
Merge 82f7901ba into 6c3583161
Pull Request #1664: feat: image LQIP filter

9 of 9 new or added lines in 2 files covered. (100.0%)

2787 of 4117 relevant lines covered (67.69%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/Assets/Image.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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') {
1✔
30
            throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path']));
×
31
        }
32

33
        $srcset = '';
1✔
34
        $widthMax = 0;
1✔
35
        foreach ($widths as $width) {
1✔
36
            if ($asset['width'] < $width) {
1✔
37
                break;
1✔
38
            }
39
            $img = $asset->resize($width);
1✔
40
            $srcset .= \sprintf('%s %sw, ', (string) $img, $width);
1✔
41
            $widthMax = $width;
1✔
42
        }
43
        rtrim($srcset, ', ');
1✔
44
        // add reference image
45
        if (!empty($srcset) && ($asset['width'] != $widthMax)) {
1✔
46
            $srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']);
1✔
47
        }
48

49
        return $srcset;
1✔
50
    }
51

52
    /**
53
     * Checks if an asset is an animated gif.
54
     */
55
    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
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
1✔
62

63
        return $count > 1;
1✔
64
    }
65

66
    /**
67
     * Returns the dominant hex color of an image asset.
68
     *
69
     * @throws RuntimeException
70
     */
71
    public static function getDominantColor(Asset $asset): string
72
    {
73
        if ($asset['type'] !== 'image') {
1✔
74
            throw new RuntimeException(\sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
×
75
        }
76

77
        $assetColor = clone $asset;
1✔
78
        $assetColor = $assetColor->resize(100);
1✔
79
        $image = ImageManager::make($assetColor['content']);
1✔
80
        $color = $image->limitColors(1)->pickColor(0, 0, 'hex');
1✔
81
        $image->destroy();
1✔
82

83
        return $color;
1✔
84
    }
85

86
    /**
87
     * Returns a Low Quality Image Placeholder (LQIP) as data URL.
88
     *
89
     * @throws RuntimeException
90
     */
91
    public static function getLqip(Asset $asset): string
92
    {
93
        if ($asset['type'] !== 'image') {
1✔
94
            throw new RuntimeException(\sprintf('can\'t create LQIP of "%s": it\'s not an image file.', $asset['path']));
×
95
        }
96

97
        $assetLqip = clone $asset;
1✔
98
        $assetLqip = $assetLqip->resize(100);
1✔
99
        return (string) ImageManager::make($assetLqip['content'])->blur(50)->interlace()->encode('data-url');
1✔
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);
1✔
108
        foreach ($classArray as $class) {
1✔
109
            if (array_key_exists($class, $config)) {
1✔
110
                $result = $config[$class] . ', ';
1✔
111
            }
112
        }
113
        if (!empty($result)) {
1✔
114
            return trim($result, ', ');
1✔
115
        }
116

117
        return $config['default'];
×
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc