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