Completed
Push — master ( 4d5688...be014c )
by WEBEWEB
01:51
created

ImageTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
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\Exception\ExceptionInterface;
17
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
18
use Twig_SimpleFunction;
19
use WBW\Library\Core\Argument\ArrayHelper;
20
21
/**
22
 * Image Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\CSS
26
 * @link https://getbootstrap.com/docs/3.3/css/#images
27
 */
28
class ImageTwigExtension extends AbstractImageTwigExtension {
29
30
    /**
31
     * Service name.
32
     *
33
     * @var string
34
     */
35
    const SERVICE_NAME = "webeweb.bootstrap.twig.extension.css.image";
36
37
    /**
38
     * Encode an URI into base 64.
39
     *
40
     * @param string $uri The URI.
41
     * @return string Returns the URI encoded into base 64.
42
     */
43
    private function base64Encode($uri) {
44
45
        if (null === $uri) {
46
            return null;
47
        }
48
49
        $splFileObject = new SplFileObject($uri);
50
51
        if (30100 < Kernel::VERSION_ID) {
52
            return (new DataUriNormalizer())->normalize($splFileObject);
53
        }
54
55
        $data = "";
56
        while (false === $splFileObject->eof()) {
57
            $data .= $splFileObject->fgets();
58
        }
59
60
        return sprintf("data:%s;base64,%s", mime_content_type($uri), base64_encode($data));
61
    }
62
63
    /**
64
     * Displays a Bootstrap image "Base 64".
65
     *
66
     * @param array $args The arguments.
67
     * @return string Returns the Bootstrap base 64 image.
68
     */
69
    public function bootstrapImageBase64Function(array $args = []) {
70
71
        $src = $this->base64Encode(ArrayHelper::get($args, "src"));
72
73
        return $this->bootstrapImage($src, ArrayHelper::get($args, "alt"), ArrayHelper::get($args, "width"), ArrayHelper::get($args, "height"), ArrayHelper::get($args, "class"), ArrayHelper::get($args, "usemap"));
74
    }
75
76
    /**
77
     * Get the Twig functions.
78
     *
79
     * @return array Returns the Twig functions.
80
     */
81
    public function getFunctions() {
82
        return [
83
            new Twig_SimpleFunction("bootstrapImageBase64", [$this, "bootstrapImageBase64Function"], ["is_safe" => ["html"]]),
84
            new Twig_SimpleFunction("bsImageBase64", [$this, "bootstrapImageBase64Function"], ["is_safe" => ["html"]]),
85
        ];
86
    }
87
}
88