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

Cecilapp / Cecil / 26407566224

25 May 2026 03:18PM UTC coverage: 82.54%. First build
26407566224

Pull #2383

github

web-flow
Merge ddfaf03b1 into 88004e568
Pull Request #2383: refactor: asset handling and add renderer extensions

282 of 358 new or added lines in 10 files covered. (78.77%)

3503 of 4244 relevant lines covered (82.54%)

0.83 hits per line

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

78.93
/src/Asset.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Cecil;
15

16
use Cecil\Asset\Compiler as AssetCompiler;
17
use Cecil\Asset\Image;
18
use Cecil\Asset\Locator;
19
use Cecil\Asset\Optimizer as AssetOptimizer;
20
use Cecil\Builder;
21
use Cecil\Cache;
22
use Cecil\Config;
23
use Cecil\Exception\RuntimeException;
24
use Cecil\Url;
25
use Cecil\Util;
26
use wapmorgan\Mp3Info\Mp3Info;
27

28
/**
29
 * Asset class.
30
 *
31
 * Represents an asset (file) in the Cecil project.
32
 * Handles file locating, content reading, compiling, minifying, fingerprinting,
33
 * resizing images, and more.
34
 */
35
class Asset implements \ArrayAccess
36
{
37
    public const IMAGE_THUMB = 'thumbnails';
38

39
    /** @var Builder */
40
    protected $builder;
41

42
    /** @var Config */
43
    protected $config;
44

45
    /** @var array */
46
    protected $data = [];
47

48
    /** @var array Cache tags */
49
    protected $cacheTags = [];
50

51
    /**
52
     * Creates an Asset from a file path, an array of files path or an URL.
53
     * Options:
54
     * [
55
     *     'filename' => <string>,
56
     *     'ignore_missing' => <bool>,
57
     *     'fingerprint' => <bool>,
58
     *     'minify' => <bool>,
59
     *     'optimize' => <bool>,
60
     *     'fallback' => <string>,
61
     *     'useragent' => <string>,
62
     *     'language' => <string|null>,
63
     * ]
64
     *
65
     * @param Builder      $builder
66
     * @param string|array $paths
67
     * @param array|null   $options
68
     *
69
     * @throws RuntimeException
70
     */
71
    public function __construct(Builder $builder, string|array $paths, array|null $options = null)
72
    {
73
        $this->builder = $builder;
1✔
74
        $this->config = $builder->getConfig();
1✔
75
        $paths = \is_array($paths) ? $paths : [$paths];
1✔
76
        // checks path(s)
77
        array_walk($paths, function ($path) {
1✔
78
            // must be a string
79
            if (!\is_string($path)) {
1✔
80
                throw new RuntimeException(\sprintf('The path of an asset must be a string ("%s" given).', \gettype($path)));
×
81
            }
82
            // can't be empty
83
            if (empty($path)) {
1✔
84
                throw new RuntimeException('The path of an asset can\'t be empty.');
×
85
            }
86
            // can't be relative
87
            if (substr($path, 0, 2) == '..') {
1✔
88
                throw new RuntimeException(\sprintf('The path of asset "%s" is wrong: it must be directly relative to `assets` or `static` directory, or a remote URL.', $path));
×
89
            }
90
        });
1✔
91
        $this->data = [
1✔
92
            'file'     => '',    // absolute file path
1✔
93
            'files'    => [],    // array of absolute files path
1✔
94
            'missing'  => false, // if file not found but missing allowed: 'missing' is true
1✔
95
            '_path'    => '',    // original path
1✔
96
            'path'     => '',    // public path
1✔
97
            'url'      => null,  // URL if it's a remote file
1✔
98
            'ext'      => '',    // file extension
1✔
99
            'type'     => '',    // file type (e.g.: image, audio, video, etc.)
1✔
100
            'subtype'  => '',    // file media type (e.g.: image/png, audio/mp3, etc.)
1✔
101
            'size'     => 0,     // file size (in bytes)
1✔
102
            'width'    => null,  // width (in pixels)
1✔
103
            'height'   => null,  // height (in pixels)
1✔
104
            'exif'     => [],    // image exif data
1✔
105
            'duration' => null,  // audio or video duration
1✔
106
            'content'  => '',    // file content
1✔
107
            'hash'     => '',    // file content hash
1✔
108
        ];
1✔
109

110
        // handles options
111
        $options = array_merge(
1✔
112
            [
1✔
113
                'filename'       => '',
1✔
114
                'ignore_missing' => false,
1✔
115
                'fingerprint'    => $this->config->isEnabled('assets.fingerprint'),
1✔
116
                'minify'         => $this->config->isEnabled('assets.minify'),
1✔
117
                'optimize'       => $this->config->isEnabled('assets.images.optimize'),
1✔
118
                'fallback'       => '',
1✔
119
                'useragent'      => (string) $this->config->get('assets.remote.useragent.default'),
1✔
120
                'language'       => null,
1✔
121
            ],
1✔
122
            \is_array($options) ? $options : []
1✔
123
        );
1✔
124
        $language = null;
1✔
125
        if (isset($options['language']) && \is_scalar($options['language'])) {
1✔
126
            $language = (string) $options['language'];
1✔
127
            $language = $language === '' ? null : $language;
1✔
128
        }
129
        unset($options['language']);
1✔
130

131
        // cache for "locate file(s)"
132
        $cache = new Cache($this->builder, 'assets');
1✔
133
        $locateCacheKey = \sprintf(
1✔
134
            '%s_locate%s__%s__%s',
1✔
135
            $options['filename'] ?: implode('_', $paths),
1✔
136
            $language ? '_' . $language : '',
1✔
137
            Builder::getBuildId(),
1✔
138
            Builder::getVersion()
1✔
139
        );
1✔
140

141
        // locate file(s) and get content
142
        if (!$cache->has($locateCacheKey)) {
1✔
143
            $pathsCount = \count($paths);
1✔
144
            for ($i = 0; $i < $pathsCount; $i++) {
1✔
145
                try {
146
                    $this->data['missing'] = false;
1✔
147
                    $locate = (new Locator($this->builder))->locate(
1✔
148
                        $paths[$i],
1✔
149
                        $options['fallback'],
1✔
150
                        $options['useragent'],
1✔
151
                        $language
1✔
152
                    );
1✔
153
                    $file = $locate['file'];
1✔
154
                    $path = $locate['path'];
1✔
155
                    $type = Util\File::getMediaType($file)[0];
1✔
156
                    if ($i > 0) { // bundle
1✔
157
                        if ($type != $this->data['type']) {
1✔
158
                            throw new RuntimeException(\sprintf('Asset bundle type error (%s != %s).', $type, $this->data['type']));
×
159
                        }
160
                    }
161
                    $this->data['file'] = $file;
1✔
162
                    $this->data['files'][] = $file;
1✔
163
                    $this->data['path'] = $path;
1✔
164
                    $this->data['url'] = Util\File::isRemote($paths[$i]) ? $paths[$i] : null;
1✔
165
                    $this->data['ext'] = Util\File::getExtension($file);
1✔
166
                    $this->data['type'] = $type;
1✔
167
                    $this->data['subtype'] = Util\File::getMediaType($file)[1];
1✔
168
                    $this->data['size'] += filesize($file) ?: 0;
1✔
169
                    $this->data['content'] .= Util\File::fileGetContents($file);
1✔
170
                    $this->data['hash'] = hash('xxh128', $this->data['content']);
1✔
171
                    // bundle default filename
172
                    $filename = $options['filename'];
1✔
173
                    if ($pathsCount > 1 && empty($filename)) {
1✔
174
                        switch ($this->data['ext']) {
1✔
175
                            case 'scss':
1✔
176
                            case 'css':
1✔
177
                                $filename = 'styles.css';
1✔
178
                                break;
1✔
179
                            case 'js':
1✔
180
                                $filename = 'scripts.js';
1✔
181
                                break;
1✔
182
                            default:
183
                                throw new RuntimeException(\sprintf('Asset bundle supports %s files only.', '.scss, .css and .js'));
×
184
                        }
185
                    }
186
                    // apply bundle filename to path
187
                    if (!empty($filename)) {
1✔
188
                        $this->data['path'] = $filename;
1✔
189
                    }
190
                    // add leading slash
191
                    $this->data['path'] = '/' . ltrim($this->data['path'], '/');
1✔
192
                    $this->data['_path'] = $this->data['path'];
1✔
193
                } catch (RuntimeException $e) {
1✔
194
                    if ($options['ignore_missing']) {
1✔
195
                        $this->data['missing'] = true;
1✔
196
                        continue;
1✔
197
                    }
198
                    throw new RuntimeException(\sprintf('Unable to handle asset "%s".', $paths[$i]), previous: $e);
×
199
                }
200
            }
201
            $cache->set($locateCacheKey, $this->data);
1✔
202
        }
203
        $this->data = $cache->get($locateCacheKey);
1✔
204

205
        // missing
206
        if ($this->isMissing()) {
1✔
207
            return;
1✔
208
        }
209

210
        // cache for "process asset"
211
        $cache = new Cache($this->builder, 'assets');
1✔
212
        // create cache tags from options
213
        $this->cacheTags = $options;
1✔
214
        // remove unnecessary cache tags
215
        unset($this->cacheTags['optimize'], $this->cacheTags['ignore_missing'], $this->cacheTags['fallback'], $this->cacheTags['useragent']);
1✔
216
        if (!\in_array($this->data['ext'], ['css', 'js', 'scss'])) {
1✔
217
            unset($this->cacheTags['minify']);
1✔
218
        }
219
        // optimize image?
220
        $optimize = false;
1✔
221
        if ($options['optimize'] && $this->data['type'] == 'image' && !$this->isImageInCdn()) {
1✔
222
            $optimize = true;
1✔
223
            $quality = (int) $this->config->get('assets.images.quality');
1✔
224
            $this->cacheTags['quality'] = $quality;
1✔
225
        }
226
        $cacheKey = $cache->createKey($this, tags: $this->cacheTags);
1✔
227
        if (!$cache->has($cacheKey)) {
1✔
228
            // fingerprinting
229
            if ($options['fingerprint']) {
1✔
230
                $this->doFingerprint();
×
231
            }
232
            // compiling Sass files
233
            $this->doCompile();
1✔
234
            // minifying (CSS and JavaScript files)
235
            if ($options['minify']) {
1✔
236
                $this->doMinify();
×
237
            }
238
            // get width and height
239
            $this->data['width'] = $this->getWidth();
1✔
240
            $this->data['height'] = $this->getHeight();
1✔
241
            // get image exif
242
            if ($this->data['subtype'] == 'image/jpeg') {
1✔
243
                $this->data['exif'] = Util\File::readExif($this->data['file']);
1✔
244
            }
245
            // get duration
246
            if ($this->data['type'] == 'audio') {
1✔
247
                $this->data['duration'] = $this->getAudio()['duration'];
1✔
248
            }
249
            if ($this->data['type'] == 'video') {
1✔
250
                $this->data['duration'] = $this->getVideo()['duration'];
1✔
251
            }
252
            $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
253
            $this->builder->getLogger()->debug(\sprintf('Asset cached: "%s"', $this->data['path']));
1✔
254
            // optimizing images files (in cache directory)
255
            if ($optimize) {
1✔
256
                $this->optimizeImage($cache->getContentFile($this->data['path']), $this->data['path'], $quality);
1✔
257
            }
258
        }
259
        $this->data = $cache->get($cacheKey);
1✔
260
    }
261

262
    /**
263
     * Returns path.
264
     */
265
    public function __toString(): string
266
    {
267
        $this->save();
1✔
268

269
        if ($this->isImageInCdn()) {
1✔
270
            return $this->buildImageCdnUrl();
×
271
        }
272

273
        if ($this->builder->getConfig()->isEnabled('canonicalurl')) {
1✔
274
            return (string) new Url($this->builder, $this->data['path'], ['canonical' => true]);
×
275
        }
276

277
        return $this->data['path'];
1✔
278
    }
279

280
    /**
281
     * Implements \ArrayAccess.
282
     */
283
    #[\ReturnTypeWillChange]
284
    public function offsetSet($offset, $value): void
285
    {
286
        if (!\is_null($offset)) {
1✔
287
            $this->data[$offset] = $value;
1✔
288
        }
289
    }
290

291
    /**
292
     * Implements \ArrayAccess.
293
     */
294
    #[\ReturnTypeWillChange]
295
    public function offsetExists($offset): bool
296
    {
297
        return isset($this->data[$offset]);
1✔
298
    }
299

300
    /**
301
     * Implements \ArrayAccess.
302
     */
303
    #[\ReturnTypeWillChange]
304
    public function offsetUnset($offset): void
305
    {
306
        unset($this->data[$offset]);
×
307
    }
308

309
    /**
310
     * Implements \ArrayAccess.
311
     */
312
    #[\ReturnTypeWillChange]
313
    public function offsetGet($offset)
314
    {
315
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
1✔
316
    }
317

318
    /**
319
     * Saves the asset by adding its path to the build assets list.
320
     * Skips assets marked as missing and validates that the asset file exists in cache before adding it.
321
     *
322
     * @throws RuntimeException
323
     */
324
    public function save(): void
325
    {
326
        if ($this->isMissing()) {
1✔
327
            return;
1✔
328
        }
329

330
        $cache = new Cache($this->builder, 'assets');
1✔
331
        if (empty($this->data['path']) || !Util\File::getFS()->exists($cache->getContentFile($this->data['path']))) {
1✔
332
            throw new RuntimeException(\sprintf('Unable to add "%s" to assets list. Please clear cache and retry.', $this->data['path']));
×
333
        }
334

335
        $this->builder->addToAssetsList($this->data['path']);
1✔
336
    }
337

338
    /**
339
     * Checks if the asset is missing.
340
     */
341
    public function isMissing(): bool
342
    {
343
        return $this->data['missing'];
1✔
344
    }
345

346
    /**
347
     * Add hash to the file name + cache.
348
     */
349
    public function fingerprint(): self
350
    {
351
        $this->cacheTags['fingerprint'] = true;
1✔
352
        $cache = new Cache($this->builder, 'assets');
1✔
353
        $cacheKey = $cache->createKey($this, tags: $this->cacheTags);
1✔
354
        if (!$cache->has($cacheKey)) {
1✔
355
            $this->doFingerprint();
1✔
356
            $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
357
        }
358
        $this->data = $cache->get($cacheKey);
1✔
359

360
        return $this;
1✔
361
    }
362

363
    /**
364
     * Compiles a SCSS + cache.
365
     *
366
     * @throws RuntimeException
367
     */
368
    public function compile(): self
369
    {
370
        $this->cacheTags['compile'] = true;
1✔
371
        $cache = new Cache($this->builder, 'assets');
1✔
372
        $cacheKey = $cache->createKey($this, tags: $this->cacheTags);
1✔
373
        if (!$cache->has($cacheKey)) {
1✔
374
            $this->doCompile();
1✔
375
            $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
376
        }
377
        $this->data = $cache->get($cacheKey);
1✔
378

379
        return $this;
1✔
380
    }
381

382
    /**
383
     * Minifying a CSS or a JS.
384
     */
385
    public function minify(): self
386
    {
387
        $this->cacheTags['minify'] = true;
1✔
388
        $cache = new Cache($this->builder, 'assets');
1✔
389
        $cacheKey = $cache->createKey($this, tags: $this->cacheTags);
1✔
390
        if (!$cache->has($cacheKey)) {
1✔
391
            $this->doMinify();
1✔
392
            $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
393
        }
394
        $this->data = $cache->get($cacheKey);
1✔
395

396
        return $this;
1✔
397
    }
398

399
    /**
400
     * Returns the Data URL (encoded in Base64).
401
     *
402
     * @throws RuntimeException
403
     */
404
    public function dataurl(): string
405
    {
406
        if ($this->data['type'] == 'image' && !Image::isSVG($this)) {
1✔
407
            return Image::getDataUrl($this, (int) $this->config->get('assets.images.quality'));
1✔
408
        }
409

410
        return \sprintf('data:%s;base64,%s', $this->data['subtype'], base64_encode($this->data['content']));
1✔
411
    }
412

413
    /**
414
     * Hashing content of an asset with the specified algo, sha384 by default.
415
     * Used for SRI (Subresource Integrity).
416
     *
417
     * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity
418
     */
419
    public function integrity(string $algo = 'sha384'): string
420
    {
421
        return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true)));
