1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ImageOptimize 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\models; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
14
|
|
|
use nystudio107\imageoptimize\imagetransforms\CraftImageTransform; |
15
|
|
|
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface; |
16
|
|
|
use nystudio107\imageoptimize\imagetransforms\ImgixImageTransform; |
|
|
|
|
17
|
|
|
use nystudio107\imageoptimize\imagetransforms\ThumborImageTransform; |
|
|
|
|
18
|
|
|
|
19
|
|
|
use craft\base\Model; |
20
|
|
|
use craft\behaviors\EnvAttributeParserBehavior; |
|
|
|
|
21
|
|
|
use craft\validators\ArrayValidator; |
22
|
|
|
|
23
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ImageOptimize Settings model |
27
|
|
|
* |
28
|
|
|
* @author nystudio107 |
|
|
|
|
29
|
|
|
* @package ImageOptimize |
|
|
|
|
30
|
|
|
* @since 1.0.0 |
|
|
|
|
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class Settings extends Model |
33
|
|
|
{ |
34
|
|
|
// Constants |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
const DEPRECATED_PROPERTIES = [ |
38
|
|
|
'generatePlacholders', |
39
|
|
|
'transformMethod', |
40
|
|
|
'imgixDomain', |
41
|
|
|
'imgixApiKey', |
42
|
|
|
'imgixSecurityToken', |
43
|
|
|
'thumborBaseUrl', |
44
|
|
|
'thumborSecurityKey', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
// Public Properties |
48
|
|
|
// ========================================================================= |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var string The image transform class to use for image transforms |
52
|
|
|
*/ |
53
|
|
|
public $transformClass = CraftImageTransform::class; |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @var array Settings for the image transform components |
57
|
|
|
*/ |
58
|
|
|
public $imageTransformTypeSettings = []; |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var bool Should the image variants in an Asset Volume be automatically |
62
|
|
|
* re-saved when saving an OptimizedImages field, saving an Asset |
63
|
|
|
* Volume that has an OptimizedImages field in its layout, or saving |
64
|
|
|
* the ImageOptimized settings. Set this to false only if you will be |
65
|
|
|
* manually using the CLI console command to resave image variants |
66
|
|
|
*/ |
67
|
|
|
public $automaticallyResaveImageVariants = true; |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var bool Should image variant be created on Asset save (aka |
71
|
|
|
* BeforePageLoad) |
72
|
|
|
*/ |
73
|
|
|
public $generateTransformsBeforePageLoad = true; |
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* @var bool Set to false to disable all placeholder generation |
77
|
|
|
*/ |
78
|
|
|
public $generatePlaceholders = true; |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @var bool Controls whether a dominant color palette should be created |
82
|
|
|
* for image variants It takes a bit of time, so if you never plan to |
83
|
|
|
* use it, you can turn it off |
84
|
|
|
*/ |
85
|
|
|
public $createColorPalette = true; |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @var bool Controls whether SVG placeholder silhouettes should be created |
89
|
|
|
* for image variants It takes a bit of time, so if you never plan to |
90
|
|
|
* use them, you can turn it off |
91
|
|
|
*/ |
92
|
|
|
public $createPlaceholderSilhouettes = true; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var bool Controls whether retina images are automatically created with |
96
|
|
|
* reduced quality as per |
97
|
|
|
* https://www.netvlies.nl/tips-updates/design-interactie/design-interactie/retina-revolution/ |
98
|
|
|
*/ |
99
|
|
|
public $lowerQualityRetinaImageVariants = true; |
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* @var bool Controls whether Optimized Image Variants are created that |
103
|
|
|
* would be up-scaled to be larger than the original source image |
104
|
|
|
*/ |
105
|
|
|
public $allowUpScaledImageVariants = false; |
106
|
|
|
|
107
|
|
|
/** |
|
|
|
|
108
|
|
|
* @var bool Controls whether images scaled down >= 50% should be |
109
|
|
|
* automatically sharpened |
110
|
|
|
*/ |
111
|
|
|
public $autoSharpenScaledImages = true; |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* @var ImageTransformInterface[] The default Image Transform type classes |
115
|
|
|
*/ |
116
|
|
|
public $defaultImageTransformTypes = [ |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
/** |
|
|
|
|
120
|
|
|
* @var array Default aspect ratios |
121
|
|
|
*/ |
122
|
|
|
public $defaultAspectRatios = [ |
123
|
|
|
['x' => 16, 'y' => 9], |
124
|
|
|
['x' => 8, 'y' => 5], |
125
|
|
|
['x' => 4, 'y' => 3], |
126
|
|
|
['x' => 5, 'y' => 4], |
127
|
|
|
['x' => 1, 'y' => 1], |
128
|
|
|
['x' => 9, 'y' => 16], |
129
|
|
|
['x' => 5, 'y' => 8], |
130
|
|
|
['x' => 3, 'y' => 4], |
131
|
|
|
['x' => 4, 'y' => 5], |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
/** |
|
|
|
|
135
|
|
|
* @var array Default variants |
136
|
|
|
*/ |
137
|
|
|
public $defaultVariants = [ |
138
|
|
|
[ |
139
|
|
|
'width' => 1200, |
140
|
|
|
'useAspectRatio' => true, |
141
|
|
|
'aspectRatioX' => 16.0, |
142
|
|
|
'aspectRatioY' => 9.0, |
143
|
|
|
'retinaSizes' => ['1'], |
144
|
|
|
'quality' => 82, |
145
|
|
|
'format' => 'jpg', |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
'width' => 992, |
149
|
|
|
'useAspectRatio' => true, |
150
|
|
|
'aspectRatioX' => 16.0, |
151
|
|
|
'aspectRatioY' => 9.0, |
152
|
|
|
'retinaSizes' => ['1'], |
153
|
|
|
'quality' => 82, |
154
|
|
|
'format' => 'jpg', |
155
|
|
|
], |
156
|
|
|
[ |
157
|
|
|
'width' => 768, |
158
|
|
|
'useAspectRatio' => true, |
159
|
|
|
'aspectRatioX' => 4.0, |
160
|
|
|
'aspectRatioY' => 3.0, |
161
|
|
|
'retinaSizes' => ['1'], |
162
|
|
|
'quality' => 60, |
163
|
|
|
'format' => 'jpg', |
164
|
|
|
], |
165
|
|
|
[ |
166
|
|
|
'width' => 576, |
167
|
|
|
'useAspectRatio' => true, |
168
|
|
|
'aspectRatioX' => 4.0, |
169
|
|
|
'aspectRatioY' => 3.0, |
170
|
|
|
'retinaSizes' => ['1'], |
171
|
|
|
'quality' => 60, |
172
|
|
|
'format' => 'jpg', |
173
|
|
|
], |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
/** |
|
|
|
|
177
|
|
|
* @var array Active image processors |
178
|
|
|
*/ |
179
|
|
|
public $activeImageProcessors = [ |
180
|
|
|
'jpg' => [ |
181
|
|
|
'jpegoptim', |
182
|
|
|
], |
183
|
|
|
'png' => [ |
184
|
|
|
'optipng', |
185
|
|
|
], |
186
|
|
|
'svg' => [ |
187
|
|
|
'svgo', |
188
|
|
|
], |
189
|
|
|
'gif' => [ |
190
|
|
|
'gifsicle', |
191
|
|
|
], |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
/** |
|
|
|
|
195
|
|
|
* @var array Active image variant creators |
196
|
|
|
*/ |
197
|
|
|
public $activeImageVariantCreators = [ |
198
|
|
|
'jpg' => [ |
199
|
|
|
'cwebp', |
200
|
|
|
], |
201
|
|
|
'png' => [ |
202
|
|
|
'cwebp', |
203
|
|
|
], |
204
|
|
|
'gif' => [ |
205
|
|
|
'cwebp', |
206
|
|
|
], |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
/** |
|
|
|
|
210
|
|
|
* @var array Preset image processors |
211
|
|
|
*/ |
212
|
|
|
public $imageProcessors = [ |
213
|
|
|
// jpeg optimizers |
214
|
|
|
'jpegoptim' => [ |
215
|
|
|
'commandPath' => '/usr/bin/jpegoptim', |
216
|
|
|
'commandOptions' => '-s', |
217
|
|
|
'commandOutputFileFlag' => '', |
218
|
|
|
], |
219
|
|
|
'mozjpeg' => [ |
220
|
|
|
'commandPath' => '/usr/bin/mozjpeg', |
221
|
|
|
'commandOptions' => '-optimize -copy none', |
222
|
|
|
'commandOutputFileFlag' => '-outfile', |
223
|
|
|
], |
224
|
|
|
'jpegtran' => [ |
225
|
|
|
'commandPath' => '/usr/bin/jpegtran', |
226
|
|
|
'commandOptions' => '-optimize -copy none', |
227
|
|
|
'commandOutputFileFlag' => '', |
228
|
|
|
], |
229
|
|
|
// png optimizers |
230
|
|
|
'optipng' => [ |
231
|
|
|
'commandPath' => '/usr/bin/optipng', |
232
|
|
|
'commandOptions' => '-o3 -strip all', |
233
|
|
|
'commandOutputFileFlag' => '', |
234
|
|
|
], |
235
|
|
|
'pngcrush' => [ |
236
|
|
|
'commandPath' => '/usr/bin/pngcrush', |
237
|
|
|
'commandOptions' => '-brute -ow', |
238
|
|
|
'commandOutputFileFlag' => '', |
239
|
|
|
], |
240
|
|
|
'pngquant' => [ |
241
|
|
|
'commandPath' => '/usr/bin/pngquant', |
242
|
|
|
'commandOptions' => '--strip --skip-if-larger', |
243
|
|
|
'commandOutputFileFlag' => '', |
244
|
|
|
], |
245
|
|
|
// svg optimizers |
246
|
|
|
'svgo' => [ |
247
|
|
|
'commandPath' => '/usr/bin/svgo', |
248
|
|
|
'commandOptions' => '', |
249
|
|
|
'commandOutputFileFlag' => '', |
250
|
|
|
], |
251
|
|
|
// gif optimizers |
252
|
|
|
'gifsicle' => [ |
253
|
|
|
'commandPath' => '/usr/bin/gifsicle', |
254
|
|
|
'commandOptions' => '-O3 -k 256', |
255
|
|
|
'commandOutputFileFlag' => '', |
256
|
|
|
], |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
public $imageVariantCreators = [ |
260
|
|
|
// webp variant creator |
261
|
|
|
'cwebp' => [ |
262
|
|
|
'commandPath' => '/usr/bin/cwebp', |
263
|
|
|
'commandOptions' => '', |
264
|
|
|
'commandOutputFileFlag' => '-o', |
265
|
|
|
'commandQualityFlag' => '-q', |
266
|
|
|
'imageVariantExtension' => 'webp', |
267
|
|
|
], |
268
|
|
|
]; |
269
|
|
|
|
270
|
|
|
// Public Methods |
271
|
|
|
// ========================================================================= |
272
|
|
|
|
273
|
|
|
/** |
|
|
|
|
274
|
|
|
* @inheritdoc |
275
|
|
|
*/ |
276
|
|
|
public function __construct(array $config = []) |
277
|
|
|
{ |
278
|
|
|
// Unset any deprecated properties |
279
|
|
|
if (!empty($config)) { |
280
|
|
|
// Handle migrating old Imagix settings |
281
|
|
|
if (isset($config['imgixDomain'])) { |
282
|
|
|
$config['imageTransformTypeSettings'][ImgixImageTransform::class]['domain'] = $config['imgixDomain']; |
283
|
|
|
} |
284
|
|
|
if (isset($config['imgixApiKey'])) { |
285
|
|
|
$config['imageTransformTypeSettings'][ImgixImageTransform::class]['apiKey'] = $config['imgixApiKey']; |
286
|
|
|
} |
287
|
|
|
if (isset($config['imgixSecurityToken'])) { |
288
|
|
|
$config['imageTransformTypeSettings'][ImgixImageTransform::class]['securityToken'] = $config['imgixSecurityToken']; |
289
|
|
|
} |
290
|
|
|
// Handle migrating old Thumbor settings |
291
|
|
|
if (isset($config['thumborBaseUrl'])) { |
292
|
|
|
$config['imageTransformTypeSettings'][ThumborImageTransform::class]['baseUrl'] = $config['thumborBaseUrl']; |
293
|
|
|
} |
294
|
|
|
if (isset($config['thumborSecurityKey'])) { |
295
|
|
|
$config['imageTransformTypeSettings'][ThumborImageTransform::class]['securityKey'] = $config['thumborSecurityKey']; |
296
|
|
|
} |
297
|
|
|
// Remove deprecated properties |
298
|
|
|
foreach (self::DEPRECATED_PROPERTIES as $prop) { |
299
|
|
|
unset($config[$prop]); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
parent::__construct($config); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
|
|
|
|
307
|
|
|
* @inheritdoc |
308
|
|
|
*/ |
|
|
|
|
309
|
|
|
public function rules() |
310
|
|
|
{ |
311
|
|
|
return [ |
312
|
|
|
['transformClass', 'string'], |
313
|
|
|
['transformClass', 'default', 'value' => CraftImageTransform::class], |
314
|
|
|
[ |
315
|
|
|
[ |
316
|
|
|
'automaticallyResaveImageVariants', |
317
|
|
|
'generateTransformsBeforePageLoad', |
318
|
|
|
'createColorPalette', |
319
|
|
|
'createPlaceholderSilhouettes', |
320
|
|
|
'lowerQualityRetinaImageVariants', |
321
|
|
|
'allowUpScaledImageVariants', |
322
|
|
|
'autoSharpenScaledImages', |
323
|
|
|
], |
324
|
|
|
'boolean', |
325
|
|
|
], |
326
|
|
|
[ |
327
|
|
|
[ |
328
|
|
|
'defaultVariants', |
329
|
|
|
'activeImageProcessors', |
330
|
|
|
'activeImageVariantCreators', |
331
|
|
|
'imageProcessors', |
332
|
|
|
'imageVariantCreators', |
333
|
|
|
], |
334
|
|
|
'required', |
335
|
|
|
], |
336
|
|
|
[ |
337
|
|
|
[ |
338
|
|
|
'imageTransformTypeSettings', |
339
|
|
|
'defaultImageTransformTypes', |
340
|
|
|
'defaultVariants', |
341
|
|
|
'activeImageProcessors', |
342
|
|
|
'activeImageVariantCreators', |
343
|
|
|
'imageProcessors', |
344
|
|
|
'imageVariantCreators', |
345
|
|
|
], |
346
|
|
|
ArrayValidator::class, |
347
|
|
|
], |
348
|
|
|
]; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
|
|
|
|
352
|
|
|
* @inheritdoc |
353
|
|
|
*/ |
|
|
|
|
354
|
|
|
public function fields() |
355
|
|
|
{ |
356
|
|
|
// Only return user-editable settings |
357
|
|
|
$fields = [ |
358
|
|
|
'transformClass', |
359
|
|
|
'imageTransformTypeSettings', |
360
|
|
|
'createColorPalette', |
361
|
|
|
'createPlaceholderSilhouettes', |
362
|
|
|
'lowerQualityRetinaImageVariants', |
363
|
|
|
'allowUpScaledImageVariants', |
364
|
|
|
'autoSharpenScaledImages', |
365
|
|
|
]; |
366
|
|
|
|
367
|
|
|
return $fields; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
|
|
|
|
371
|
|
|
* @return array |
372
|
|
|
*/ |
373
|
|
|
public function behaviors() |
374
|
|
|
{ |
375
|
|
|
$craft31Behaviors = []; |
376
|
|
|
if (ImageOptimize::$craft31) { |
377
|
|
|
$craft31Behaviors = [ |
378
|
|
|
'parser' => [ |
379
|
|
|
'class' => EnvAttributeParserBehavior::class, |
380
|
|
|
'attributes' => [ |
381
|
|
|
], |
382
|
|
|
] |
383
|
|
|
]; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return array_merge($craft31Behaviors, [ |
|
|
|
|
387
|
|
|
'typecast' => [ |
388
|
|
|
'class' => AttributeTypecastBehavior::class, |
389
|
|
|
// 'attributeTypes' will be composed automatically according to `rules()` |
390
|
|
|
], |
391
|
|
|
]); |
|
|
|
|
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
} |
395
|
|
|
|