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

Cecilapp / Cecil / 6254695617

20 Sep 2023 10:20PM UTC coverage: 82.303% (-0.07%) from 82.368%
6254695617

push

github

ArnaudLigny
fix: coveralls download link

2823 of 3430 relevant lines covered (82.3%)

0.82 hits per line

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

91.43
/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
        foreach ($widths as $width) {
1✔
35
            if ($asset['width'] < $width) {
1✔
36
                break;
1✔
37
            }
38
            $img = $asset->resize($width);
1✔
39
            $srcset .= sprintf('%s %sw, ', (string) $img, $width);
1✔
40
        }
41
        rtrim($srcset, ', ');
1✔
42
        // adds source image?
43
        if (!empty($srcset) && ($asset['width'] < max($widths))) {
1✔
44
            $srcset .= sprintf('%s %sw', (string) $asset, $asset['width']);
1✔
45
        }
46

47
        return $srcset;
1✔
48
    }
49

50
    /**
51
     * Returns the value of the "sizes" attribute corresponding to the configured class.
52
     */
53
    public static function getSizes(string $class, array $sizes = []): string
54
    {
55
        $result = '';
1✔
56
        $classArray = explode(' ', $class);
1✔
57
        foreach ($classArray as $class) {
1✔
58
            if (\array_key_exists($class, $sizes)) {
1✔
59
                $result = $sizes[$class] . ', ';
1✔
60
            }
61
        }
62
        if (!empty($result)) {
1✔
63
            return trim($result, ', ');
1✔
64
        }
65

66
        return $sizes['default'] ?? '100vw';
1✔
67
    }
68

69
    /**
70
     * Checks if an asset is an animated GIF.
71
     */
72
    public static function isAnimatedGif(Asset $asset): bool
73
    {
74
        // an animated GIF contains multiple "frames", with each frame having a header made up of:
75
        // 1. a static 4-byte sequence (\x00\x21\xF9\x04)
76
        // 2. 4 variable bytes
77
        // 3. a static 2-byte sequence (\x00\x2C)
78
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
1✔
79

80
        return $count > 1;
1✔
81
    }
82

83
    /**
84
     * Returns the dominant hexadecimal color of an image asset.
85
     *
86
     * @throws RuntimeException
87
     */
88
    public static function getDominantColor(Asset $asset): string
89
    {
90
        if ($asset['type'] !== 'image') {
1✔
91
            throw new RuntimeException(sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path']));
×
92
        }
93

94
        $assetColor = clone $asset;
1✔
95
        $assetColor = $assetColor->resize(100);
1✔
96
        $img = ImageManager::make($assetColor['content']);
1✔
97
        $color = $img->limitColors(1)->pickColor(0, 0, 'hex');
1✔
98
        $img->destroy();
1✔
99

100
        return $color;
1✔
101
    }
102

103
    /**
104
     * Returns a Low Quality Image Placeholder (LQIP) as data URL.
105
     *
106
     * @throws RuntimeException
107
     */
108
    public static function getLqip(Asset $asset): string
109
    {
110
        if ($asset['type'] !== 'image') {
1✔
111
            throw new RuntimeException(sprintf('can\'t create LQIP of "%s": it\'s not an image file.', $asset['path']));
×
112
        }
113

114
        $assetLqip = clone $asset;
1✔
115
        $assetLqip = $assetLqip->resize(100);
1✔
116

117
        return (string) ImageManager::make($assetLqip['content'])->blur(50)->encode('data-url');
1✔
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