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 <link rel="preload"> tag for this OptimizedImages model |
382
|
|
|
* |
383
|
|
|
* @param array $linkAttrs |
|
|
|
|
384
|
|
|
* |
385
|
|
|
* @return \Twig\Markup |
386
|
|
|
*/ |
387
|
|
|
public function linkPreloadTag($linkAttrs = []) |
388
|
|
|
{ |
389
|
|
|
// Any web browser that supports link rel="preload" as="image" also supports webp, so prefer that |
390
|
|
|
$srcset = $this->optimizedImageUrls; |
391
|
|
|
if (!empty($this->optimizedWebPImageUrls)) { |
392
|
|
|
$srcset = $this->optimizedWebPImageUrls; |
393
|
|
|
} |
394
|
|
|
// Merge the passed in options with the tag attributes |
395
|
|
|
$attrs = array_merge([ |
|
|
|
|
396
|
|
|
'rel' => 'preload', |
397
|
|
|
'as' => 'image', |
398
|
|
|
'href' => reset($srcset), |
399
|
|
|
'imagesrcset' => $this->getSrcsetFromArray($srcset), |
400
|
|
|
'imagesizes' => '100vw', |
401
|
|
|
], |
|
|
|
|
402
|
|
|
$linkAttrs |
403
|
|
|
); |
404
|
|
|
// Remove any empty attributes |
405
|
|
|
$attrs = array_filter($attrs); |
406
|
|
|
// Render the tag |
407
|
|
|
$tag = Html::tag('link', '', $attrs); |
408
|
|
|
|
409
|
|
|
return Template::raw($tag); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Generate a complete <img> tag for this OptimizedImages model |
414
|
|
|
* |
415
|
|
|
* @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
|
|
|
|
416
|
|
|
* @param string $placeHolder 'box', 'color', 'image', 'silhouette' |
417
|
|
|
* @param array $imgAttrs |
|
|
|
|
418
|
|
|
* |
419
|
|
|
* @return \Twig\Markup |
420
|
|
|
*/ |
421
|
|
|
public function imgTag($loading = 'eager', $placeHolder = 'box', $imgAttrs = []) |
422
|
|
|
{ |
423
|
|
|
// Merge the passed in options with the tag attributes |
424
|
|
|
$attrs = array_merge([ |
|
|
|
|
425
|
|
|
'class' => '', |
426
|
|
|
'style' => '', |
427
|
|
|
'width' => $this->placeholderWidth, |
428
|
|
|
'height' => $this->placeholderHeight, |
429
|
|
|
'src' => reset($this->optimizedImageUrls), |
430
|
|
|
'srcset' => $this->getSrcsetFromArray($this->optimizedImageUrls), |
431
|
|
|
'sizes' => '100vw', |
432
|
|
|
'loading' => '', |
433
|
|
|
], |
434
|
|
|
$imgAttrs |
435
|
|
|
); |
436
|
|
|
// Handle lazy loading |
437
|
|
|
if ($loading !== 'eager') { |
438
|
|
|
$attrs = $this->swapLazyLoadAttrs($loading, $placeHolder, $attrs); |
439
|
|
|
} |
440
|
|
|
// Remove any empty attributes |
441
|
|
|
$attrs = array_filter($attrs); |
442
|
|
|
// Render the tag |
443
|
|
|
$tag = Html::tag('img', '', $attrs); |
444
|
|
|
|
445
|
|
|
return Template::raw($tag); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Generate a complete <picture> tag for this OptimizedImages model |
450
|
|
|
* |
451
|
|
|
* @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
|
|
|
|
452
|
|
|
* @param string $placeHolder 'box', 'color', 'image', 'silhouette' |
|
|
|
|
453
|
|
|
* @param array $pictureAttrs |
|
|
|
|
454
|
|
|
* @param array $srcsetAttrs |
|
|
|
|
455
|
|
|
* @param array $imgAttrs |
|
|
|
|
456
|
|
|
* |
457
|
|
|
* @return \Twig\Markup |
458
|
|
|
*/ |
459
|
|
|
public function pictureTag($loading = 'eager', $placeHolder = 'box', $pictureAttrs = [], $srcsetAttrs = [], $imgAttrs = []) |
460
|
|
|
{ |
461
|
|
|
$content = ''; |
462
|
|
|
// Handle the webp srcset |
463
|
|
|
if (!empty($this->optimizedWebPImageUrls)) { |
464
|
|
|
// Merge the passed in options with the tag attributes |
465
|
|
|
$attrs = array_merge([ |
|
|
|
|
466
|
|
|
'srcset' => $this->getSrcsetFromArray($this->optimizedWebPImageUrls), |
467
|
|
|
'type' => 'image/webp', |
468
|
|
|
'sizes' => '100vw', |
469
|
|
|
], |
470
|
|
|
$srcsetAttrs |
471
|
|
|
); |
472
|
|
|
// Handle lazy loading |
473
|
|
|
if ($loading !== 'eager') { |
474
|
|
|
$attrs = $this->swapLazyLoadAttrs($loading, $placeHolder, $attrs); |
475
|
|
|
} |
476
|
|
|
// Remove any empty attributes |
477
|
|
|
$attrs = array_filter($attrs); |
478
|
|
|
// Render the tag |
479
|
|
|
$content .= Html::tag('source', '', $attrs); |
480
|
|
|
} |
481
|
|
|
// Handle the regular srcset |
482
|
|
|
if (!empty($this->optimizedImageUrls)) { |
483
|
|
|
// Merge the passed in options with the tag attributes |
484
|
|
|
$attrs = array_merge([ |
|
|
|
|
485
|
|
|
'srcset' => $this->getSrcsetFromArray($this->optimizedImageUrls), |
486
|
|
|
'sizes' => '100vw', |
487
|
|
|
], |
488
|
|
|
$srcsetAttrs |
489
|
|
|
); |
490
|
|
|
// Handle lazy loading |
491
|
|
|
if ($loading !== 'eager') { |
492
|
|
|
$attrs = $this->swapLazyLoadAttrs($loading, $placeHolder, $attrs); |
493
|
|
|
} |
494
|
|
|
// Remove any empty attributes |
495
|
|
|
$attrs = array_filter($attrs); |
496
|
|
|
// Render the tag |
497
|
|
|
$content .= Html::tag('source', '', $attrs); |
498
|
|
|
} |
499
|
|
|
// Handle the img tag |
500
|
|
|
/** @noinspection SuspiciousAssignmentsInspection */ |
|
|
|
|
501
|
|
|
$attrs = array_merge([ |
|
|
|
|
502
|
|
|
'class' => '', |
503
|
|
|
'style' => '', |
504
|
|
|
'width' => $this->placeholderWidth, |
505
|
|
|
'height' => $this->placeholderHeight, |
506
|
|
|
'src' => reset($this->optimizedImageUrls), |
507
|
|
|
'loading' => '', |
508
|
|
|
], |
509
|
|
|
$imgAttrs |
510
|
|
|
); |
511
|
|
|
// Handle lazy loading |
512
|
|
|
if ($loading !== 'eager') { |
513
|
|
|
$attrs = $this->swapLazyLoadAttrs($loading, $placeHolder, $attrs); |
514
|
|
|
} |
515
|
|
|
// Remove any empty attributes |
516
|
|
|
$attrs = array_filter($attrs); |
517
|
|
|
// Render the tag |
518
|
|
|
$content .= Html::tag('img', '', $attrs); |
519
|
|
|
// Merge the passed in options with the tag attributes |
520
|
|
|
$attrs = array_merge([ |
|
|
|
|
521
|
|
|
], |
522
|
|
|
$pictureAttrs |
523
|
|
|
); |
524
|
|
|
// Remove any empty attributes |
525
|
|
|
$attrs = array_filter($attrs); |
526
|
|
|
// Render the tag |
527
|
|
|
$tag = Html::tag('picture', $content, $attrs); |
528
|
|
|
|
529
|
|
|
return Template::raw($tag); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Return a base64-encoded placeholder image |
534
|
|
|
* |
535
|
|
|
* @return \Twig\Markup|null |
536
|
|
|
*/ |
537
|
|
|
public function placeholderImage() |
538
|
|
|
{ |
539
|
|
|
$header = 'data:image/jpeg;base64,'; |
540
|
|
|
if (!empty($this->placeholder)) { |
541
|
|
|
$content = $this->placeholder; |
542
|
|
|
} else { |
543
|
|
|
// At least return something |
544
|
|
|
return $this->defaultPlaceholderImage(); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return Template::raw($header.rawurlencode($content)); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Getter for CraftQL |
552
|
|
|
* |
553
|
|
|
* @return string |
554
|
|
|
*/ |
555
|
|
|
public function getPlaceholderImage(): string |
556
|
|
|
{ |
557
|
|
|
return (string)$this->placeholderImage(); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
|
|
|
|
561
|
|
|
* @return string |
562
|
|
|
*/ |
563
|
|
|
public function placeholderImageSize(): string |
564
|
|
|
{ |
565
|
|
|
$placeholder = $this->placeholderImage(); |
566
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
567
|
|
|
|
568
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Return an SVG box as a placeholder image |
573
|
|
|
* |
574
|
|
|
* @param string|null $color |
|
|
|
|
575
|
|
|
* |
576
|
|
|
* @return \Twig\Markup|null |
577
|
|
|
*/ |
578
|
|
|
public function placeholderBox(string $color = null) |
579
|
|
|
{ |
580
|
|
|
$width = $this->placeholderWidth ?? 1; |
581
|
|
|
$height = $this->placeholderHeight ?? 1; |
582
|
|
|
$color = $color ?? $this->colorPalette[0] ?? '#CCC'; |
583
|
|
|
|
584
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
|
|
|
|
588
|
|
|
* @param string|null $color |
|
|
|
|
589
|
|
|
* |
590
|
|
|
* @return string |
591
|
|
|
*/ |
592
|
|
|
public function getPlaceholderBox(string $color = null): string |
593
|
|
|
{ |
594
|
|
|
return (string)$this->placeholderBox($color); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Getter for CraftQL |
599
|
|
|
* |
600
|
|
|
* @return string |
601
|
|
|
*/ |
602
|
|
|
public function placeholderBoxSize(): string |
603
|
|
|
{ |
604
|
|
|
$placeholder = $this->placeholderBox(); |
605
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
606
|
|
|
|
607
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Return a silhouette of the image as an SVG placeholder |
612
|
|
|
* |
613
|
|
|
* @return \Twig\Markup|null |
614
|
|
|
*/ |
615
|
|
|
public function placeholderSilhouette() |
616
|
|
|
{ |
617
|
|
|
$header = 'data:image/svg+xml,'; |
618
|
|
|
if (!empty($this->placeholderSvg)) { |
619
|
|
|
$content = $this->placeholderSvg; |
620
|
|
|
} else { |
621
|
|
|
// At least return something |
622
|
|
|
return $this->defaultPlaceholderImage(); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
return Template::raw($header.$content); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Getter for CraftQL |
630
|
|
|
* |
631
|
|
|
* @return string |
632
|
|
|
*/ |
633
|
|
|
public function getPlaceholderSilhouette(): string |
634
|
|
|
{ |
635
|
|
|
return (string)$this->placeholderSilhouette(); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
|
|
|
|
639
|
|
|
* @return string |
640
|
|
|
*/ |
641
|
|
|
public function placeholderSilhouetteSize(): string |
642
|
|
|
{ |
643
|
|
|
$placeholder = $this->placeholderSilhouette(); |
644
|
|
|
$contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
645
|
|
|
|
646
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Get the file size of any remote resource (using curl), |
651
|
|
|
* either in bytes or - default - as human-readable formatted string. |
652
|
|
|
* |
653
|
|
|
* @author Stephan Schmitz <[email protected]> |
654
|
|
|
* @license MIT <http://eyecatchup.mit-license.org/> |
655
|
|
|
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d> |
656
|
|
|
* |
657
|
|
|
* @param string $url Takes the remote object's URL. |
|
|
|
|
658
|
|
|
* @param boolean $formatSize Whether to return size in bytes or |
|
|
|
|
659
|
|
|
* formatted. |
660
|
|
|
* @param boolean $useHead Whether to use HEAD requests. If false, |
|
|
|
|
661
|
|
|
* uses GET. |
662
|
|
|
* |
663
|
|
|
* @return int|mixed|string Returns human-readable formatted size |
|
|
|
|
664
|
|
|
* or size in bytes (default: formatted). |
665
|
|
|
*/ |
666
|
|
|
public function getRemoteFileSize($url, $formatSize = true, $useHead = true) |
667
|
|
|
{ |
668
|
|
|
// Get an absolute URL with protocol that curl will be happy with |
669
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
670
|
|
|
$ch = curl_init($url); |
671
|
|
|
curl_setopt_array($ch, [ |
|
|
|
|
672
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
673
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
674
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
675
|
|
|
]); |
|
|
|
|
676
|
|
|
if ($useHead) { |
677
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, 1); |
678
|
|
|
} |
679
|
|
|
curl_exec($ch); |
680
|
|
|
// content-length of download (in bytes), read from Content-Length: field |
681
|
|
|
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
682
|
|
|
curl_close($ch); |
683
|
|
|
// cannot retrieve file size, return "-1" |
684
|
|
|
if (!$contentLength) { |
685
|
|
|
return -1; |
686
|
|
|
} |
687
|
|
|
// return size in bytes |
688
|
|
|
if (!$formatSize) { |
689
|
|
|
return $contentLength; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
// Protected Methods |
696
|
|
|
// ========================================================================= |
697
|
|
|
|
698
|
|
|
protected function getSrcsetSubsetArray(array $set, int $width, string $comparison): array |
|
|
|
|
699
|
|
|
{ |
700
|
|
|
$subset = []; |
701
|
|
|
$index = 0; |
702
|
|
|
if (empty($this->variantSourceWidths)) { |
703
|
|
|
return $subset; |
704
|
|
|
} |
705
|
|
|
foreach ($this->variantSourceWidths as $variantSourceWidth) { |
706
|
|
|
$match = false; |
707
|
|
|
switch ($comparison) { |
708
|
|
|
case 'width': |
|
|
|
|
709
|
|
|
if ($variantSourceWidth == $width) { |
|
|
|
|
710
|
|
|
$match = true; |
711
|
|
|
} |
|
|
|
|
712
|
|
|
break; |
713
|
|
|
|
714
|
|
|
case 'minwidth': |
|
|
|
|
715
|
|
|
if ($variantSourceWidth >= $width) { |
|
|
|
|
716
|
|
|
$match = true; |
717
|
|
|
} |
|
|
|
|
718
|
|
|
break; |
719
|
|
|
|
720
|
|
|
case 'maxwidth': |
|
|
|
|
721
|
|
|
if ($variantSourceWidth <= $width) { |
|
|
|
|
722
|
|
|
$match = true; |
723
|
|
|
} |
|
|
|
|
724
|
|
|
break; |
725
|
|
|
} |
726
|
|
|
if ($match) { |
727
|
|
|
$subset += array_slice($set, $index, 1, true); |
728
|
|
|
} |
729
|
|
|
$index++; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
return $subset; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
|
|
|
|
736
|
|
|
* @param array $array |
|
|
|
|
737
|
|
|
* @param bool $dpr |
|
|
|
|
738
|
|
|
* |
739
|
|
|
* @return string |
740
|
|
|
*/ |
741
|
|
|
protected function getSrcsetFromArray(array $array, bool $dpr = false): string |
742
|
|
|
{ |
743
|
|
|
$srcset = ''; |
744
|
|
|
foreach ($array as $key => $value) { |
745
|
|
|
if ($dpr) { |
746
|
|
|
$descriptor = '1x'; |
747
|
|
|
if (!empty($array[(int)$key / 2])) { |
748
|
|
|
$descriptor = '2x'; |
749
|
|
|
} |
750
|
|
|
if (!empty($array[(int)$key / 3])) { |
751
|
|
|
$descriptor = '3x'; |
752
|
|
|
} |
753
|
|
|
} else { |
754
|
|
|
$descriptor = $key.'w'; |
755
|
|
|
} |
756
|
|
|
$srcset .= $value.' '.$descriptor.', '; |
757
|
|
|
} |
758
|
|
|
$srcset = rtrim($srcset, ', '); |
759
|
|
|
|
760
|
|
|
return $srcset; |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
/** |
764
|
|
|
* Return a default placeholder image |
765
|
|
|
* |
766
|
|
|
* @return \Twig\Markup |
767
|
|
|
*/ |
768
|
|
|
protected function defaultPlaceholderImage(): \Twig\Markup |
769
|
|
|
{ |
770
|
|
|
$width = 1; |
771
|
|
|
$height = 1; |
772
|
|
|
$color = '#CCC'; |
773
|
|
|
|
774
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
/** |
778
|
|
|
* Swap the tag attributes to work with lazy loading |
779
|
|
|
* |
780
|
|
|
* @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
|
|
|
|
781
|
|
|
* @param string $placeHolder 'box', 'color', 'image', 'silhouette' |
782
|
|
|
* @param array $attrs |
|
|
|
|
783
|
|
|
* |
784
|
|
|
* @return array |
785
|
|
|
*/ |
786
|
|
|
protected function swapLazyLoadAttrs(string $loading, string $placeHolder, array $attrs): array |
787
|
|
|
{ |
788
|
|
|
// Set the class and loading attributes |
789
|
|
|
if (isset($attrs['class'])) { |
790
|
|
|
$attrs['class'] = trim($attrs['class'] . ' lazyload'); |
791
|
|
|
} |
792
|
|
|
// Set the style on this element to be the placeholder image as the background-image |
793
|
|
|
if (isset($attrs['style']) && !empty($attrs['src'])) { |
794
|
|
|
$attrs['style'] = trim( |
795
|
|
|
$attrs['style'] . |
796
|
|
|
'background-image:url(' . $this->getLazyLoadSrc($placeHolder) . '); background-size: cover;' |
797
|
|
|
); |
798
|
|
|
} |
799
|
|
|
// Handle attributes that lazy and lazySizesFallback have in common |
800
|
|
|
switch ($loading) { |
801
|
|
|
case 'lazy': |
|
|
|
|
802
|
|
|
case 'lazySizesFallback': |
|
|
|
|
803
|
|
|
if (isset($attrs['loading'])) { |
804
|
|
|
$attrs['loading'] = 'lazy'; |
805
|
|
|
} |
|
|
|
|
806
|
|
|
break; |
807
|
|
|
default: |
|
|
|
|
808
|
|
|
break; |
809
|
|
|
} |
810
|
|
|
// Handle attributes that lazySizes and lazySizesFallback have in common |
811
|
|
|
switch ($loading) { |
812
|
|
|
case 'lazySizes': |
|
|
|
|
813
|
|
|
case 'lazySizesFallback': |
|
|
|
|
814
|
|
|
// Only swap to data- attributes if they want the LazySizes fallback |
815
|
|
|
if (!empty($attrs['sizes'])) { |
|
|
|
|
816
|
|
|
$attrs['data-sizes'] = $attrs['sizes']; |
817
|
|
|
$attrs['sizes'] = ''; |
818
|
|
|
} |
|
|
|
|
819
|
|
|
if (!empty($attrs['srcset'])) { |
|
|
|
|
820
|
|
|
$attrs['data-srcset'] = $attrs['srcset']; |
821
|
|
|
$attrs['srcset'] = ''; |
822
|
|
|
} |
|
|
|
|
823
|
|
|
if (!empty($attrs['src'])) { |
|
|
|
|
824
|
|
|
$attrs['data-src'] = $attrs['src']; |
825
|
|
|
$attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
826
|
|
|
} |
|
|
|
|
827
|
|
|
break; |
|
|
|
|
828
|
|
|
default: |
|
|
|
|
829
|
|
|
break; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
return $attrs; |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* Return a lazy loading placeholder image based on the passed in $lazyload setting |
837
|
|
|
* |
838
|
|
|
* @param string $lazyLoad |
|
|
|
|
839
|
|
|
* @param array $attrs |
|
|
|
|
840
|
|
|
* |
841
|
|
|
* @return array |
842
|
|
|
*/ |
843
|
|
|
protected function getLazyLoadSrc(string $lazyLoad): string |
844
|
|
|
{ |
845
|
|
|
$result = ''; |
846
|
|
|
if (is_string($lazyLoad)) { |
|
|
|
|
847
|
|
|
$lazyLoad = strtolower($lazyLoad); |
848
|
|
|
} |
849
|
|
|
switch ($lazyLoad) { |
850
|
|
|
case 'image': |
|
|
|
|
851
|
|
|
$result = $this->getPlaceholderImage(); |
852
|
|
|
break; |
853
|
|
|
case 'silhouette': |
|
|
|
|
854
|
|
|
$result = $this->getPlaceholderSilhouette(); |
855
|
|
|
break; |
856
|
|
|
case 'color': |
|
|
|
|
857
|
|
|
$result = $this->getPlaceholderBox($this->colorPalette[0] ?? null); |
858
|
|
|
break; |
859
|
|
|
default: |
|
|
|
|
860
|
|
|
$result = $this->getPlaceholderBox(); |
861
|
|
|
break; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
return $result; |
|
|
|
|
865
|
|
|
} |
866
|
|
|
} |
867
|
|
|
|