1✔
422
    }
423

424
    /**
425
     * Resizes an image to the given width or/and height.
426
     *
427
     * - If only the width is specified, the height is calculated to preserve the aspect ratio
428
     * - If only the height is specified, the width is calculated to preserve the aspect ratio
429
     * - If both width and height are specified, the image is resized to fit within the given dimensions, image is cropped and centered if necessary
430
     * - If rmAnimation is true, any animation in the image (e.g., GIF) will be removed.
431
     *
432
     * @throws RuntimeException
433
     */
434
    public function resize(?int $width = null, ?int $height = null, bool $rmAnimation = false): self
435
    {
436
        $this->checkImage();
1✔
437

438
        // if no width and no height, return the original image
439
        if ($width === null && $height === null) {
1✔
440
            return $this;
×
441
        }
442

443
        // if equal with and height, return the original image
444
        if ($width == $this->data['width'] && $height == $this->data['height']) {
1✔
445
            return $this;
×
446
        }
447

448
        // if the image width or height is already smaller, return the original image
449
        if ($width !== null && $this->data['width'] <= $width && $height === null) {
1✔
450
            return $this;
1✔
451
        }
452
        if ($height !== null && $this->data['height'] <= $height && $width === null) {
1✔
453
            return $this;
×
454
        }
455

456
        $assetResized = clone $this;
1✔
457
        $assetResized->data['width'] = $width ?? $this->data['width'];
1✔
458
        $assetResized->data['height'] = $height ?? $this->data['height'];
1✔
459

460
        if ($this->isImageInCdn()) {
1✔
461
            if ($width === null) {
×
462
                $assetResized->data['width'] = round($this->data['width'] / ($this->data['height'] / $height));
×
463
            }
464
            if ($height === null) {
×
465
                $assetResized->data['height'] = round($this->data['height'] / ($this->data['width'] / $width));
×
466
            }
467

468
            return $assetResized; // returns asset with the new dimensions only: CDN do the rest of the job
×
469
        }
470

471
        $quality = (int) $this->config->get('assets.images.quality');
1✔
472

473
        $cache = new Cache($this->builder, 'assets');
1✔
474
        $assetResized->cacheTags['quality'] = $quality;
1✔
475
        $assetResized->cacheTags['width'] = $width;
1✔
476
        $assetResized->cacheTags['height'] = $height;
1✔
477
        $cacheKey = $cache->createKey($assetResized, tags: $assetResized->cacheTags);
1✔
478
        if (!$cache->has($cacheKey)) {
1✔
479
            $assetResized->data['content'] = Image::resize($assetResized, $width, $height, $quality, $rmAnimation);
1✔
480
            $assetResized->data['path'] = '/' . Util::joinPath(
1✔
481
                (string) $this->config->get('assets.target'),
1✔
482
                self::IMAGE_THUMB,
1✔
483
                (string) $width . 'x' . (string) $height,
1✔
484
                $assetResized->data['path']
1✔
485
            );
1✔
486
            $assetResized->data['path'] = $this->deduplicateThumbPath($assetResized->data['path']);
1✔
487
            $assetResized->data['width'] = $assetResized->getWidth();
1✔
488
            $assetResized->data['height'] = $assetResized->getHeight();
1✔
489
            $assetResized->data['size'] = \strlen($assetResized->data['content']);
1✔
490

491
            $cache->set($cacheKey, $assetResized->data, $this->config->get('cache.assets.ttl'));
1✔
492
            $this->builder->getLogger()->debug(\sprintf('Asset resized: "%s" (%sx%s)', $assetResized->data['path'], $width, $height));
1✔
493
        }
494
        $assetResized->data = $cache->get($cacheKey);
1✔
495

496
        return $assetResized;
1✔
497
    }
