Completed
Push — master ( 695906...a2e16e )
by WEBEWEB
19:00
created

Twig/Extension/CSS/ImageTwigExtension.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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"]]),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
84
            new Twig_SimpleFunction("bsImageBase64", [$this, "bootstrapImageBase64Function"], ["is_safe" => ["html"]]),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
85
        ];
86
    }
87
}
88