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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package ImageOptimize |
|
|
|
|
26
|
|
|
* @since 1.4.0 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
39
|
|
|
* @param array $variables |
|
|
|
|
40
|
|
|
* @return Markup |
|
|
|
|
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 |
|
|
|
|
51
|
|
|
* @param array $variables |
|
|
|
|
52
|
|
|
* @return Markup |
|
|
|
|
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 |
|
|
|
|
63
|
|
|
* @param $height |
|
|
|
|
64
|
|
|
* @param ?string $color |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
74
|
|
|
* @param Asset $asset |
|
|
|
|
75
|
|
|
* @param ?array $variants |
|
|
|
|
76
|
|
|
* @param bool $generatePlaceholders |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|