498

499
    /**
500
     * Creates a maskable image (with a padding = 20%).
501
     *
502
     * @throws RuntimeException
503
     */
504
    public function maskable(?int $padding = null): self
505
    {
506
        $this->checkImage();
×
507

508
        if ($padding === null) {
×
509
            $padding = 20; // default padding
×
510
        }
511

512
        $assetMaskable = clone $this;
×
513

514
        $quality = (int) $this->config->get('assets.images.quality');
×
515

516
        $cache = new Cache($this->builder, 'assets');
×
517
        $assetMaskable->cacheTags['maskable'] = true;
×
518
        $cacheKey = $cache->createKey($assetMaskable, tags: $assetMaskable->cacheTags);
×
519
        if (!$cache->has($cacheKey)) {
×
520
            $assetMaskable->data['content'] = Image::maskable($assetMaskable, $quality, $padding);
×
521
            $assetMaskable->data['path'] = '/' . Util::joinPath(
×
522
                (string) $this->config->get('assets.target'),
×
523
                'maskable',
×
524
                $assetMaskable->data['path']
×
525
            );
×
526
            $assetMaskable->data['size'] = \strlen($assetMaskable->data['content']);
×
527

528
            $cache->set($cacheKey, $assetMaskable->data, $this->config->get('cache.assets.ttl'));
×
529
            $this->builder->getLogger()->debug(\sprintf('Asset maskabled: "%s"', $assetMaskable->data['path']));
×
530
        }
531
        $assetMaskable->data = $cache->get($cacheKey);
×
532

533
        return $assetMaskable;
×
534
    }
