|
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\Drivers\Gd\Driver as GdDriver; |
|
18
|
|
|
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; |
|
19
|
|
|
use Intervention\Image\Encoders\AutoEncoder; |
|
20
|
|
|
use Intervention\Image\ImageManager; |
|
21
|
|
|
|
|
22
|
|
|
class Image |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Create new manager instance with available driver. |
|
26
|
|
|
*/ |
|
27
|
1 |
|
private static function manager(): ImageManager |
|
28
|
|
|
{ |
|
29
|
1 |
|
$driver = null; |
|
30
|
|
|
|
|
31
|
|
|
// ImageMagick is available? (for a future quality option) |
|
32
|
1 |
|
if (\extension_loaded('imagick') && class_exists('Imagick')) { |
|
33
|
|
|
$driver = ImagickDriver::class; |
|
34
|
|
|
} |
|
35
|
|
|
// Use GD, because it's the faster driver |
|
36
|
1 |
|
if (\extension_loaded('gd') && \function_exists('gd_info')) { |
|
37
|
1 |
|
$driver = GdDriver::class; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
if ($driver) { |
|
41
|
1 |
|
return ImageManager::withDriver( |
|
42
|
1 |
|
$driver, |
|
43
|
1 |
|
[ |
|
44
|
1 |
|
'autoOrientation' => true, |
|
45
|
1 |
|
'decodeAnimation' => true, |
|
46
|
1 |
|
'blendingColor' => 'ffffff', |
|
47
|
1 |
|
'strip' => true, // remove metadata |
|
48
|
1 |
|
] |
|
49
|
1 |
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
throw new RuntimeException('PHP GD (or Imagick) extension is required.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Resize an image Asset. |
|
57
|
|
|
* |
|
58
|
|
|
* @throws RuntimeException |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public static function resize(Asset $asset, int $width, int $quality): string |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
|
|
// creates image object from source |
|
64
|
1 |
|
$image = self::manager()->read($asset['content']); |
|
65
|
|
|
// resizes to $width with constraint the aspect-ratio and unwanted upsizing |
|
66
|
1 |
|
$image->scaleDown(width: $width); |
|
67
|
|
|
// return image data |
|
68
|
1 |
|
return (string) $image->encodeByMediaType($asset['subtype'], /** @scrutinizer ignore-type */ progressive: true, /** @scrutinizer ignore-type */ interlaced: false, quality: $quality); |
|
69
|
|
|
} catch (\Exception $e) { |
|
70
|
|
|
throw new RuntimeException(\sprintf('Asset "%s" can\'t be resized: %s', $asset['path'], $e->getMessage())); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Converts an image Asset to the target format. |
|
76
|
|
|
* |
|
77
|
|
|
* @throws RuntimeException |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public static function convert(Asset $asset, string $format, int $quality): string |
|
80
|
|
|
{ |
|
81
|
|
|
try { |
|
82
|
1 |
|
$image = self::manager()->read($asset['content']); |
|
83
|
|
|
|
|
84
|
1 |
|
if (!\function_exists("image$format")) { |
|
85
|
1 |
|
throw new RuntimeException(\sprintf('Function "image%s" is not available.', $format)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return (string) $image->encodeByExtension($format, /** @scrutinizer ignore-type */ progressive: true, /** @scrutinizer ignore-type */ interlaced: false, quality: $quality); |
|
89
|
1 |
|
} catch (\Exception $e) { |
|
90
|
1 |
|
throw new RuntimeException(\sprintf('Not able to convert "%s" to %s: %s', $asset['path'], $format, $e->getMessage())); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the Data URL (encoded in Base64). |
|
96
|
|
|
* |
|
97
|
|
|
* @throws RuntimeException |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public static function getDataUrl(Asset $asset, int $quality): string |
|
100
|
|
|
{ |
|
101
|
|
|
try { |
|
102
|
1 |
|
$image = self::manager()->read($asset['content']); |
|
103
|
|
|
|
|
104
|
1 |
|
return (string) $image->encode(new AutoEncoder(quality: $quality))->toDataUri(); |
|
105
|
|
|
} catch (\Exception $e) { |
|
106
|
|
|
throw new RuntimeException(\sprintf('Can\'t get Data URL of "%s": %s', $asset['path'], $e->getMessage())); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the dominant RGB color of an image asset. |
|
112
|
|
|
* |
|
113
|
|
|
* @throws RuntimeException |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public static function getDominantColor(Asset $asset): string |
|
116
|
|
|
{ |
|
117
|
|
|
try { |
|
118
|
1 |
|
$image = self::manager()->read(self::resize($asset, 100, 50)); |
|
119
|
|
|
|
|
120
|
1 |
|
return $image->reduceColors(1)->pickColor(0, 0)->toString(); |
|
121
|
|
|
} catch (\Exception $e) { |
|
122
|
|
|
throw new RuntimeException(\sprintf('Can\'t get dominant color of "%s": %s', $asset['path'], $e->getMessage())); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns a Low Quality Image Placeholder (LQIP) as data URL. |
|
128
|
|
|
* |
|
129
|
|
|
* @throws RuntimeException |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public static function getLqip(Asset $asset): string |
|
132
|
|
|
{ |
|
133
|
|
|
try { |
|
134
|
1 |
|
$image = self::manager()->read(self::resize($asset, 100, 50)); |
|
135
|
|
|
|
|
136
|
1 |
|
return (string) $image->blur(50)->encode()->toDataUri(); |
|
137
|
|
|
} catch (\Exception $e) { |
|
138
|
|
|
throw new RuntimeException(\sprintf('can\'t create LQIP of "%s": %s', $asset['path'], $e->getMessage())); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Build the `srcset` attribute for responsive images. |
|
144
|
|
|
* e.g.: `srcset="/img-480.jpg 480w, /img-800.jpg 800w"`. |
|
145
|
|
|
* |
|
146
|
|
|
* @throws RuntimeException |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public static function buildSrcset(Asset $asset, array $widths): string |
|
149
|
|
|
{ |
|
150
|
1 |
|
if (!self::isImage($asset)) { |
|
151
|
|
|
throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path'])); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
$srcset = ''; |
|
155
|
1 |
|
$widthMax = 0; |
|
156
|
1 |
|
sort($widths, SORT_NUMERIC); |
|
157
|
1 |
|
$widths = array_reverse($widths); |
|
158
|
1 |
|
foreach ($widths as $width) { |
|
159
|
1 |
|
if ($asset['width'] < $width) { |
|
160
|
1 |
|
break; |
|
161
|
|
|
} |
|
162
|
|
|
$img = $asset->resize($width); |
|
163
|
|
|
$srcset .= \sprintf('%s %sw, ', (string) $img, $width); |
|
164
|
|
|
$widthMax = $width; |
|
165
|
|
|
} |
|
166
|
|
|
// adds source image |
|
167
|
1 |
|
if (!empty($srcset) && ($asset['width'] < max($widths) && $asset['width'] != $widthMax)) { |
|
168
|
|
|
$srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
return rtrim($srcset, ', '); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns the value of the "sizes" attribute corresponding to the configured class. |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public static function getSizes(string $class, array $sizes = []): string |
|
178
|
|
|
{ |
|
179
|
1 |
|
$result = ''; |
|
180
|
1 |
|
$classArray = explode(' ', $class); |
|
181
|
1 |
|
foreach ($classArray as $class) { |
|
182
|
1 |
|
if (\array_key_exists($class, $sizes)) { |
|
183
|
|
|
$result = $sizes[$class] . ', '; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
1 |
|
if (!empty($result)) { |
|
187
|
|
|
return trim($result, ', '); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
return $sizes['default'] ?? '100vw'; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Checks if an asset is an animated GIF. |
|
195
|
|
|
*/ |
|
196
|
1 |
|
public static function isAnimatedGif(Asset $asset): bool |
|
197
|
|
|
{ |
|
198
|
|
|
// an animated GIF contains multiple "frames", with each frame having a header made up of: |
|
199
|
|
|
// 1. a static 4-byte sequence (\x00\x21\xF9\x04) |
|
200
|
|
|
// 2. 4 variable bytes |
|
201
|
|
|
// 3. a static 2-byte sequence (\x00\x2C) |
|
202
|
1 |
|
$count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content']); |
|
203
|
|
|
|
|
204
|
1 |
|
return $count > 1; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Returns true if asset is a SVG. |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public static function isSVG(Asset $asset): bool |
|
211
|
|
|
{ |
|
212
|
1 |
|
return \in_array($asset['subtype'], ['image/svg', 'image/svg+xml']) || $asset['ext'] == 'svg'; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Returns true if asset is an ICO. |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function isIco(Asset $asset): bool |
|
219
|
|
|
{ |
|
220
|
|
|
return \in_array($asset['subtype'], ['image/x-icon', 'image/vnd.microsoft.icon']) || $asset['ext'] == 'ico'; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Asset is a valid image? |
|
225
|
|
|
*/ |
|
226
|
|
|
public static function isImage(Asset $asset): bool |
|
227
|
|
|
{ |
|
228
|
|
|
if ($asset['type'] !== 'image' || self::isSVG($asset) || self::isIco($asset)) { |
|
229
|
|
|
return false; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
1 |
|
return true; |
|
233
|
|
|
} |
|
234
|
1 |
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns SVG attributes. |
|
237
|
|
|
* |
|
238
|
1 |
|
* @return \SimpleXMLElement|false |
|
239
|
|
|
*/ |
|
240
|
|
|
public static function getSvgAttributes(Asset $asset) |
|
241
|
|
|
{ |
|
242
|
1 |
|
if (!self::isSVG($asset)) { |
|
243
|
|
|
return false; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
if (false === $xml = simplexml_load_string($asset['content'] ?? '')) { |
|
247
|
|
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return $xml->attributes(); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|