webard/twig-base64-extension

Convert images to base64 in twig

dev-master 2023-02-17 12:19 UTC

This package is auto-updated.

Last update: 2024-10-10 17:39:41 UTC


README

SymfonyInsight Build Status Code Coverage Scrutinizer Code Quality

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.