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