535

536
    /**
537
     * Converts an image asset to $format format.
538
     *
539
     * @throws RuntimeException
540
     */
541
    public function convert(string $format, ?int $quality = null): self
542
    {
543
        if ($this->data['type'] != 'image') {
1✔
544
            throw new RuntimeException(\sprintf('Unable to convert "%s" (%s) to %s: not an image.', $this->data['path'], $this->data['type'], $format));
×
545
        }
546

547
        if ($quality === null) {
1✔
548
            $quality = (int) $this->config->get('assets.images.quality');
1✔
549
        }
550

551
        $asset = clone $this;
1✔
552
        $asset['ext'] = $format;
1✔
553
        $asset->data['subtype'] = "image/$format";
1✔
554

555
        if ($this->isImageInCdn()) {
1✔
556
            return $asset; // returns the asset with the new extension only: CDN do the rest of the job
×
557
        }
558

559
        $cache = new Cache($this->builder, 'assets');
1✔
560
        $this->cacheTags['quality'] = $quality;
1✔
561
        if ($this->data['width']) {
1✔
562
            $this->cacheTags['width'] = $this->data['width'];
1✔
563
        }
564
        $cacheKey = $cache->createKey($asset, tags: $this->cacheTags);
1✔
565
        if (!$cache->has($cacheKey)) {
1✔
566
            $asset->data['content'] = Image::convert($asset, $format, $quality);
1✔
567
            $asset->data['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']);
1✔
568
            $asset->data['size'] = \strlen($asset->data['content']);
1✔
569
            $cache->set($cacheKey, $asset->data, $this->config->get('cache.assets.ttl'));
1✔
570
            $this->builder->getLogger()->debug(\sprintf('Asset converted: "%s" (%s -> %s)', $asset->data['path'], $this->data['ext'], $format));
1✔
571
        }
