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

Cecilapp / Cecil / 9993477550

18 Jul 2024 02:32PM UTC coverage: 83.537% (-0.1%) from 83.645%
9993477550

Pull #2013

github

web-flow
Merge 9c9f330f3 into d168a7498
Pull Request #2013: refactor: migration to Intervention/Image v3

13 of 16 new or added lines in 1 file covered. (81.25%)

8 existing lines in 1 file now uncovered.

2943 of 3523 relevant lines covered (83.54%)

0.84 hits per line

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

69.57
/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\Encoders\AutoEncoder;
18
use Intervention\Image\ImageManager;
19

20
class Image
21
{
22
    /**
23
     * Create new manager instance with desired driver.
24
     */
25
    private static function manager(): ImageManager
26
    {
27
        if (\extension_loaded('gd') && \function_exists('gd_info')) {
1✔
28
            return ImageManager::gd();
1✔
29
        }
NEW
30
        if (\extension_loaded('imagick') && class_exists('Imagick')) {
×
NEW
31
            return ImageManager::imagick();
×
32
        }
33

NEW
34
        throw new RuntimeException('PHP GD extension is required.');
×
35
    }
36

37
    /**
38
     * Resize an image Asset.
39
     *
40
     * @throws RuntimeException
41
     */
42
    public static function resize(Asset $asset, int $width, int $quality): string
43
    {
44
        try {
45
            // is image Asset?
46
            if ($asset['type'] !== 'image') {
1✔
47
                throw new RuntimeException(sprintf('Not an image.'));
×
48
            }
49
            // creates image object from source
50
            $image = self::manager()->read($asset['content_source']);
1✔
51
            // resizes to $width with constraint the aspect-ratio and unwanted upsizing
52
            $image->scaleDown(width: $width);
1✔
53
            // return image data
54
            return (string)$image->encodeByMediaType($asset['subtype'], progressive: true, interlaced: true, quality: $quality);
1✔
55
        } catch (\Exception $e) {
×
56
            throw new RuntimeException(sprintf('Not able to resize "%s": %s', $asset['path'], $e->getMessage()));
×
57
        }
58
    }
59

60
    /**
61
     * Converts an image Asset to the target format.
62
     *
63
     * @throws RuntimeException
64
     */
65
    public static function convert(Asset $asset, string $format, int $quality): string
66
    {
67
        try {
68
            if ($asset['type'] !== 'image') {
1✔
69
                throw new RuntimeException(sprintf('Not an image.'));
×
70
            }
71
            $image = self::manager()->read($asset['content']);
1✔
72

73
            return (string)$image->encodeByExtension($format, progressive: true, interlaced: true, quality: $quality);
1✔
74
        } catch (\Exception $e) {
×
75
            throw new RuntimeException(sprintf('Not able to convert "%s": %s', $asset['path'], $e->getMessage()));
×
76
        }
77
    }
78

79
    /**
80
     * Returns the Data URL (encoded in Base64).
81
     *
82
     * @throws RuntimeException
83
     */
84
    public static function getDataUrl(Asset $asset, int $quality): string
85
    {
86
        try {
87
            if ($asset['type'] != 'image' || self::isSVG($asset)) {
1✔
UNCOV
88
                throw new RuntimeException(sprintf('Not an image.'));
×
89
            }
90
            $image = self::manager()->read($asset['content']);
1✔
91

92
            return (string) $image->encode(new AutoEncoder(quality: $quality))->toDataUri();
1✔
UNCOV
93
        } catch (\Exception $e) {
×
94
            throw new RuntimeException(sprintf('Can\'t get Data URL of "%s": %s', $asset['path'], $e->getMessage()));
×
95
        }
96
    }
97

98
    /**
99
     * Returns the dominant hexadecimal color of an image asset.
100
     *
101
     * @throws RuntimeException
102
     */
103
    public static function getDominantColor(Asset $asset): string
104
    {
105
        try {
106
            if ($asset['type'] != 'image' || self::isSVG($asset)) {
1✔
UNCOV
107
                throw new RuntimeException(sprintf('Not an image.'));
×
108
            }
109
            $assetColor = clone $asset;
1✔
110
            $assetColor = $assetColor->resize(100);
1✔
111
            $image = self::manager()->read($assetColor['content']);
1✔
112

113
            return $image->reduceColors(1)->pickColor(0, 0)->toHex();
1✔
UNCOV
114
        } catch (\Exception $e) {
×
115
            throw new RuntimeException(sprintf('Can\'t get dominant color of "%s": %s', $asset['path'], $e->getMessage()));
×
116
        }
117
    }
118

119
    /**
120
     * Returns a Low Quality Image Placeholder (LQIP) as data URL.
121
     *
122
     * @throws RuntimeException
123
     */
124
    public static function getLqip(Asset $asset): string
125
    {
126
        try {
127
            if ($asset['type'] !== 'image') {
1✔
UNCOV
128
                throw new RuntimeException(sprintf('Not an image.'));
×
129
            }
130
            $assetLqip = clone $asset;
1✔
131
            $assetLqip = $assetLqip->resize(100);
1✔
132
            $image = self::manager()->read($assetLqip['content']);
1✔
133

134
            return (string) $image->blur(50)->encode()->toDataUri();
1✔
UNCOV
135
        } catch (\Exception $e) {
×
136
            throw new RuntimeException(sprintf('can\'t create LQIP of "%s": %s', $asset['path'], $e->getMessage()));
×
137
        }
138
    }
139

140
    /**
141
     * Build the `srcset` attribute for responsive images.
142
     * e.g.: `srcset="/img-480.jpg 480w, /img-800.jpg 800w"`.
143
     *
144
     * @throws RuntimeException
145
     */
146
    public static function buildSrcset(Asset $asset, array $widths): string
147
    {
148
        if ($asset['type'] !== 'image') {
1✔
UNCOV
149
            throw new RuntimeException(sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path']));
×
150
        }
151

152
        $srcset = '';
1✔
153
        $widthMax = 0;
1✔
154
        foreach ($widths as $width) {
1✔
155
            if ($asset['width'] < $width) {
1✔
156
                break;
1✔
157
            }
158
            $img = $asset->resize($width);
1✔
159
            $srcset .= sprintf('%s %sw, ', (string) $img, $width);
1✔
160
            $widthMax = $width;
1✔
161
        }
162
        // adds source image
163
        if (!empty($srcset) && ($asset['width'] < max($widths) && $asset['width'] != $widthMax)) {
1✔
164
            $srcset .= sprintf('%s %sw', (string) $asset, $asset['width']);
1✔
165
        }
166

167
        return rtrim($srcset, ', ');
1✔
168
    }
169

170
    /**
171
     * Returns the value of the "sizes" attribute corresponding to the configured class.
172
     */
173
    public static function getSizes(string $class, array $sizes = []): string
174
    {
175
        $result = '';
1✔
176
        $classArray = explode(' ', $class);
1✔
177
        foreach ($classArray as $class) {
1✔
178
            if (\array_key_exists($class, $sizes)) {
1✔
179
                $result = $sizes[$class] . ', ';
1✔
180
            }
181
        }
182
        if (!empty($result)) {
1✔
183
            return trim($result, ', ');
1✔
184
        }
185

186
        return $sizes['default'] ?? '100vw';
1✔
187
    }
188

189
    /**
190
     * Checks if an asset is an animated GIF.
191
     */
192
    public static function isAnimatedGif(Asset $asset): bool
193
    {
194
        // an animated GIF contains multiple "frames", with each frame having a header made up of:
195
        // 1. a static 4-byte sequence (\x00\x21\xF9\x04)
196
        // 2. 4 variable bytes
197
        // 3. a static 2-byte sequence (\x00\x2C)
198
        $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']);
1✔
199

200
        return $count > 1;
1✔
201
    }
202

203
    /**
204
     * Returns true if asset is a SVG.
205
     */
206
    public static function isSVG(Asset $asset): bool
207
    {
208
        return \in_array($asset['subtype'], ['image/svg', 'image/svg+xml']) || $asset['ext'] == 'svg';
1✔
209
    }
210

211
    /**
212
     * Returns SVG attributes.
213
     *
214
     * @return \SimpleXMLElement|false
215
     */
216
    public static function getSvgAttributes(Asset $asset)
217
    {
218
        if (!self::isSVG($asset)) {
1✔
UNCOV
219
            return false;
×
220
        }
221

222
        if (false === $xml = simplexml_load_string($asset['content_source'] ?? '')) {
1✔
223
            return false;
×
224
        }
225

226
        return $xml->attributes();
1✔
227
    }
228
}
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