Completed
Push — master ( 7d0ef3...14c63c )
by WEBEWEB
03:01
created

ImageTwigExtension::bootstrapImageBase64Function()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\CSS;
13
14
use SplFileObject;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
17
use Twig_SimpleFunction;
18
use WBW\Library\Core\Utility\Argument\ArrayUtility;
19
20
/**
21
 * Image Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\CSS
25
 * @see https://getbootstrap.com/docs/3.3/css/#images
26
 */
27
class ImageTwigExtension extends AbstractImageTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.css.image";
35
36
    /**
37
     * Constructor.
38
     */
39
    public function __construct() {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Encode an URI into base 64.
45
     *
46
     * @param string $uri The URI.
47
     * @return string Returns the URI encoded into base 64.
48
     */
49
    private function base64Encode($uri) {
50
51
        // Check the URI.
52
        if (null === $uri) {
53
            return null;
54
        }
55
56
        // Initialize a SPL file object.
57
        $splFileObject = new SplFileObject($uri);
58
59
        // Check the Kernel version.
60
        if (30100 < Kernel::VERSION_ID) {
61
62
            // Use a Data URI normalizer.
63
            return (new DataUriNormalizer())->normalize($splFileObject);
64
        }
65
66
        // Read the SPL file object.
67
        $data = "";
68
        while (false === $splFileObject->eof()) {
69
            $data .= $splFileObject->fgets();
70
        }
71
72
        // Encode into base 64.
73
        return sprintf("data:%s;base64,%s", mime_content_type($uri), base64_encode($data));
74
    }
75
76
    /**
77
     * Displays a Bootstrap image "Base 64".
78
     *
79
     * @param array $args The arguments.
80
     * @return string Returns the Bootstrap base 64 image.
81
     */
82
    public function bootstrapImageBase64Function(array $args = []) {
83
84
        // Initialize the src.
85
        $src = $this->base64Encode(ArrayUtility::get($args, "src"));
86
87
        // Return the Bootstrap image.
88
        return $this->bootstrapImage($src, ArrayUtility::get($args, "alt"), ArrayUtility::get($args, "width"), ArrayUtility::get($args, "height"), ArrayUtility::get($args, "class"), ArrayUtility::get($args, "usemap"));
89
    }
90
91
    /**
92
     * Get the Twig functions.
93
     *
94
     * @return array Returns the Twig functions.
95
     */
96
    public function getFunctions() {
97
        return [
98
            new Twig_SimpleFunction("bootstrapImageBase64", [$this, "bootstrapImageBase64Function"], ["is_safe" => ["html"]]),
99
        ];
100
    }
101
102
}
103