572
        $asset->data = $cache->get($cacheKey);
1✔
573

574
        return $asset;
1✔
575
    }
576

577
    /**
578
     * Converts an image asset to WebP format.
579
     *
580
     * @throws RuntimeException
581
     */
582
    public function webp(?int $quality = null): self
583
    {
584
        return $this->convert('webp', $quality);
×
585
    }
586

587
    /**
588
     * Converts an image asset to AVIF format.
589
     *
590
     * @throws RuntimeException
591
     */
592
    public function avif(?int $quality = null): self
593
    {
594
        return $this->convert('avif', $quality);
×
595
    }
596

597
    /**
598
     * Is the asset an image and is it in CDN?
599
     */
600
    public function isImageInCdn(): bool
601
    {
602
        if (
603
            $this->data['type'] == 'image'
1✔
604
            && $this->config->isEnabled('assets.images.cdn')
1✔
605
            && $this->data['ext'] != 'ico'
1✔
606
            && (Image::isSVG($this) && $this->config->isEnabled('assets.images.cdn.svg'))
1✔
607
        ) {
608
            return true;
×
609
        }
610
        // handle remote image?
611
        if ($this->data['url'] !== null && $this->config->isEnabled('assets.images.cdn.remote')) {
1✔
612
            return true;
×
613
        }
614

615
        return false;
1✔
616
    }
