1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\models; |
12
|
|
|
|
13
|
|
|
use craft\helpers\Html; |
14
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
15
|
|
|
use nystudio107\imageoptimize\helpers\UrlHelper; |
16
|
|
|
use nystudio107\imageoptimize\helpers\Color as ColorHelper; |
17
|
|
|
|
18
|
|
|
use craft\helpers\Template; |
19
|
|
|
use craft\base\Model; |
20
|
|
|
use craft\validators\ArrayValidator; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package ImageOptimize |
|
|
|
|
25
|
|
|
* @since 1.2.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class OptimizedImage extends Model |
28
|
|
|
{ |
29
|
|
|
// Public Properties |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var string[] An array of optimized image variant URLs |
34
|
|
|
*/ |
35
|
|
|
public $optimizedImageUrls = []; |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var string[] An array of optimized .webp image variant URLs |
39
|
|
|
*/ |
40
|
|
|
public $optimizedWebPImageUrls = []; |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var int[] An array of the widths of the optimized image variants |
44
|
|
|
*/ |
45
|
|
|
public $variantSourceWidths = []; |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var int[] An array of the heights of the optimized image variants |
49
|
|
|
*/ |
50
|
|
|
public $variantHeights = []; |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @var float[] An array of the x,y image focal point coords, ranging from 0.0 to 1.0 |
54
|
|
|
*/ |
55
|
|
|
public $focalPoint; |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @var int The width of the original source image |
59
|
|
|
*/ |
60
|
|
|
public $originalImageWidth; |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* @var int The height of the original source image |
64
|
|
|
*/ |
65
|
|
|
public $originalImageHeight; |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var string The base64 encoded placeholder LQIP image |
69
|
|
|
*/ |
70
|
|
|
public $placeholder = ''; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var string The base64 encoded placeholder LQIP SVG image |
74
|
|
|
*/ |
75
|
|
|
public $placeholderSvg = ''; |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var string[] An array the 5 most dominant colors in the image |
79
|
|
|
*/ |
80
|
|
|
public $colorPalette = []; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var int The overall lightness of the image, from 0..100 |
84
|
|
|
*/ |
85
|
|
|
public $lightness; |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @var int The width of the placeholder image |
89
|
|
|
*/ |
90
|
|
|
public $placeholderWidth; |
91
|
|
|
|
92
|
|
|
/** |
|
|
|
|
93
|
|
|
* @var int The height of the placeholder image |
94
|
|
|
*/ |
95
|
|
|
public $placeholderHeight; |
96
|
|
|
|
97
|
|
|
/** |
|
|
|
|
98
|
|
|
* @var string[] An array of errors logged when generating the image transforms |
99
|
|
|
*/ |
100
|
|
|
public $stickyErrors = []; |
101
|
|
|
|
102
|
|
|
// Public Methods |
103
|
|
|
// ========================================================================= |
104
|
|
|
|
105
|
|
|
/** |
|
|
|
|
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
|
|
|
|
108
|
|
|
public function rules(): array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
['optimizedImageUrls', ArrayValidator::class], |
112
|
|
|
['optimizedWebPImageUrls', ArrayValidator::class], |
113
|
|
|
['variantSourceWidths', ArrayValidator::class], |
114
|
|
|
['variantHeights', ArrayValidator::class], |
115
|
|
|
['focalPoint', 'safe'], |
116
|
|
|
['originalImageWidth', 'integer'], |
117
|
|
|
['originalImageHeight', 'integer'], |
118
|
|
|
['placeholder', 'string'], |
119
|
|
|
['placeholderSvg', 'string'], |
120
|
|
|
['colorPalette', ArrayValidator::class], |
121
|
|
|
['placeholderWidth', 'integer'], |
122
|
|
|
['placeholderHeight', 'integer'], |
123
|
|
|
['stickyErrors', ArrayValidator::class], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return the first image variant URL or the specific one passed in via |
129
|
|
|
* $width |
130
|
|
|
* |
131
|
|
|
* @param int $width |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return \Twig\Markup|null |
134
|
|
|
*/ |
135
|
|
|
public function src(int $width = 0): string |
136
|
|
|
{ |
137
|
|
|
if (empty($width)) { |
138
|
|
|
return Template::raw(reset($this->optimizedImageUrls)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return Template::raw($this->optimizedImageUrls[$width] ?? ''); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Getter for CraftQL |
146
|
|
|
* |
147
|
|
|
* @param int $width |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return null|string|\Twig\Markup |
150
|
|
|
*/ |
151
|
|
|
public function getSrc(int $width = 0): string |
152
|
|
|
{ |
153
|
|
|
return $this->src($width); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return a string of image URLs and their sizes |
158
|
|
|
* |
159
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
160
|
|
|
* srcsets |
161
|
|
|
* |
162
|
|
|
* @return \Twig\Markup|null |
163
|
|
|
*/ |
164
|
|
|
public function srcset(bool $dpr = false): string |
165
|
|
|
{ |
166
|
|
|
return Template::raw($this->getSrcsetFromArray($this->optimizedImageUrls, $dpr)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Getter for CraftQL |
171
|
|
|
* |
172
|
|
|
* @param bool $dpr |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getSrcset(bool $dpr = false): string |
177
|
|
|
{ |
178
|
|
|
return $this->srcset($dpr); |
179
|
|
|
} |
180
|
|
|
/** |
181
|
|
|
* Return a string of image URLs and their sizes that match $width |
182
|
|
|
* |
183
|
|
|
* @param int $width |
|
|
|
|
184
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
185
|
|
|
* srcsets |
186
|
|
|
* |
187
|
|
|
* @return \Twig\Markup|null |
188
|
|
|
*/ |
189
|
|
|
public function srcsetWidth(int $width, bool $dpr = false): string |
190
|
|
|
{ |
191
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'width'); |
192
|
|
|
|
193
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return a string of image URLs and their sizes that are at least $width |
198
|
|
|
* or larger |
199
|
|
|
* |
200
|
|
|
* @param int $width |
|
|
|
|
201
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
202
|
|
|
* srcsets |
203
|
|
|
* |
204
|
|
|
* @return \Twig\Markup|null |
205
|
|
|
*/ |
206
|
|
|
public function srcsetMinWidth(int $width, bool $dpr = false): string |
207
|
|
|
{ |
208
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'minwidth'); |
209
|
|
|
|
210
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Return a string of image URLs and their sizes that are $width or smaller |
215
|
|
|
* |
216
|
|
|
* @param int $width |
|
|
|
|
217
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
218
|
|
|
* srcsets |
219
|
|
|
* |
220
|
|
|
* @return \Twig\Markup|null |
221
|
|
|
*/ |
222
|
|
|
public function srcsetMaxWidth(int $width, bool $dpr = false): string |
223
|
|
|
{ |
224
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'maxwidth'); |
225
|
|
|
|
226
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the first webp image variant URL or the specific one passed in |
231
|
|
|
* via $width |
232
|
|
|
* |
233
|
|
|
* @param int $width |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return \Twig\Markup|null |
236
|
|
|
*/ |
237
|
|
|
public function srcWebp(int $width = 0): string |
238
|
|
|
{ |
239
|
|
|
if (empty($width)) { |
240
|
|
|
return Template::raw(reset($this->optimizedWebPImageUrls)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return Template::raw($this->optimizedWebPImageUrls[$width] ?? ''); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Getter for CraftQL |
248
|
|
|
* |
249
|
|
|
* @param int $width |
|
|
|
|
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getSrcWebp(int $width = 0): string |
254
|
|
|
{ |
255
|
|
|
return $this->srcWebp($width); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return a string of webp image URLs and their sizes |
260
|
|
|
* |
261
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
262
|
|
|
* srcsets |
263
|
|
|
* |
264
|
|
|
* @return \Twig\Markup|null |
265
|
|
|
*/ |
266
|
|
|
public function srcsetWebp(bool $dpr = false): string |
267
|
|
|
{ |
268
|
|
|
return Template::raw($this->getSrcsetFromArray($this->optimizedWebPImageUrls, $dpr)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Getter for CraftQL |
273
|
|
|
* |
274
|
|
|
* @param bool $dpr |
|
|
|
|
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function getSrcsetWebp(bool $dpr = false): string |
279
|
|
|
{ |
280
|
|
|
return $this->srcsetWebp($dpr); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return a string of webp image URLs and their sizes that match $width |
285
|
|
|
* |
286
|
|
|
* @param int $width |
|
|
|
|
287
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
288
|
|
|
* srcsets |
289
|
|
|
* |
290
|
|
|
* @return \Twig\Markup|null |
291
|
|
|
*/ |
292
|
|
|
public function srcsetWidthWebp(int $width, bool $dpr = false): string |
293
|
|
|
{ |
294
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'width'); |
295
|
|
|
|
296
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Return a string of webp image URLs and their sizes that are at least |
301
|
|
|
* $width or larger |
302
|
|
|
* |
303
|
|
|
* @param int $width |
|
|
|
|
304
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
305
|
|
|
* srcsets |
306
|
|
|
* |
307
|
|
|
* @return \Twig\Markup|null |
308
|
|
|
*/ |
309
|
|
|
public function srcsetMinWidthWebp(int $width, bool $dpr = false): string |
310
|
|
|
{ |
311
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'minwidth'); |
312
|
|
|
|
313
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Return a string of webp image URLs and their sizes that are $width or |
318
|
|
|
* smaller |
319
|
|
|
* |
320
|
|
|
* @param int $width |
|
|
|
|
321
|
|
|
* @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
|
|
|
|
322
|
|
|
* srcsets |
323
|
|
|
* |
324
|
|
|
* @return \Twig\Markup|null |
325
|
|
|
*/ |
326
|
|
|
public function srcsetMaxWidthWebp(int $width, bool $dpr = false): string |
327
|
|
|
{ |
328
|
|
|
$subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'maxwidth'); |
329
|
|
|
|
330
|
|
|
return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Work around issues with `<img srcset>` returning sizes larger than are |
335
|
|
|
* available as per: |
336
|
|
|
* https://medium.com/@MRWwebDesign/responsive-images-the-sizes-attribute-and-unexpected-image-sizes-882a2eadb6db |
337
|
|
|
* |
338
|
|
|
* @return int |
339
|
|
|
*/ |
340
|
|
|
public function maxSrcsetWidth(): int |
341
|
|
|
{ |
342
|
|
|
$result = 0; |
343
|
|
|
if (!empty($this->optimizedImageUrls)) { |
344
|
|
|
$tempArray = $this->optimizedImageUrls; |
345
|
|
|
ksort($tempArray, SORT_NUMERIC); |
346
|
|
|
|
347
|
|
|
$keys = array_keys($tempArray); |
348
|
|
|
$result = end($keys); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return $result; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Getter for CraftQL |
356
|
|
|
* |
357
|
|
|
* @return int |
358
|
|
|
*/ |
359
|
|
|
public function getMaxSrcsetWidth(): int |
360
|
|
|
{ |
361
|
|
|
return $this->maxSrcsetWidth(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Return the colors as an array of RGB colors |
366
|
|
|
*/ |
|
|
|
|
367
|
|
|
public function colorPaletteRgb(): array |
368
|
|
|
{ |
369
|
|
|
$colors = []; |
370
|
|
|
|
371
|
|
|
foreach ($this->colorPalette as $color) { |
372
|
|
|
if (!empty($color)) { |
373
|
|
|
$colors[] = ColorHelper::HTMLToRGB($color); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $colors; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Generate a complete <img> tag for this OptimizedImages model |
382
|
|
|
* |
383
|
|
|
* @param false|string $lazyLoad |
|
|
|
|
384
|
|
|
* @param array $imgAttrs |
|
|
|
|
385
|
|
|
* |
386
|
|
|
* @return \Twig\Markup |
387
|
|
|
*/ |
388
|
|
|
public function imgTag($lazyLoad = false, $imgAttrs = []) |
389
|
|
|
{ |
390
|
|
|
// Merge the passed in options with the tag attributes |
391
|
|
|
$attrs = array_merge([ |
|
|
|
|
392
|
|
|
'class' => '', |
393
|
|
|
'width' => $this->placeholderWidth, |
394
|
|
|
'height' => $this->placeholderHeight, |
395
|
|
|
'src' => reset($this->optimizedImageUrls), |
396
|
|
|
'srcset' => $this->getSrcsetFromArray($this->optimizedImageUrls), |
397
|
|
|
'sizes' => '100vw', |
398
|
|
|
'loading' => $lazyLoad, |
399
|
|
|
], |
400
|
|
|
$imgAttrs |
401
|
|
|
); |
402
|
|
|
// Handle lazy loading |
403
|
|
|
if ($lazyLoad) { |
404
|
|
|
$attrs = $this->swapLazyLoadAttrs($lazyLoad, $attrs); |
405
|
|
|
} |
406
|
|
|
// Remove any empty attributes |
407
|
|
|
$attrs = array_filter($attrs); |
408
|
|
|
// Render the tag |
409
|
|
|
$tag = Html::tag('img', '', $attrs); |
410
|
|
|
|
411
|
|
|
return Template::raw($tag); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Generate a complete <picture> tag for this OptimizedImages model |
416
|
|
|
* |
417
|
|
|
* @param false $lazyLoad |
|
|
|
|
418
|
|
|
* @param array $pictureAttrs |
|
|
|
|
419
|
|
|
* @param array $srcsetAttrs |
|
|
|
|
420
|
|
|
* @param array $imgAttrs |
|
|
|
|
421
|
|
|
* |
422
|
|
|
* @return \Twig\Markup |
423
|
|
|
*/ |
424
|
|
|
public function pictureTag($lazyLoad = false, $pictureAttrs = [], $srcsetAttrs = [], $imgAttrs = []) |
425
|
|
|
{ |
426
|
|
|
$content = ''; |
427
|
|
|
// Handle the webp srcset |
428
|
|
|
if (!empty($this->optimizedWebPImageUrls)) { |
429
|
|
|
// Merge the passed in options with the tag attributes |
430
|
|
|
$attrs = array_merge([ |
|
|
|
|
431
|
|
|
'data-srcset' => $this->getSrcsetFromArray($this->optimizedWebPImageUrls), |
432
|
|
|
'type' => 'image/webp', |
433
|
|
|
'sizes' => '100vw', |
434
|
|
|
], |
435
|
|
|
$srcsetAttrs |
436
|
|
|
); |
437
|
|
|
// Handle lazy loading |
438
|
|
|
if ($lazyLoad) { |
|
|
|
|
439
|
|
|
$attrs = $this->swapLazyLoadAttrs($lazyLoad, $attrs); |
440
|
|
|
} |
441
|
|
|
// Remove any empty attributes |
442
|
|
|
$attrs = array_filter($attrs); |
443
|
|
|
// Render the tag |
444
|
|
|
$content .= Html::tag('source', '', $attrs); |
445
|
|
|
} |
446
|
|
|
// Handle the regular srcset |
447
|
|
|
if (!empty($this->optimizedImageUrls)) { |
448
|
|
|
// Merge the passed in options with the tag attributes |
449
|
|
|
$attrs = array_merge([ |
|
|
|
|
450
|
|
|
'data-srcset' => $this->getSrcsetFromArray($this->optimizedImageUrls), |
451
|
|
|
'sizes' => '100vw', |
452
|
|
|
], |
453
|
|
|
$srcsetAttrs |
454
|
|
|
); |
455
|
|
|
// Handle lazy loading |
456
|
|
|
if ($lazyLoad) { |
|
|
|
|
457
|
|
|
$attrs = $this->swapLazyLoadAttrs($lazyLoad, $attrs); |
458
|
|
|
} |
459
|
|
|
// Remove any empty attributes |
460
|
|
|
$attrs = array_filter($attrs); |
461
|
|
|
// Render the tag |
462
|
|
|
$content .= Html::tag('source', '', $attrs); |
463
|
|
|
} |
464
|
|
|
// Handle the img tag |
465
|
|
|
/** @noinspection SuspiciousAssignmentsInspection */ |
|
|
|
|
466
|
|
|
$attrs = array_merge([ |
|
|
|
|
467
|
|
|
'class' => '', |
468
|
|
|
'width' => $this->placeholderWidth, |
469
|
|
|
'height' => $this->placeholderHeight, |
470
|
|
|
'src' => reset($this->optimizedImageUrls), |
471
|
|
|
'loading' => $lazyLoad, |
472
|
|
|
], |
473
|
|
|
$imgAttrs |
474
|
|
|
); |
475
|
|
|
// Handle lazy loading |
476
|
|
|
if ($lazyLoad) { |
|
|
|
|
477
|
|
|
$attrs = $this->swapLazyLoadAttrs($lazyLoad, $attrs); |
478
|
|
|
} |
479
|
|
|
// Remove any empty attributes |
480
|
|
|
$attrs = array_filter($attrs); |
481
|
|
|
// Render the tag |
482
|
|
|
$content .= Html::tag('img', '', $attrs); |
483
|
|
|
// Merge the passed in options with the tag attributes |
484
|
|
|
$attrs = array_merge([ |
|
|
|
|
485
|
|
|
], |
486
|
|
|
$pictureAttrs |
487
|
|
|
); |
488
|
|
|
// Handle lazy loading |
489
|
|
|
if ($lazyLoad) { |
|
|
|
|
490
|
|
|
$attrs = $this->swapLazyLoadAttrs($lazyLoad, $attrs); |
491
|
|
|
} |
492
|
|
|
// Remove any empty attributes |
493
|
|
|
$attrs = array_filter($attrs); |
494
|
|
|
// Render the tag |
495
|
|
|
$tag = Html::tag('picture', $content, $attrs); |
496
|
|
|
|
497
|
|
|
return Template::raw($tag); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Return a base64-encoded placeholder image |
502
|
|
|
* |
503
|
|
|
* @return \Twig\Markup|null |
504
|
|
|
*/ |
505
|
|
|
public function placeholderImage() |
506
|
|
|
{ |
507
|
|
|
$header = 'data:image/jpeg;base64,'; |
508
|
|
|
if (!empty($this->placeholder)) { |
509
|
|
|
$content = $this->placeholder; |
510
|
|
|
} else { |
511
|
|
|
// At least return something |
512
|
|
|
return $this->defaultPlaceholderImage(); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
return Template::raw($header.rawurlencode($content)); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Getter for CraftQL |
520
|
|
|
* |
521
|
|
|
* @return string |
522
|
|
|
*/ |
523
|
|
|
public function getPlaceholderImage(): string |
524
|
|
|
{ |
525
|
|
|
return (string)$this->placeholderImage(); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
|
|
|
|
529
|
|
|
* @return string |
530
|
|
|
*/ |
531
|
|
|
public function placeholderImageSize(): string |
532
|
|
|
{ |
533
|
|
|
$placeholder = $this->placeholderImage(); |
534
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
535
|
|
|
|
536
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Return an SVG box as a placeholder image |
541
|
|
|
* |
542
|
|
|
* @param string|null $color |
|
|
|
|
543
|
|
|
* |
544
|
|
|
* @return \Twig\Markup|null |
545
|
|
|
*/ |
546
|
|
|
public function placeholderBox(string $color = null) |
547
|
|
|
{ |
548
|
|
|
$width = $this->placeholderWidth ?? 1; |
549
|
|
|
$height = $this->placeholderHeight ?? 1; |
550
|
|
|
$color = $color ?? $this->colorPalette[0] ?? '#CCC'; |
551
|
|
|
|
552
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
|
|
|
|
556
|
|
|
* @param string|null $color |
|
|
|
|
557
|
|
|
* |
558
|
|
|
* @return string |
559
|
|
|
*/ |
560
|
|
|
public function getPlaceholderBox(string $color = null): string |
561
|
|
|
{ |
562
|
|
|
return (string)$this->placeholderBox($color); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Getter for CraftQL |
567
|
|
|
* |
568
|
|
|
* @return string |
569
|
|
|
*/ |
570
|
|
|
public function placeholderBoxSize(): string |
571
|
|
|
{ |
572
|
|
|
$placeholder = $this->placeholderBox(); |
573
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
574
|
|
|
|
575
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Return a silhouette of the image as an SVG placeholder |
580
|
|
|
* |
581
|
|
|
* @return \Twig\Markup|null |
582
|
|
|
*/ |
583
|
|
|
public function placeholderSilhouette() |
584
|
|
|
{ |
585
|
|
|
$header = 'data:image/svg+xml,'; |
586
|
|
|
if (!empty($this->placeholderSvg)) { |
587
|
|
|
$content = $this->placeholderSvg; |
588
|
|
|
} else { |
589
|
|
|
// At least return something |
590
|
|
|
return $this->defaultPlaceholderImage(); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
return Template::raw($header.$content); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Getter for CraftQL |
598
|
|
|
* |
599
|
|
|
* @return string |
600
|
|
|
*/ |
601
|
|
|
public function getPlaceholderSilhouette(): string |
602
|
|
|
{ |
603
|
|
|
return (string)$this->placeholderSilhouette(); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
|
|
|
|
607
|
|
|
* @return string |
608
|
|
|
*/ |
609
|
|
|
public function placeholderSilhouetteSize(): string |
610
|
|
|
{ |
611
|
|
|
$placeholder = $this->placeholderSilhouette(); |
612
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
613
|
|
|
|
614
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Get the file size of any remote resource (using curl), |
619
|
|
|
* either in bytes or - default - as human-readable formatted string. |
620
|
|
|
* |
621
|
|
|
* @author Stephan Schmitz <[email protected]> |
622
|
|
|
* @license MIT <http://eyecatchup.mit-license.org/> |
623
|
|
|
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d> |
624
|
|
|
* |
625
|
|
|
* @param string $url Takes the remote object's URL. |
|
|
|
|
626
|
|
|
* @param boolean $formatSize Whether to return size in bytes or |
|
|
|
|
627
|
|
|
* formatted. |
628
|
|
|
* @param boolean $useHead Whether to use HEAD requests. If false, |
|
|
|
|
629
|
|
|
* uses GET. |
630
|
|
|
* |
631
|
|
|
* @return int|mixed|string Returns human-readable formatted size |
|
|
|
|
632
|
|
|
* or size in bytes (default: formatted). |
633
|
|
|
*/ |
634
|
|
|
public function getRemoteFileSize($url, $formatSize = true, $useHead = true) |
635
|
|
|
{ |
636
|
|
|
// Get an absolute URL with protocol that curl will be happy with |
637
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
638
|
|
|
$ch = curl_init($url); |
639
|
|
|
curl_setopt_array($ch, [ |
|
|
|
|
640
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
641
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
642
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
643
|
|
|
]); |
|
|
|
|
644
|
|
|
if ($useHead) { |
645
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, 1); |
646
|
|
|
} |
647
|
|
|
curl_exec($ch); |
648
|
|
|
// content-length of download (in bytes), read from Content-Length: field |
649
|
|
|
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
650
|
|
|
curl_close($ch); |
651
|
|
|
// cannot retrieve file size, return "-1" |
652
|
|
|
if (!$contentLength) { |
653
|
|
|
return -1; |
654
|
|
|
} |
655
|
|
|
// return size in bytes |
656
|
|
|
if (!$formatSize) { |
657
|
|
|
return $contentLength; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
// Protected Methods |
664
|
|
|
// ========================================================================= |
665
|
|
|
|
666
|
|
|
protected function getSrcsetSubsetArray(array $set, int $width, string $comparison): array |
|
|
|
|
667
|
|
|
{ |
668
|
|
|
$subset = []; |
669
|
|
|
$index = 0; |
670
|
|
|
if (empty($this->variantSourceWidths)) { |
671
|
|
|
return $subset; |
672
|
|
|
} |
673
|
|
|
foreach ($this->variantSourceWidths as $variantSourceWidth) { |
674
|
|
|
$match = false; |
675
|
|
|
switch ($comparison) { |
676
|
|
|
case 'width': |
|
|
|
|
677
|
|
|
if ($variantSourceWidth == $width) { |
|
|
|
|
678
|
|
|
$match = true; |
679
|
|
|
} |
|
|
|
|
680
|
|
|
break; |
681
|
|
|
|
682
|
|
|
case 'minwidth': |
|
|
|
|
683
|
|
|
if ($variantSourceWidth >= $width) { |
|
|
|
|
684
|
|
|
$match = true; |
685
|
|
|
} |
|
|
|
|
686
|
|
|
break; |
687
|
|
|
|
688
|
|
|
case 'maxwidth': |
|
|
|
|
689
|
|
|
if ($variantSourceWidth <= $width) { |
|
|
|
|
690
|
|
|
$match = true; |
691
|
|
|
} |
|
|
|
|
692
|
|
|
break; |
693
|
|
|
} |
694
|
|
|
if ($match) { |
695
|
|
|
$subset += array_slice($set, $index, 1, true); |
696
|
|
|
} |
697
|
|
|
$index++; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
return $subset; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
|
|
|
|
704
|
|
|
* @param array $array |
|
|
|
|
705
|
|
|
* @param bool $dpr |
|
|
|
|
706
|
|
|
* |
707
|
|
|
* @return string |
708
|
|
|
*/ |
709
|
|
|
protected function getSrcsetFromArray(array $array, bool $dpr = false): string |
710
|
|
|
{ |
711
|
|
|
$srcset = ''; |
712
|
|
|
foreach ($array as $key => $value) { |
713
|
|
|
if ($dpr) { |
714
|
|
|
$descriptor = '1x'; |
715
|
|
|
if (!empty($array[(int)$key / 2])) { |
716
|
|
|
$descriptor = '2x'; |
717
|
|
|
} |
718
|
|
|
if (!empty($array[(int)$key / 3])) { |
719
|
|
|
$descriptor = '3x'; |
720
|
|
|
} |
721
|
|
|
} else { |
722
|
|
|
$descriptor = $key.'w'; |
723
|
|
|
} |
724
|
|
|
$srcset .= $value.' '.$descriptor.', '; |
725
|
|
|
} |
726
|
|
|
$srcset = rtrim($srcset, ', '); |
727
|
|
|
|
728
|
|
|
return $srcset; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Return a default placeholder image |
733
|
|
|
* |
734
|
|
|
* @return \Twig\Markup |
735
|
|
|
*/ |
736
|
|
|
protected function defaultPlaceholderImage(): \Twig\Markup |
737
|
|
|
{ |
738
|
|
|
$width = 1; |
739
|
|
|
$height = 1; |
740
|
|
|
$color = '#CCC'; |
741
|
|
|
|
742
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* Swap the tag attributes to work with lazy loading |
747
|
|
|
* |
748
|
|
|
* @param array $attrs |
|
|
|
|
749
|
|
|
* @param string $lazyLoad |
|
|
|
|
750
|
|
|
* @return array |
|
|
|
|
751
|
|
|
*/ |
752
|
|
|
protected function swapLazyLoadAttrs(string $lazyLoad, array $attrs): array |
753
|
|
|
{ |
754
|
|
|
if (isset($attrs['class'])) { |
755
|
|
|
$attrs['class'] = trim($attrs['class'] . ' lazyload'); |
756
|
|
|
} |
757
|
|
|
if (!empty($attrs['loading'])) { |
758
|
|
|
$attrs['loading'] = 'lazy'; |
759
|
|
|
} |
760
|
|
|
if (!empty($attrs['srcset'])) { |
761
|
|
|
$attrs['data-srcset'] = $attrs['srcset']; |
762
|
|
|
$attrs['srcset'] = ''; |
763
|
|
|
} |
764
|
|
|
if (!empty($attrs['src'])) { |
765
|
|
|
$attrs['data-src'] = $attrs['src']; |
766
|
|
|
$attrs['src'] = $this->getLazyLoadSrc($lazyLoad); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
return $attrs; |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Return a lazy loading placeholder image based on the passed in $lazyload setting |
774
|
|
|
* |
775
|
|
|
* @param string $lazyLoad |
|
|
|
|
776
|
|
|
* @param array $attrs |
|
|
|
|
777
|
|
|
* |
778
|
|
|
* @return array |
779
|
|
|
*/ |
780
|
|
|
protected function getLazyLoadSrc(string $lazyLoad): string |
781
|
|
|
{ |
782
|
|
|
$result = ''; |
783
|
|
|
if (is_string($lazyLoad)) { |
|
|
|
|
784
|
|
|
$lazyLoad = strtolower($lazyLoad); |
785
|
|
|
} |
786
|
|
|
switch ($lazyLoad) { |
787
|
|
|
case 'image': |
|
|
|
|
788
|
|
|
$result = $this->getPlaceholderImage(); |
789
|
|
|
break; |
790
|
|
|
case 'silhouette': |
|
|
|
|
791
|
|
|
$result = $this->getPlaceholderSilhouette(); |
792
|
|
|
break; |
793
|
|
|
case 'color': |
|
|
|
|
794
|
|
|
$result = $this->getPlaceholderBox($this->colorPalette[0] ?? null); |
795
|
|
|
break; |
796
|
|
|
default: |
|
|
|
|
797
|
|
|
$result = $this->getPlaceholderBox(); |
798
|
|
|
break; |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
return $result; |
|
|
|
|
802
|
|
|
} |
803
|
|
|
} |
804
|
|
|
|