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