|
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
|
|
|
if (empty($attrs['style'])) { |
|
39
|
|
|
$attrs['style'] = []; |
|
40
|
|
|
} |
|
41
|
|
|
// Don't add the background placeholder if it is set to 'none' |
|
42
|
|
|
if ($placeHolder !== 'none') { |
|
43
|
|
|
$attrs['style']['background-image'] = 'url(' . $this->getLazyLoadSrc($placeHolder) . ')'; |
|
44
|
|
|
$attrs['style']['background-size'] = 'cover'; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
// Handle attributes that lazy and lazySizesFallback have in common |
|
48
|
|
|
switch ($loading) { |
|
49
|
|
|
case 'lazy': |
|
|
|
|
|
|
50
|
|
|
case 'lazySizesFallback': |
|
|
|
|
|
|
51
|
|
|
if (isset($attrs['loading'])) { |
|
|
|
|
|
|
52
|
|
|
$attrs['loading'] = 'lazy'; |
|
53
|
|
|
} |
|
|
|
|
|
|
54
|
|
|
break; |
|
55
|
|
|
default: |
|
|
|
|
|
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
// Handle attributes that lazySizes and lazySizesFallback have in common |
|
59
|
|
|
switch ($loading) { |
|
60
|
|
|
case 'lazySizes': |
|
|
|
|
|
|
61
|
|
|
case 'lazySizesFallback': |
|
|
|
|
|
|
62
|
|
|
// Only swap to data- attributes if they want the LazySizes fallback |
|
63
|
|
|
if (!empty($attrs['sizes'])) { |
|
|
|
|
|
|
64
|
|
|
$attrs['data-sizes'] = $attrs['sizes']; |
|
65
|
|
|
$attrs['sizes'] = ''; |
|
66
|
|
|
} |
|
|
|
|
|
|
67
|
|
|
if (!empty($attrs['srcset'])) { |
|
|
|
|
|
|
68
|
|
|
$attrs['data-srcset'] = $attrs['srcset']; |
|
69
|
|
|
$attrs['srcset'] = ''; |
|
70
|
|
|
} |
|
|
|
|
|
|
71
|
|
|
if (!empty($attrs['src'])) { |
|
|
|
|
|
|
72
|
|
|
$attrs['data-src'] = $attrs['src']; |
|
73
|
|
|
$attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
|
74
|
|
|
} |
|
|
|
|
|
|
75
|
|
|
break; |
|
76
|
|
|
default: |
|
|
|
|
|
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $attrs; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return a lazy loading placeholder image based on the passed in $lazyload setting |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $lazyLoad |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getLazyLoadSrc(string $lazyLoad): string |
|
91
|
|
|
{ |
|
92
|
|
|
$lazyLoad = strtolower($lazyLoad); |
|
93
|
|
|
return match ($lazyLoad) { |
|
94
|
|
|
'image' => $this->optimizedImage->getPlaceholderImage(), |
|
|
|
|
|
|
95
|
|
|
'silhouette' => $this->optimizedImage->getPlaceholderSilhouette(), |
|
96
|
|
|
'color' => $this->optimizedImage->getPlaceholderBox($this->colorPalette[0] ?? null), |
|
|
|
|
|
|
97
|
|
|
default => $this->optimizedImage->getPlaceholderBox('#CCC'), |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|