basster / twig-base64-extension
Convert images to base64 in twig
Installs: 29 081
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 5
Open Issues: 4
Requires
- php: >=8.0
- symfony/serializer: ^4.4|^5.0
- twig/twig: ^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.6
This package is auto-updated.
Last update: 2024-11-06 22:42:00 UTC
README
Install
composer req basster/twig-base64-extension
Use in Symfony
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);
Usage
<img src="{{ 'img/logo.svg' | image64 }}" alt="My awesome logo" />
Supported Mime-Types?
It utilizes Symfonys MimeTypeGuesser
, nuff said.