|
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\models\OptimizedImage; |
|
15
|
|
|
|
|
16
|
|
|
use Craft; |
|
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 |
|
26
|
|
|
{ |
|
27
|
|
|
// Public Methods |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return an SVG box as a placeholder image |
|
32
|
|
|
* |
|
33
|
|
|
* @param $width |
|
34
|
|
|
* @param $height |
|
35
|
|
|
* @param string|null $color |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Twig_Markup|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public function placeholderBox($width, $height, $color = null) |
|
40
|
|
|
{ |
|
41
|
|
|
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Asset $asset |
|
46
|
|
|
* @param array $variants |
|
47
|
|
|
* @param bool $generatePlacholders |
|
48
|
|
|
* |
|
49
|
|
|
* @return OptimizedImage|null |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createOptimizedImages( |
|
52
|
|
|
Asset $asset, |
|
53
|
|
|
$variants = null, |
|
54
|
|
|
$generatePlacholders = false |
|
55
|
|
|
) { |
|
56
|
|
|
// Override our settings for lengthy operations, since we're doing this via Twig |
|
57
|
|
|
ImageOptimize::$generatePlacholders = $generatePlacholders; |
|
58
|
|
|
|
|
59
|
|
|
return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns whether `.webp` is a format supported by the server |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public function serverSupportsWebP(): bool |
|
68
|
|
|
{ |
|
69
|
|
|
$result = false; |
|
70
|
|
|
$variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators(); |
|
71
|
|
|
foreach ($variantCreators as $variantCreator) { |
|
72
|
|
|
if ($variantCreator['creator'] == 'cwebp' && $variantCreator['installed']) { |
|
73
|
|
|
$result = true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|