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
|
|
|
use craft\helpers\Html; |
14
|
|
|
use craft\helpers\Template; |
15
|
|
|
use Twig\Markup; |
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
19
|
|
|
* @package ImageOptimize |
|
|
|
|
20
|
|
|
* @since 5.0.0-beta.1 |
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class PictureTag extends BaseImageTag |
23
|
|
|
{ |
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @var string The loading scheme to use: 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
26
|
|
|
*/ |
27
|
|
|
public string $loadingStrategy = 'eager'; |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var string The type of placeholder image to use: 'box', 'color', 'image', 'silhouette' |
31
|
|
|
*/ |
32
|
|
|
public string $placeholder = 'box'; |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @var array array of tag attributes for the <picture> tag |
36
|
|
|
*/ |
37
|
|
|
public array $pictureAttrs = []; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @var array array of tag attributes for the <source> tags |
41
|
|
|
*/ |
42
|
|
|
public array $sourceAttrs = []; |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var array array of tag attributes for the <img> tag |
46
|
|
|
*/ |
47
|
|
|
public array $imgAttrs = []; |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @param $config |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function __construct($config = []) |
53
|
|
|
{ |
54
|
|
|
parent::__construct($config); |
55
|
|
|
// Populate the $imageAttrs |
56
|
|
|
$this->imgAttrs = [ |
57
|
|
|
'class' => '', |
58
|
|
|
'style' => '', |
59
|
|
|
'width' => $this->optimizedImage->placeholderWidth, |
60
|
|
|
'height' => $this->optimizedImage->placeholderHeight, |
61
|
|
|
'src' => reset($this->optimizedImage->optimizedImageUrls), |
|
|
|
|
62
|
|
|
'loading' => '', |
63
|
|
|
]; |
64
|
|
|
// Populate the $sourceAttrs |
65
|
|
|
$this->populateSourceAttrs($this->optimizedImage, []); |
|
|
|
|
66
|
|
|
// Populate the $pictureAttrs |
67
|
|
|
$this->pictureAttrs = []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the $loading property |
72
|
|
|
* |
73
|
|
|
* @param string $value |
|
|
|
|
74
|
|
|
* @return $this |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function loadingStrategy(string $value): PictureTag |
77
|
|
|
{ |
78
|
|
|
$this->loadingStrategy = $value; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the $placeholder property |
85
|
|
|
* |
86
|
|
|
* @param string $value |
|
|
|
|
87
|
|
|
* @return $this |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function placeholder(string $value): PictureTag |
90
|
|
|
{ |
91
|
|
|
$this->placeholder = $value; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Merge the passed array of tag attributes into $pictureAttrs |
98
|
|
|
* |
99
|
|
|
* @param array $value |
|
|
|
|
100
|
|
|
* @return $this |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
public function pictureAttrs(array $value): PictureTag |
103
|
|
|
{ |
104
|
|
|
$this->pictureAttrs = array_merge($this->pictureAttrs, $value); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Merge the passed array of tag attributes into $sourceAttrs |
111
|
|
|
* |
112
|
|
|
* @param array $value |
|
|
|
|
113
|
|
|
* @return $this |
|
|
|
|
114
|
|
|
*/ |
115
|
|
|
public function sourceAttrs(array $value): PictureTag |
116
|
|
|
{ |
117
|
|
|
foreach ($this->sourceAttrs as &$attrs) { |
118
|
|
|
$attrs = array_merge($attrs, $value); |
119
|
|
|
} |
120
|
|
|
unset($attrs); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Merge the passed array of tag attributes into $imgAttrs |
127
|
|
|
* |
128
|
|
|
* @param array $value |
|
|
|
|
129
|
|
|
* @return $this |
|
|
|
|
130
|
|
|
*/ |
131
|
|
|
public function imgAttrs(array $value): PictureTag |
132
|
|
|
{ |
133
|
|
|
$this->imgAttrs = array_merge($this->imgAttrs, $value); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add art direction sources to the $sourceAttrs |
140
|
|
|
* |
141
|
|
|
* @param OptimizedImage $optimizedImage |
|
|
|
|
142
|
|
|
* @param array $sourceAttrs |
|
|
|
|
143
|
|
|
* @return PictureTag |
|
|
|
|
144
|
|
|
*/ |
145
|
|
|
public function addSourceFrom(OptimizedImage $optimizedImage, array $sourceAttrs = []): PictureTag |
146
|
|
|
{ |
147
|
|
|
$this->populateSourceAttrs($optimizedImage, $sourceAttrs); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Generate a complete <img> tag for the $optimizedImage OptimizedImage model |
154
|
|
|
* |
155
|
|
|
* @return Markup |
156
|
|
|
*/ |
157
|
|
|
public function render(): Markup |
158
|
|
|
{ |
159
|
|
|
$content = ''; |
160
|
|
|
// Handle the <source> tag(s) |
161
|
|
|
foreach ($this->sourceAttrs as $attrs) { |
162
|
|
|
// Handle lazy loading |
163
|
|
|
if ($this->loadingStrategy !== 'eager') { |
164
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
165
|
|
|
} |
166
|
|
|
// Remove any empty attributes |
167
|
|
|
$attrs = array_filter($attrs); |
168
|
|
|
// Render the tag |
169
|
|
|
$content .= Html::tag('source', '', $attrs); |
170
|
|
|
} |
171
|
|
|
// Handle the <img> tag |
172
|
|
|
$attrs = $this->imgAttrs; |
173
|
|
|
// Handle lazy loading |
174
|
|
|
if ($this->loadingStrategy !== 'eager') { |
175
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
176
|
|
|
} |
177
|
|
|
// Remove any empty attributes |
178
|
|
|
$attrs = array_filter($attrs); |
179
|
|
|
// Render the tag |
180
|
|
|
$content .= Html::tag('img', '', $attrs); |
181
|
|
|
// Handle the <picture> tag |
182
|
|
|
$attrs = $this->pictureAttrs; |
183
|
|
|
// Remove any empty attributes |
184
|
|
|
$attrs = array_filter($attrs); |
185
|
|
|
// Render the tag |
186
|
|
|
$tag = Html::tag('picture', $content, $attrs); |
187
|
|
|
|
188
|
|
|
return Template::raw($tag); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Populate the $sourceAttrs from the passed in $optimizedImage and $sizes |
193
|
|
|
* |
194
|
|
|
* @param OptimizedImage $optimizedImage |
|
|
|
|
195
|
|
|
* @param array $sourceAttrs attributes to add to the $sourceAttrs array |
|
|
|
|
196
|
|
|
* @return void |
|
|
|
|
197
|
|
|
*/ |
198
|
|
|
protected function populateSourceAttrs(OptimizedImage $optimizedImage, array $sourceAttrs): void |
199
|
|
|
{ |
200
|
|
|
if (!empty($optimizedImage->optimizedWebPImageUrls)) { |
201
|
|
|
$this->sourceAttrs[] = array_merge([ |
|
|
|
|
202
|
|
|
'media' => '', |
203
|
|
|
'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedWebPImageUrls), |
204
|
|
|
'type' => 'image/webp', |
205
|
|
|
'sizes' => '100vw', |
206
|
|
|
'width' => $optimizedImage->placeholderWidth, |
207
|
|
|
'height' => $optimizedImage->placeholderHeight, |
208
|
|
|
], $sourceAttrs); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
$this->sourceAttrs[] = array_merge([ |
|
|
|
|
211
|
|
|
'media' => '', |
212
|
|
|
'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedImageUrls), |
|
|
|
|
213
|
|
|
'sizes' => '100vw', |
214
|
|
|
'width' => $optimizedImage->placeholderWidth, |
215
|
|
|
'height' => $optimizedImage->placeholderHeight, |
216
|
|
|
], $sourceAttrs); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|