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\HttpKernel\Kernel; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; |
17
|
|
|
use Twig_SimpleFunction; |
18
|
|
|
use WBW\Library\Core\Utility\Argument\ArrayUtility; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Base64 image Twig extension. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Image |
25
|
|
|
*/ |
26
|
|
|
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
|
|
|
* Encode an URI into base 64. |
44
|
|
|
* |
45
|
|
|
* @param string $uri The URI. |
46
|
|
|
* @return string Returns the URI encoded into base 64. |
47
|
|
|
*/ |
48
|
|
|
private function base64Encode($uri) { |
49
|
|
|
|
50
|
|
|
// Check the URI. |
51
|
|
|
if (null === $uri) { |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Initialize a SPL file object. |
56
|
|
|
$splFileObject = new SplFileObject($uri); |
57
|
|
|
|
58
|
|
|
// Check the Kernel version. |
59
|
|
|
if (30100 < Kernel::VERSION_ID) { |
60
|
|
|
|
61
|
|
|
// Use a Data URI normalizer. |
62
|
|
|
return (new DataUriNormalizer())->normalize($splFileObject); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Read the SPL file object. |
66
|
|
|
$data = ""; |
67
|
|
|
while (false === $splFileObject->eof()) { |
68
|
|
|
$data .= $splFileObject->fgets(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Encode into base 64. |
72
|
|
|
return sprintf("data:%s;base64,%s", mime_content_type($uri), base64_encode($data)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Displays a Bootstrap base 64 image. |
77
|
|
|
* |
78
|
|
|
* @param array $args The arguments. |
79
|
|
|
* @return string Returns the Bootstrap base 64 image. |
80
|
|
|
*/ |
81
|
|
|
public function bootstrapBase64ImageFunction(array $args = []) { |
82
|
|
|
|
83
|
|
|
// Initialize the src. |
84
|
|
|
$src = $this->base64Encode(ArrayUtility::get($args, "src")); |
85
|
|
|
|
86
|
|
|
// Return the Bootstrap image. |
87
|
|
|
return $this->bootstrapImage($src, ArrayUtility::get($args, "alt"), ArrayUtility::get($args, "width"), ArrayUtility::get($args, "height"), ArrayUtility::get($args, "class"), ArrayUtility::get($args, "usemap")); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the Twig functions. |
92
|
|
|
* |
93
|
|
|
* @return array Returns the Twig functions. |
94
|
|
|
*/ |
95
|
|
|
public function getFunctions() { |
96
|
|
|
return [ |
97
|
|
|
new Twig_SimpleFunction("bootstrapBase64Image", [$this, "bootstrapBase64ImageFunction"], ["is_safe" => ["html"]]), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|