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

Cecilapp / Cecil / 5045835811

pending completion
5045835811

Pull #1697

github

GitHub
Merge c3e3c7443 into a16355c73
Pull Request #1697: perf: native_function_invocation

321 of 321 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

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

91.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
        // adds original 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
     * Returns the value of the "sizes" attribute corresponding to the configured class.
54
     */
55
    public static function getSizes(string $class, array $sizes = []): string
56
    {
57
        $result = '';
1✔
58
        $classArray = explode(' ', $class);
1✔
59
        foreach ($classArray as $class) {
1✔
60
            if (\array_key_exists($class, $sizes)) {
1✔
61
                $result = $sizes[$class] . ', ';
1✔
62
            }
63
        }
64
        if (!empty($result)) {
1✔
65
            return trim($result, ', ');
1✔
66
        }
67

68
        return $sizes['default'] ?? '100vw';
1✔
69
    }
70

71
    /**
72
     * 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
        // 2. 4 variable bytes
79
        // 3. 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']);
1✔
81

82
        return $count > 1;
1✔
83
    }
84

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

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

102
        return $color;
1✔
103
    }
104

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

116
        $assetLqip = clone $asset;
1✔
117
        $assetLqip = $assetLqip->resize(100);
1✔
118
        return (string) ImageManager::make($assetLqip['content'])->blur(50)->encode('data-url');
1✔
119
    }
120
}
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

© 2025 Coveralls, Inc