617

618
    /**
619
     * Returns the width of an image/SVG or a video.
620
     *
621
     * @throws RuntimeException
622
     */
623
    public function getWidth(): ?int
624
    {
625
        switch ($this->data['type']) {
1✔
626
            case 'image':
1✔
627
                if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) {
1✔
628
                    return (int) $svg->width;
1✔
629
                }
630
                if (false === $size = $this->getImageSize()) {
1✔
631
                    throw new RuntimeException(\sprintf('Unable to get width of "%s".', $this->data['path']));
×
632
                }
633

634
                return $size[0];
1✔
635
            case 'video':
1✔
636
                return $this->getVideo()['width'];
1✔
637
        }
638

639
        return null;
1✔
640
    }
641

642
    /**
643
     * Returns the height of an image/SVG or a video.
644
     *
645
     * @throws RuntimeException
646
     */
647
    public function getHeight(): ?int
648
    {
649
        switch ($this->data['type']) {
1✔
650
            case 'image':
1✔
651
                if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) {
1✔
652
                    return (int) $svg->height;
1✔
653
                }
654
                if (false === $size = $this->getImageSize()) {
1✔
655
                    throw new RuntimeException(\sprintf('Unable to get height of "%s".', $this->data['path']));
×
656
                }
657

658
                return $size[1];
1✔
659
            case 'video':
1✔
660
                return $this->getVideo()['height'];
1✔
661
        }
