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\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
|
|
|
* @final |
26
|
|
|
*/ |
27
|
|
|
final class Base64ImageTwigExtension extends AbstractImageTwigExtension { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Service name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.image.base64"; |
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 base 64 image. |
78
|
|
|
* |
79
|
|
|
* @param array $args The arguments. |
80
|
|
|
* @return string Returns the Bootstrap base 64 image. |
81
|
|
|
*/ |
82
|
|
|
public function bootstrapBase64ImageFunction(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")); |
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("bootstrapBase64Image", [$this, "bootstrapBase64ImageFunction"], ["is_safe" => ["html"]]), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|