composer req basster/twig-base64-extension
If you are using Symfony Flex you're done.
If you are not using Flex, add the following to your services.yaml
:
services:
_defaults:
public: false
autowire: true
autoconfigure: true
Basster\TwigBase64\Converter\ImageToBase64Converter: ~
Basster\TwigBase64\Twig\Base64ImageExtension: ~
Basster\TwigBase64\Converter\FileConverterInterface: '@Basster\TwigBase64\Converter\ImageToBase64Converter'
If you are using Twig standalone, do something like this:
$converter = new \Basster\TwigBase64\Converter\ImageToBase64Converter(new \Symfony\Component\Serializer\Normalizer\DataUriNormalizer());
$extension = new \Basster\TwigBase64\Twig\Base64ImageExtension($converter);
$twig = new \Twig\Environment($loader);
$twig->addExtension($extension);
<img src="{{ 'img/logo.svg' | image64 }}" alt="My awesome logo" />
It utilizes Symfonys MimeTypeGuesser
, nuff said.