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', or 'none' |
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
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
public function init(): void |
53
|
|
|
{ |
54
|
|
|
parent::init(); |
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
|
|
|
// If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended |
69
|
|
|
$path = parse_url($this->imgAttrs['src'], PHP_URL_PATH); |
70
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
71
|
|
|
if ($extension === 'svg' || $extension === 'gif') { |
72
|
|
|
$this->placeholder = 'none'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the $loading property |
78
|
|
|
* |
79
|
|
|
* @param string $value |
|
|
|
|
80
|
|
|
* @return $this |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
public function loadingStrategy(string $value): PictureTag |
83
|
|
|
{ |
84
|
|
|
$this->loadingStrategy = $value; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the $placeholder property |
91
|
|
|
* |
92
|
|
|
* @param string $value |
|
|
|
|
93
|
|
|
* @return $this |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function placeholder(string $value): PictureTag |
96
|
|
|
{ |
97
|
|
|
$this->placeholder = $value; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Merge the passed array of tag attributes into $pictureAttrs |
104
|
|
|
* |
105
|
|
|
* @param array $value |
|
|
|
|
106
|
|
|
* @return $this |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public function pictureAttrs(array $value): PictureTag |
109
|
|
|
{ |
110
|
|
|
$this->pictureAttrs = array_merge($this->pictureAttrs, $value); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Merge the passed array of tag attributes into $sourceAttrs |
117
|
|
|
* |
118
|
|
|
* @param array $value |
|
|
|
|
119
|
|
|
* @return $this |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public function sourceAttrs(array $value): PictureTag |
122
|
|
|
{ |
123
|
|
|
foreach ($this->sourceAttrs as &$attrs) { |
124
|
|
|
$attrs = array_merge($attrs, $value); |
125
|
|
|
} |
126
|
|
|
unset($attrs); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Merge the passed array of tag attributes into $imgAttrs |
133
|
|
|
* |
134
|
|
|
* @param array $value |
|
|
|
|
135
|
|
|
* @return $this |
|
|
|
|
136
|
|
|
*/ |
137
|
|
|
public function imgAttrs(array $value): PictureTag |
138
|
|
|
{ |
139
|
|
|
$this->imgAttrs = array_merge($this->imgAttrs, $value); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add art direction sources to the $sourceAttrs |
146
|
|
|
* |
147
|
|
|
* @param OptimizedImage $optimizedImage |
|
|
|
|
148
|
|
|
* @param array $sourceAttrs |
|
|
|
|
149
|
|
|
* @return PictureTag |
|
|
|
|
150
|
|
|
*/ |
151
|
|
|
public function addSourceFrom(OptimizedImage $optimizedImage, array $sourceAttrs = []): PictureTag |
152
|
|
|
{ |
153
|
|
|
$this->populateSourceAttrs($optimizedImage, $sourceAttrs); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Generate a complete <img> tag for the $optimizedImage OptimizedImage model |
160
|
|
|
* |
161
|
|
|
* @return Markup |
162
|
|
|
*/ |
163
|
|
|
public function render(): Markup |
164
|
|
|
{ |
165
|
|
|
$content = ''; |
166
|
|
|
// Handle the <source> tag(s) |
167
|
|
|
foreach ($this->sourceAttrs as $attrs) { |
168
|
|
|
// Handle lazy loading |
169
|
|
|
if ($this->loadingStrategy !== 'eager') { |
170
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
171
|
|
|
} |
172
|
|
|
// Remove any empty attributes |
173
|
|
|
$attrs = array_filter($attrs); |
174
|
|
|
// Render the tag |
175
|
|
|
$content .= Html::tag('source', '', $attrs); |
176
|
|
|
} |
177
|
|
|
// Handle the <img> tag |
178
|
|
|
$attrs = $this->imgAttrs; |
179
|
|
|
// Handle lazy loading |
180
|
|
|
if ($this->loadingStrategy !== 'eager') { |
181
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
182
|
|
|
} |
183
|
|
|
// Remove any empty attributes |
184
|
|
|
$attrs = $this->filterEmptyAttributes($attrs); |
185
|
|
|
// Render the tag |
186
|
|
|
$content .= Html::tag('img', '', $attrs); |
187
|
|
|
// Handle the <picture> tag |
188
|
|
|
$attrs = $this->pictureAttrs; |
189
|
|
|
// Remove any empty attributes |
190
|
|
|
$attrs = array_filter($attrs); |
191
|
|
|
// Render the tag |
192
|
|
|
$tag = Html::tag('picture', $content, $attrs); |
193
|
|
|
|
194
|
|
|
return Template::raw($tag); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Populate the $sourceAttrs from the passed in $optimizedImage and $sizes |
199
|
|
|
* |
200
|
|
|
* @param OptimizedImage $optimizedImage |
|
|
|
|
201
|
|
|
* @param array $sourceAttrs attributes to add to the $sourceAttrs array |
|
|
|
|
202
|
|
|
* @return void |
|
|
|
|
203
|
|
|
*/ |
204
|
|
|
protected function populateSourceAttrs(OptimizedImage $optimizedImage, array $sourceAttrs): void |
205
|
|
|
{ |
206
|
|
|
if (!empty($optimizedImage->optimizedWebPImageUrls)) { |
207
|
|
|
$this->sourceAttrs[] = array_merge([ |
|
|
|
|
208
|
|
|
'media' => '', |
209
|
|
|
'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedWebPImageUrls), |
210
|
|
|
'type' => 'image/webp', |
211
|
|
|
'sizes' => '100vw', |
212
|
|
|
'width' => $optimizedImage->placeholderWidth, |
213
|
|
|
'height' => $optimizedImage->placeholderHeight, |
214
|
|
|
], $sourceAttrs); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
$this->sourceAttrs[] = array_merge([ |
|
|
|
|
217
|
|
|
'media' => '', |
218
|
|
|
'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedImageUrls), |
|
|
|
|
219
|
|
|
'sizes' => '100vw', |
220
|
|
|
'width' => $optimizedImage->placeholderWidth, |
221
|
|
|
'height' => $optimizedImage->placeholderHeight, |
222
|
|
|
], $sourceAttrs); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|