PHP code example of data-uri / twig-extension

1. Go to this page and download the library: Download data-uri/twig-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

data-uri / twig-extension example snippets


$twig->addExtension(new \DataURI\TwigExtension());

$twig->render('<img title="hello" src="{{ image | dataUri }}" />', array('image' => '/path/to/image.jpg'));

$file = fopen('/path/to/image.jpg', 'r');
$twig->render('<img title="hello" src="{{ image | dataUri }}" />', array('image' => $file));

$file = file_get_contents('/path/to/image.jpg');
$twig->render('<img title="hello" src="{{ image | dataUri(true, \'image/jpeg\') }}" />', array('image' => $file));

$twig->render('<img title="hello" src="{{ image | dataUri(false) }}" />', array('image' => '/path/to/BIGPICTURE.jpg'));

$file = fopen('bunny.png', 'r');
$twig->render("{{ file | dataUri(false, 'image/png') }}", array('file' => $file));

$json = '{"Hello":"World !"}';
$twig->render( '{{ json | dataUri(false, "application/json", {"charset":"utf-8"}) }}', array('json' => $json));