1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize 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\variables; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
14
|
|
|
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface; |
15
|
|
|
use nystudio107\imageoptimize\models\OptimizedImage; |
16
|
|
|
|
17
|
|
|
use craft\elements\Asset; |
18
|
|
|
use craft\helpers\Template; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package ImageOptimize |
|
|
|
|
23
|
|
|
* @since 1.4.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class ImageOptimizeVariable extends ManifestVariable |
26
|
|
|
{ |
27
|
|
|
// Public Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Render the LazySizes fallback JS |
32
|
|
|
* |
33
|
|
|
* @param array $scriptAttrs |
|
|
|
|
34
|
|
|
* @param array $variables |
|
|
|
|
35
|
|
|
* @return string |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function renderLazySizesFallbackJs($scriptAttrs = [], $variables = []) |
38
|
|
|
{ |
39
|
|
|
return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesFallbackJs($scriptAttrs, $variables)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Render the LazySizes JS |
44
|
|
|
* |
45
|
|
|
* @param array $scriptAttrs |
|
|
|
|
46
|
|
|
* @param array $variables |
|
|
|
|
47
|
|
|
* @return string |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function renderLazySizesJs($scriptAttrs = [], $variables = []) |
50
|
|
|
{ |
51
|
|
|
return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesJs($scriptAttrs, $variables)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return an SVG box as a placeholder image |
56
|
|
|
* |
57
|
|
|
* @param $width |
|
|
|
|
58
|
|
|
* @param $height |
|
|
|
|
59
|
|
|
* @param string|null $color |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return \Twig_Markup|null |
62
|
|
|
*/ |
63
|
|
|
public function placeholderBox($width, $height, $color = null) |
64
|
|
|
{ |
65
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @param Asset $asset |
|
|
|
|
70
|
|
|
* @param array $variants |
|
|
|
|
71
|
|
|
* @param bool $generatePlaceholders |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return OptimizedImage|null |
74
|
|
|
*/ |
75
|
|
|
public function createOptimizedImages( |
76
|
|
|
Asset $asset, |
77
|
|
|
$variants = null, |
78
|
|
|
$generatePlaceholders = false |
79
|
|
|
) { |
80
|
|
|
// Override our settings for lengthy operations, since we're doing this via Twig |
81
|
|
|
ImageOptimize::$generatePlaceholders = $generatePlaceholders; |
82
|
|
|
|
83
|
|
|
return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns whether `.webp` is a format supported by the server |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function serverSupportsWebP(): bool |
92
|
|
|
{ |
93
|
|
|
return ImageOptimize::$plugin->optimize->serverSupportsWebP(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates an Image Transform with a given config. |
98
|
|
|
* |
99
|
|
|
* @param mixed $config The Image Transform’s class name, or its config, |
100
|
|
|
* with a `type` value and optionally a `settings` value |
101
|
|
|
* |
102
|
|
|
* @return null|ImageTransformInterface The Image Transform |
103
|
|
|
*/ |
104
|
|
|
public function createImageTransformType($config): ImageTransformInterface |
105
|
|
|
{ |
106
|
|
|
return ImageOptimize::$plugin->optimize->createImageTransformType($config); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return whether we are running Craft 3.1 or later |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function craft31(): bool |
115
|
|
|
{ |
116
|
|
|
return ImageOptimize::$craft31; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|