662

663
        return null;
1✔
664
    }
665

666
    /**
667
     * Returns audio file infos:
668
     * - duration (in seconds.microseconds)
669
     * - bitrate (in bps)
670
     * - channel ('stereo', 'dual_mono', 'joint_stereo' or 'mono')
671
     *
672
     * @see https://github.com/wapmorgan/Mp3Info
673
     */
674
    public function getAudio(): array
675
    {
676
        $audio = new Mp3Info($this->data['file']);
1✔
677

678
        return [
1✔
679
            'duration' => $audio->duration,
1✔
680
            'bitrate'  => $audio->bitRate,
1✔
681
            'channel'  => $audio->channel,
1✔
682
        ];
1✔
683
    }
684

685
    /**
686
     * Returns video file infos:
687
     * - duration (in seconds)
688
     * - width (in pixels)
689
     * - height (in pixels)
690
     *
691
     * @see https://github.com/JamesHeinrich/getID3
692
     */
693
    public function getVideo(): array
694
    {
695
        if ($this->data['type'] !== 'video') {
1✔
696
            throw new RuntimeException(\sprintf('Unable to get video infos of "%s".', $this->data['path']));
×
697
        }
698

699
        $video = (new \getID3())->analyze($this->data['file']);
1✔
700

701
        return [
1✔
702
            'duration' => $video['playtime_seconds'],
1✔
703
            'width'    => $video['video']['resolution_x'],
1✔
704
            'height'   => $video['video']['resolution_y'],
1✔
705
        ];
1✔
706
    }
707

708
    /**
709
     * Builds a relative path from a URL.
710
     * Used for remote files.
711
     *
712
     * @deprecated Use Locator::buildPathFromUrl() instead.
713
     */
714
    public static function buildPathFromUrl(string $url): string
715
    {
NEW
716
        return Locator::buildPathFromUrl($url);
×
717
    }
718

719
    /**
720
     * Replaces some characters by '_'.
721
     *
722
     * @deprecated Use Locator::sanitize() instead.
723
     */
724
    public static function sanitize(string $string): string
725
    {
NEW
726
        return Locator::sanitize($string);
×
727
    }
728

729
    /**
730
     * Add hash to the file name.
731
     */
732
    protected function doFingerprint(): self
