Passed
Push — develop ( a86c87...b33751 )
by Andrew
03:19
created

ImageOptimizeVariable::createOptimizedImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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