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