1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\models; |
12
|
|
|
|
13
|
|
|
/** |
|
|
|
|
14
|
|
|
* @author nystudio107 |
|
|
|
|
15
|
|
|
* @package ImageOptimize |
|
|
|
|
16
|
|
|
* @since 5.0.0-beta.1 |
|
|
|
|
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
abstract class BaseImageTag extends BaseTag |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Swap the tag attributes to work with lazy loading |
22
|
|
|
* ref: https://web.dev/native-lazy-loading/#how-do-i-handle-browsers-that-don't-yet-support-native-lazy-loading |
23
|
|
|
* |
24
|
|
|
* @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
|
|
|
|
25
|
|
|
* @param string $placeHolder 'box', 'color', 'image', 'silhouette' |
26
|
|
|
* @param array $attrs |
|
|
|
|
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
protected function swapLazyLoadAttrs(string $loading, string $placeHolder, array $attrs): array |
31
|
|
|
{ |
32
|
|
|
// Set the class and loading attributes |
33
|
|
|
if (isset($attrs['class'])) { |
34
|
|
|
$attrs['class'] = trim($attrs['class'] . ' lazyload'); |
35
|
|
|
} |
36
|
|
|
// Set the style on this element to be the placeholder image as the background-image |
37
|
|
|
if (isset($attrs['style']) && !empty($attrs['src'])) { |
38
|
|
|
$attrs['style'] = trim( |
39
|
|
|
$attrs['style'] . |
40
|
|
|
'background-image:url(' . $this->getLazyLoadSrc($placeHolder) . '); background-size: cover;' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
// Handle attributes that lazy and lazySizesFallback have in common |
44
|
|
|
switch ($loading) { |
45
|
|
|
case 'lazy': |
|
|
|
|
46
|
|
|
case 'lazySizesFallback': |
|
|
|
|
47
|
|
|
if (isset($attrs['loading'])) { |
|
|
|
|
48
|
|
|
$attrs['loading'] = 'lazy'; |
49
|
|
|
} |
|
|
|
|
50
|
|
|
break; |
51
|
|
|
default: |
|
|
|
|
52
|
|
|
break; |
53
|
|
|
} |
54
|
|
|
// Handle attributes that lazySizes and lazySizesFallback have in common |
55
|
|
|
switch ($loading) { |
56
|
|
|
case 'lazySizes': |
|
|
|
|
57
|
|
|
case 'lazySizesFallback': |
|
|
|
|
58
|
|
|
// Only swap to data- attributes if they want the LazySizes fallback |
59
|
|
|
if (!empty($attrs['sizes'])) { |
|
|
|
|
60
|
|
|
$attrs['data-sizes'] = $attrs['sizes']; |
61
|
|
|
$attrs['sizes'] = ''; |
62
|
|
|
} |
|
|
|
|
63
|
|
|
if (!empty($attrs['srcset'])) { |
|
|
|
|
64
|
|
|
$attrs['data-srcset'] = $attrs['srcset']; |
65
|
|
|
$attrs['srcset'] = ''; |
66
|
|
|
} |
|
|
|
|
67
|
|
|
if (!empty($attrs['src'])) { |
|
|
|
|
68
|
|
|
$attrs['data-src'] = $attrs['src']; |
69
|
|
|
$attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
70
|
|
|
} |
|
|
|
|
71
|
|
|
break; |
72
|
|
|
default: |
|
|
|
|
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $attrs; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return a lazy loading placeholder image based on the passed in $lazyload setting |
81
|
|
|
* |
82
|
|
|
* @param string $lazyLoad |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getLazyLoadSrc(string $lazyLoad): string |
87
|
|
|
{ |
88
|
|
|
$lazyLoad = strtolower($lazyLoad); |
89
|
|
|
return match ($lazyLoad) { |
90
|
|
|
'image' => $this->optimizedImage->getPlaceholderImage(), |
|
|
|
|
91
|
|
|
'silhouette' => $this->optimizedImage->getPlaceholderSilhouette(), |
92
|
|
|
'color' => $this->optimizedImage->getPlaceholderBox($this->colorPalette[0] ?? null), |
|
|
|
|
93
|
|
|
default => $this->optimizedImage->getPlaceholderBox('#CCC'), |
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|