733
    {
734
        $hash = hash('xxh128', $this->data['content']);
1✔
735
        $this->data['path'] = preg_replace(
1✔
736
            '/\.' . $this->data['ext'] . '$/m',
1✔
737
            ".$hash." . $this->data['ext'],
1✔
738
            $this->data['path']
1✔
739
        );
1✔
740
        $this->builder->getLogger()->debug(\sprintf('Asset fingerprinted: "%s"', $this->data['path']));
1✔
741

742
        return $this;
1✔
743
    }
744

745
    /**
746
     * Compiles SCSS to CSS.
747
     */
748
    protected function doCompile(): self
749
    {
750
        $this->data = (new AssetCompiler($this->builder))->compile($this->data);
1✔
751

752
        return $this;
1✔
753
    }
754

755
    /**
756
     * Minifies a CSS or JS asset.
757
     */
758
    protected function doMinify(): self
759
    {
760
        // compile SCSS files first
761
        if ($this->data['ext'] === 'scss') {
1✔
762
            $this->doCompile();
×
763
        }
764
        $this->data = (new AssetOptimizer($this->builder))->minify($this->data);
1✔
765

766
        return $this;
1✔
767
    }
768

769
    /**
770
     * Optimizes an image file in-place.
771
     * Returns the new file size.
772
     */
773
    private function optimizeImage(string $filepath, string $path, int $quality): int
774
    {
775
        return (new AssetOptimizer($this->builder))->optimizeImage($filepath, $path, $quality);
1✔
776
    }
777

778
    /**
779
     * Returns image size informations.
780
     *
781
     * @see https://www.php.net/manual/function.getimagesize.php
782
     *
783
     * @throws RuntimeException
784
     */
785
    private function getImageSize(): array|false
786
    {
787
        if (!$this->data['type'] == 'image') {
1✔
788
            return false;
×
789
        }
790

791
        try {
792
            if (false === $size = getimagesizefromstring($this->data['content'])) {
1✔
793
                return false;
1✔
794
            }
795
        } catch (\Exception $e) {
×
796
            throw new RuntimeException(\sprintf('Handling asset "%s" failed: "%s".', $this->data['path'], $e->getMessage()));
×
797
        }
798

799
        return $size;
1✔
800
    }
801

802
    /**
803
     * Builds CDN image URL.
804
     */
805
    private function buildImageCdnUrl(): string
806
    {
807
        return str_replace(
×
808
            [
×
809
                '%account%',
×
810
                '%image_url%',
×
811
                '%width%',
×
812
                '%quality%',
×
813
                '%format%',
×
814
            ],
×
815
            [
×
816
                $this->config->get('assets.images.cdn.account') ?? '',
×
817
                ltrim($this->data['url'] ?? (string) new Url($this->builder, $this->data['path'], ['canonical' => $this->config->get('assets.images.cdn.canonical') ?? true]), '/'),
×
818
                $this->data['width'],
×
819
                (int) $this->config->get('assets.images.quality'),
×
820
                $this->data['ext'],
×
821
            ],
×
822
            (string) $this->config->get('assets.images.cdn.url')
×
823
        );
×
824
    }
825

826
    /**
827
     * Checks if the asset is not missing and is typed as an image.
828
     *
829
     * @throws RuntimeException
830
     */
831
    private function checkImage(): void
832
    {
833
        if ($this->isMissing()) {
1✔
834
            throw new RuntimeException(\sprintf('Unable to resize "%s": file not found.', $this->data['path']));
×
835
        }
836
        if ($this->data['type'] != 'image') {
1✔
837
            throw new RuntimeException(\sprintf('Unable to resize "%s": not an image.', $this->data['path']));
×
838
        }
839
    }
840

841
    /**
842
     * Remove redondant '/thumbnails/<width(xheight)>/' in the path.
843
     */
844
    private function deduplicateThumbPath(string $path): string
845
    {
846
        // https://regex101.com/r/0r7FMY/1
847
        $pattern = '/(' . self::IMAGE_THUMB . '\/(\d+){0,1}x(\d+){0,1}\/)(' . self::IMAGE_THUMB . '\/(\d+){0,1}x(\d+){0,1}\/)(.*)/i';
1✔
848

849
        if (null === $result = preg_replace($pattern, '$1$7', $path)) {
1✔
850
            return $path;
×
851
        }
852

853
        return $result;
1✔
854
    }
855
}
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