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