|
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\Image; |
|
13
|
|
|
|
|
14
|
|
|
use SplFileObject; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; |
|
16
|
|
|
use Twig_SimpleFunction; |
|
17
|
|
|
use WBW\Library\Core\Utility\ArrayUtility; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Base64 image Twig extension. |
|
21
|
|
|
* |
|
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
23
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Image |
|
24
|
|
|
* @final |
|
25
|
|
|
*/ |
|
26
|
|
|
final class Base64ImageTwigExtension extends AbstractImageTwigExtension { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Service name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.image.base64"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() { |
|
39
|
|
|
parent::__construct(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Displays a Bootstrap base 64 image. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $args The arguments. |
|
46
|
|
|
* @return string Returns the Bootstrap base 64 image. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function bootstrapBase64ImageFunction(array $args = []) { |
|
49
|
|
|
|
|
50
|
|
|
// Initialize the URI. |
|
51
|
|
|
$uri = ArrayUtility::get($args, "src"); |
|
52
|
|
|
|
|
53
|
|
|
// Initialize the src. |
|
54
|
|
|
$src = []; |
|
55
|
|
|
if (null !== $uri) { |
|
56
|
|
|
$src = (new DataUriNormalizer())->normalize(new SplFileObject($uri)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Return the Bootstrap image. |
|
60
|
|
|
return $this->bootstrapImage($src, ArrayUtility::get($args, "alt"), ArrayUtility::get($args, "width"), ArrayUtility::get($args, "height"), ArrayUtility::get($args, "class")); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the Twig functions. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array Returns the Twig functions. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getFunctions() { |
|
69
|
|
|
return [ |
|
70
|
|
|
new Twig_SimpleFunction("bootstrapBase64Image", [$this, "bootstrapBase64ImageFunction"], ["is_safe" => ["html"]]), |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|