1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
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
|
1 |
|
public static function buildSrcset(Asset $asset, array $widths): string |
28
|
|
|
{ |
29
|
1 |
|
if ($asset['type'] !== 'image') { |
30
|
|
|
throw new RuntimeException(sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path'])); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
$srcset = ''; |
34
|
1 |
|
$widthMax = 0; |
35
|
1 |
|
foreach ($widths as $width) { |
36
|
1 |
|
if ($asset['width'] < $width) { |
37
|
1 |
|
break; |
38
|
|
|
} |
39
|
1 |
|
$img = $asset->resize($width); |
40
|
1 |
|
$srcset .= sprintf('%s %sw, ', (string) $img, $width); |
41
|
1 |
|
$widthMax = $width; |
42
|
|
|
} |
43
|
1 |
|
rtrim($srcset, ', '); |
44
|
|
|
// adds source image? |
45
|
1 |
|
if (!empty($srcset) && $asset['width'] != $widthMax && $asset['width'] < max($widths)) { |
46
|
1 |
|
$srcset .= sprintf('%s %sw', (string) $asset, $asset['width']); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $srcset; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the value of the "sizes" attribute corresponding to the configured class. |
54
|
|
|
*/ |
55
|
1 |
|
public static function getSizes(string $class, array $sizes = []): string |
56
|
|
|
{ |
57
|
1 |
|
$result = ''; |
58
|
1 |
|
$classArray = explode(' ', $class); |
59
|
1 |
|
foreach ($classArray as $class) { |
60
|
1 |
|
if (\array_key_exists($class, $sizes)) { |
61
|
1 |
|
$result = $sizes[$class] . ', '; |
62
|
|
|
} |
63
|
|
|
} |
64
|
1 |
|
if (!empty($result)) { |
65
|
1 |
|
return trim($result, ', '); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $sizes['default'] ?? '100vw'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if an asset is an animated GIF. |
73
|
|
|
*/ |
74
|
1 |
|
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
|
1 |
|
$count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content_source']); |
81
|
|
|
|
82
|
1 |
|
return $count > 1; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the dominant hexadecimal color of an image asset. |
87
|
|
|
* |
88
|
|
|
* @throws RuntimeException |
89
|
|
|
*/ |
90
|
1 |
|
public static function getDominantColor(Asset $asset): string |
91
|
|
|
{ |
92
|
1 |
|
if ($asset['type'] !== 'image') { |
93
|
|
|
throw new RuntimeException(sprintf('can\'t get dominant color of "%s": it\'s not an image file.', $asset['path'])); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$assetColor = clone $asset; |
97
|
1 |
|
$assetColor = $assetColor->resize(100); |
98
|
1 |
|
$img = ImageManager::make($assetColor['content']); |
99
|
1 |
|
$color = $img->limitColors(1)->pickColor(0, 0, 'hex'); |
100
|
1 |
|
$img->destroy(); |
101
|
|
|
|
102
|
1 |
|
return $color; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a Low Quality Image Placeholder (LQIP) as data URL. |
107
|
|
|
* |
108
|
|
|
* @throws RuntimeException |
109
|
|
|
*/ |
110
|
1 |
|
public static function getLqip(Asset $asset): string |
111
|
|
|
{ |
112
|
1 |
|
if ($asset['type'] !== 'image') { |
113
|
|
|
throw new RuntimeException(sprintf('can\'t create LQIP of "%s": it\'s not an image file.', $asset['path'])); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$assetLqip = clone $asset; |
117
|
1 |
|
$assetLqip = $assetLqip->resize(100); |
118
|
|
|
|
119
|
1 |
|
return (string) ImageManager::make($assetLqip['content'])->blur(50)->encode('data-url'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|