1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\fields; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\gql\types\generators\OptimizedImagesGenerator; |
14
|
|
|
use nystudio107\imageoptimize\assetbundles\optimizedimagesfield\OptimizedImagesFieldAsset; |
15
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
16
|
|
|
use nystudio107\imageoptimize\models\OptimizedImage; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
use craft\base\ElementInterface; |
20
|
|
|
use craft\base\Field; |
21
|
|
|
use craft\elements\Asset; |
22
|
|
|
use craft\fields\Matrix; |
23
|
|
|
use craft\helpers\Json; |
24
|
|
|
use craft\validators\ArrayValidator; |
25
|
|
|
|
26
|
|
|
use yii\base\InvalidConfigException; |
27
|
|
|
use yii\db\Exception; |
28
|
|
|
use yii\db\Schema; |
29
|
|
|
|
30
|
|
|
use verbb\supertable\fields\SuperTableField; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** @noinspection MissingPropertyAnnotationsInspection */ |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @author nystudio107 |
|
|
|
|
36
|
|
|
* @package ImageOptimize |
|
|
|
|
37
|
|
|
* @since 1.2.0 |
|
|
|
|
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
class OptimizedImages extends Field |
40
|
|
|
{ |
41
|
|
|
// Constants |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
const DEFAULT_ASPECT_RATIOS = [ |
45
|
|
|
['x' => 16, 'y' => 9], |
46
|
|
|
]; |
47
|
|
|
const DEFAULT_IMAGE_VARIANTS = [ |
48
|
|
|
[ |
49
|
|
|
'width' => 1200, |
50
|
|
|
'useAspectRatio' => true, |
51
|
|
|
'aspectRatioX' => 16.0, |
52
|
|
|
'aspectRatioY' => 9.0, |
53
|
|
|
'retinaSizes' => ['1'], |
54
|
|
|
'quality' => 82, |
55
|
|
|
'format' => 'jpg', |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
// Public Properties |
60
|
|
|
// ========================================================================= |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
public $ignoreFilesOfType = []; |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
public $displayOptimizedImageVariants = true; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
public $displayDominantColorPalette = true; |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
public $displayLazyLoadPlaceholderImages = true; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
public $variants = []; |
86
|
|
|
|
87
|
|
|
// Private Properties |
88
|
|
|
// ========================================================================= |
89
|
|
|
|
90
|
|
|
/** |
|
|
|
|
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
private $aspectRatios = []; |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Static Methods |
96
|
|
|
// ========================================================================= |
97
|
|
|
|
98
|
|
|
/** |
|
|
|
|
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function __construct(array $config = []) |
102
|
|
|
{ |
103
|
|
|
// Unset any deprecated properties |
104
|
|
|
if (!empty($config)) { |
105
|
|
|
unset($config['transformMethod'], $config['imgixDomain']); |
106
|
|
|
} |
107
|
|
|
parent::__construct($config); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Public Methods |
111
|
|
|
// ========================================================================= |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public static function displayName(): string |
117
|
|
|
{ |
118
|
|
|
return 'OptimizedImages'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
|
|
|
|
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
|
|
|
|
124
|
|
|
public function init() |
125
|
|
|
{ |
126
|
|
|
parent::init(); |
127
|
|
|
|
128
|
|
|
// Handle cases where the plugin has been uninstalled |
129
|
|
|
if (ImageOptimize::$plugin !== null) { |
130
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
131
|
|
|
if ($settings) { |
|
|
|
|
132
|
|
|
if (empty($this->variants)) { |
133
|
|
|
$this->variants = $settings->defaultVariants; |
134
|
|
|
} |
135
|
|
|
$this->aspectRatios = $settings->defaultAspectRatios; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
// If the user has deleted all default aspect ratios, provide a fallback |
139
|
|
|
if (empty($this->aspectRatios)) { |
140
|
|
|
$this->aspectRatios = self::DEFAULT_ASPECT_RATIOS; |
141
|
|
|
} |
142
|
|
|
// If the user has deleted all default variants, provide a fallback |
143
|
|
|
if (empty($this->variants)) { |
144
|
|
|
$this->variants = self::DEFAULT_IMAGE_VARIANTS; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
|
|
|
|
149
|
|
|
* @inheritdoc |
150
|
|
|
*/ |
|
|
|
|
151
|
|
|
public function rules() |
152
|
|
|
{ |
153
|
|
|
$rules = parent::rules(); |
154
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
155
|
|
|
[ |
156
|
|
|
[ |
157
|
|
|
'displayOptimizedImageVariants', |
158
|
|
|
'displayDominantColorPalette', |
159
|
|
|
'displayLazyLoadPlaceholderImages', |
160
|
|
|
], |
161
|
|
|
'boolean', |
162
|
|
|
], |
163
|
|
|
[ |
164
|
|
|
[ |
165
|
|
|
'ignoreFilesOfType', |
166
|
|
|
'variants', |
167
|
|
|
], |
168
|
|
|
ArrayValidator::class |
169
|
|
|
], |
170
|
|
|
]); |
|
|
|
|
171
|
|
|
|
172
|
|
|
return $rules; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
|
|
|
|
176
|
|
|
* @inheritdoc |
177
|
|
|
* @since 1.6.2 |
|
|
|
|
178
|
|
|
*/ |
|
|
|
|
179
|
|
|
public function getContentGqlType() |
180
|
|
|
{ |
181
|
|
|
$typeArray = OptimizedImagesGenerator::generateTypes($this); |
182
|
|
|
|
183
|
|
|
return [ |
184
|
|
|
'name' => $this->handle, |
185
|
|
|
'description' => 'Optimized Images field', |
186
|
|
|
'type' => array_shift($typeArray), |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
|
|
|
|
191
|
|
|
* @inheritdoc |
192
|
|
|
*/ |
|
|
|
|
193
|
|
|
public function afterElementSave(ElementInterface $asset, bool $isNew) |
194
|
|
|
{ |
195
|
|
|
parent::afterElementSave($asset, $isNew); |
196
|
|
|
// Update our OptimizedImages Field data now that the Asset has been saved |
197
|
|
|
if ($asset !== null && $asset instanceof Asset && $asset->id !== null) { |
198
|
|
|
// If the scenario is Asset::SCENARIO_FILEOPS or Asset::SCENARIO_ESSENTIALS treat it as a new asset |
199
|
|
|
$scenario = $asset->getScenario(); |
200
|
|
|
if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || $asset->propagating) { |
201
|
|
|
/** |
202
|
|
|
* If this is a newly uploaded/created Asset, we can save the variants |
203
|
|
|
* via a queue job to prevent it from blocking |
204
|
|
|
*/ |
205
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
206
|
|
|
} else { |
207
|
|
|
/** |
208
|
|
|
* If it's not a newly uploaded/created Asset, they may have edited |
209
|
|
|
* the image with the ImageEditor, so we need to update the variants |
210
|
|
|
* immediately, so the AssetSelectorHud displays the new images |
211
|
|
|
*/ |
212
|
|
|
try { |
213
|
|
|
ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset); |
214
|
|
|
} catch (Exception $e) { |
215
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
|
|
|
|
222
|
|
|
* @inheritdoc |
223
|
|
|
*/ |
|
|
|
|
224
|
|
|
public function normalizeValue($value, ElementInterface $asset = null) |
225
|
|
|
{ |
226
|
|
|
// If we're passed in a string, assume it's JSON-encoded, and decode it |
227
|
|
|
if (\is_string($value) && !empty($value)) { |
228
|
|
|
$value = Json::decodeIfJson($value); |
229
|
|
|
} |
230
|
|
|
// If we're passed in an array, make a model from it |
231
|
|
|
if (\is_array($value)) { |
232
|
|
|
// Create a new OptimizedImage model and populate it |
233
|
|
|
$model = new OptimizedImage($value); |
234
|
|
|
} elseif ($value instanceof OptimizedImage) { |
235
|
|
|
$model = $value; |
236
|
|
|
} else { |
237
|
|
|
// Just create a new empty model |
238
|
|
|
$model = new OptimizedImage(null); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $model; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
|
|
|
|
245
|
|
|
* @inheritdoc |
246
|
|
|
*/ |
|
|
|
|
247
|
|
|
public function getContentColumnType(): string |
248
|
|
|
{ |
249
|
|
|
return Schema::TYPE_TEXT; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// Protected Methods |
253
|
|
|
// ========================================================================= |
254
|
|
|
|
255
|
|
|
/** |
|
|
|
|
256
|
|
|
* @inheritdoc |
257
|
|
|
*/ |
|
|
|
|
258
|
|
|
public function getSettingsHtml() |
259
|
|
|
{ |
260
|
|
|
$namespace = Craft::$app->getView()->getNamespace(); |
261
|
|
|
if (strpos($namespace, Matrix::class) !== false || strpos($namespace, SuperTableField::class) !== false) { |
262
|
|
|
// Render an error template, since the field only works when attached to an Asset |
263
|
|
|
try { |
264
|
|
|
return Craft::$app->getView()->renderTemplate( |
265
|
|
|
'image-optimize/_components/fields/OptimizedImages_error', |
266
|
|
|
[ |
267
|
|
|
] |
268
|
|
|
); |
269
|
|
|
} catch (\Twig\Error\LoaderError $e) { |
270
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
271
|
|
|
} catch (\yii\base\Exception $e) { |
272
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
try { |
277
|
|
|
$reflect = new \ReflectionClass($this); |
278
|
|
|
$thisId = $reflect->getShortName(); |
279
|
|
|
} catch (\ReflectionException $e) { |
280
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
281
|
|
|
$thisId = 0; |
282
|
|
|
} |
283
|
|
|
$id = Craft::$app->getView()->formatInputId($thisId); |
284
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
285
|
|
|
$namespacePrefix = Craft::$app->getView()->namespaceInputName($thisId); |
286
|
|
|
Craft::$app->getView()->registerJs('new Craft.OptimizedImagesInput('. |
|
|
|
|
287
|
|
|
'"'.$namespacedId.'", '. |
288
|
|
|
'"'.$namespacePrefix.'"'. |
289
|
|
|
');'); |
|
|
|
|
290
|
|
|
|
291
|
|
|
// Prep our aspect ratios |
292
|
|
|
$aspectRatios = []; |
293
|
|
|
$index = 1; |
294
|
|
|
foreach ($this->aspectRatios as $aspectRatio) { |
295
|
|
|
if ($index % 6 === 0) { |
296
|
|
|
$aspectRatio['break'] = true; |
297
|
|
|
} |
298
|
|
|
$aspectRatios[] = $aspectRatio; |
299
|
|
|
$index++; |
300
|
|
|
} |
301
|
|
|
$aspectRatio = ['x' => 2, 'y' => 2, 'custom' => true]; |
302
|
|
|
$aspectRatios[] = $aspectRatio; |
303
|
|
|
|
304
|
|
|
// Render the settings template |
305
|
|
|
try { |
306
|
|
|
return Craft::$app->getView()->renderTemplate( |
307
|
|
|
'image-optimize/_components/fields/OptimizedImages_settings', |
308
|
|
|
[ |
309
|
|
|
'field' => $this, |
310
|
|
|
'aspectRatios' => $aspectRatios, |
311
|
|
|
'id' => $id, |
312
|
|
|
'name' => $this->handle, |
313
|
|
|
'namespace' => $namespacedId, |
314
|
|
|
] |
315
|
|
|
); |
316
|
|
|
} catch (\Twig\Error\LoaderError $e) { |
317
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
318
|
|
|
} catch (\yii\base\Exception $e) { |
319
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return ''; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
|
|
|
|
326
|
|
|
* @inheritdoc |
327
|
|
|
*/ |
|
|
|
|
328
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
329
|
|
|
{ |
330
|
|
|
if ($element !== null && $element instanceof Asset && $this->handle !== null) { |
331
|
|
|
/** @var Asset $element */ |
|
|
|
|
332
|
|
|
// Register our asset bundle |
333
|
|
|
try { |
334
|
|
|
Craft::$app->getView()->registerAssetBundle(OptimizedImagesFieldAsset::class); |
335
|
|
|
} catch (InvalidConfigException $e) { |
336
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// Get our id and namespace |
340
|
|
|
$id = Craft::$app->getView()->formatInputId($this->handle); |
341
|
|
|
$nameSpaceId = Craft::$app->getView()->namespaceInputId($id); |
342
|
|
|
|
343
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
344
|
|
|
$jsonVars = [ |
345
|
|
|
'id' => $id, |
346
|
|
|
'name' => $this->handle, |
347
|
|
|
'namespace' => $nameSpaceId, |
348
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
349
|
|
|
]; |
350
|
|
|
$jsonVars = Json::encode($jsonVars); |
351
|
|
|
$view = Craft::$app->getView(); |
352
|
|
|
$view->registerJs("$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(".$jsonVars.");"); |
353
|
|
|
|
354
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
355
|
|
|
|
356
|
|
|
// Render the input template |
357
|
|
|
try { |
358
|
|
|
return Craft::$app->getView()->renderTemplate( |
359
|
|
|
'image-optimize/_components/fields/OptimizedImages_input', |
360
|
|
|
[ |
361
|
|
|
'name' => $this->handle, |
362
|
|
|
'value' => $value, |
363
|
|
|
'variants' => $this->variants, |
364
|
|
|
'field' => $this, |
365
|
|
|
'settings' => $settings, |
366
|
|
|
'elementId' => $element->id, |
367
|
|
|
'format' => $element->getExtension(), |
368
|
|
|
'id' => $id, |
369
|
|
|
'nameSpaceId' => $nameSpaceId, |
370
|
|
|
] |
371
|
|
|
); |
372
|
|
|
} catch (\Twig\Error\LoaderError $e) { |
373
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
374
|
|
|
} catch (\yii\base\Exception $e) { |
375
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
// Render an error template, since the field only works when attached to an Asset |
380
|
|
|
try { |
381
|
|
|
return Craft::$app->getView()->renderTemplate( |
382
|
|
|
'image-optimize/_components/fields/OptimizedImages_error', |
383
|
|
|
[ |
384
|
|
|
] |
385
|
|
|
); |
386
|
|
|
} catch (\Twig\Error\LoaderError $e) { |
387
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
388
|
|
|
} catch (\yii\base\Exception $e) { |
389
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return ''; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|