1 | <?php |
||||
2 | /** |
||||
3 | * Image Optimize 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
![]() |
|||||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
9 | */ |
||||
0 ignored issues
–
show
|
|||||
10 | |||||
11 | namespace nystudio107\imageoptimize\variables; |
||||
12 | |||||
13 | use craft\elements\Asset; |
||||
14 | use craft\helpers\Template; |
||||
15 | use nystudio107\imageoptimize\ImageOptimize; |
||||
16 | use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface; |
||||
17 | use nystudio107\imageoptimize\models\OptimizedImage; |
||||
18 | use nystudio107\pluginvite\variables\ViteVariableInterface; |
||||
19 | use nystudio107\pluginvite\variables\ViteVariableTrait; |
||||
20 | use Twig\Markup; |
||||
21 | use yii\base\InvalidConfigException; |
||||
22 | |||||
23 | /** |
||||
0 ignored issues
–
show
|
|||||
24 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||
25 | * @package ImageOptimize |
||||
0 ignored issues
–
show
|
|||||
26 | * @since 1.4.0 |
||||
0 ignored issues
–
show
|
|||||
27 | */ |
||||
0 ignored issues
–
show
|
|||||
28 | class ImageOptimizeVariable implements ViteVariableInterface |
||||
29 | { |
||||
30 | use ViteVariableTrait; |
||||
31 | |||||
32 | // Public Methods |
||||
33 | // ========================================================================= |
||||
34 | |||||
35 | /** |
||||
36 | * Render the LazySizes fallback JS |
||||
37 | * |
||||
38 | * @param array $scriptAttrs |
||||
0 ignored issues
–
show
|
|||||
39 | * @param array $variables |
||||
0 ignored issues
–
show
|
|||||
40 | * @return Markup |
||||
0 ignored issues
–
show
|
|||||
41 | */ |
||||
42 | public function renderLazySizesFallbackJs(array $scriptAttrs = [], array $variables = []): Markup |
||||
43 | { |
||||
44 | return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesFallbackJs($scriptAttrs, $variables)); |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Render the LazySizes JS |
||||
49 | * |
||||
50 | * @param array $scriptAttrs |
||||
0 ignored issues
–
show
|
|||||
51 | * @param array $variables |
||||
0 ignored issues
–
show
|
|||||
52 | * @return Markup |
||||
0 ignored issues
–
show
|
|||||
53 | */ |
||||
54 | public function renderLazySizesJs(array $scriptAttrs = [], array $variables = []): Markup |
||||
55 | { |
||||
56 | return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesJs($scriptAttrs, $variables)); |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * Return an SVG box as a placeholder image |
||||
61 | * |
||||
62 | * @param $width |
||||
0 ignored issues
–
show
|
|||||
63 | * @param $height |
||||
0 ignored issues
–
show
|
|||||
64 | * @param ?string $color |
||||
0 ignored issues
–
show
|
|||||
65 | * |
||||
66 | * @return Markup |
||||
67 | */ |
||||
68 | public function placeholderBox($width, $height, ?string $color = null): Markup |
||||
69 | { |
||||
70 | return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
0 ignored issues
–
show
|
|||||
74 | * @param Asset $asset |
||||
0 ignored issues
–
show
|
|||||
75 | * @param ?array $variants |
||||
0 ignored issues
–
show
|
|||||
76 | * @param bool $generatePlaceholders |
||||
0 ignored issues
–
show
|
|||||
77 | * |
||||
78 | * @return ?OptimizedImage |
||||
79 | * @throws InvalidConfigException |
||||
80 | */ |
||||
81 | public function createOptimizedImages( |
||||
82 | Asset $asset, |
||||
83 | ?array $variants = null, |
||||
84 | bool $generatePlaceholders = false, |
||||
85 | ): ?OptimizedImage { |
||||
86 | // Override our settings for lengthy operations, since we're doing this via Twig |
||||
87 | ImageOptimize::$generatePlaceholders = $generatePlaceholders; |
||||
88 | |||||
89 | return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants); |
||||
0 ignored issues
–
show
It seems like
$variants can also be of type null ; however, parameter $variants of nystudio107\imageoptimiz...createOptimizedImages() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * Returns whether `.webp` is a format supported by the server |
||||
94 | * |
||||
95 | * @return bool |
||||
96 | */ |
||||
97 | public function serverSupportsWebP(): bool |
||||
98 | { |
||||
99 | return ImageOptimize::$plugin->optimize->serverSupportsWebP(); |
||||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * Creates an Image Transform with a given config. |
||||
104 | * |
||||
105 | * @param mixed $config The Image Transform’s class name, or its config, |
||||
106 | * with a `type` value and optionally a `settings` value |
||||
107 | * |
||||
108 | * @return ?ImageTransformInterface The Image Transform |
||||
109 | */ |
||||
110 | public function createImageTransformType($config): ?ImageTransformInterface |
||||
111 | { |
||||
112 | return ImageOptimize::$plugin->optimize->createImageTransformType($config); |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Return whether we are running Craft 3.1 or later |
||||
117 | * |
||||
118 | * @return bool |
||||
119 | */ |
||||
120 | public function craft31(): bool |
||||
121 | { |
||||
122 | return true; |
||||
123 | } |
||||
124 | } |
||||
125 |