PHPackages                             data-uri/twig-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. data-uri/twig-extension

ActiveLibrary[Templating &amp; Views](/categories/templating)

data-uri/twig-extension
=======================

Twig extension for data URI ; Converts data to the data URI Url scheme (RFC 2397)

1.0.1(10y ago)35209.8k↑983.3%10[2 issues](https://github.com/romainneutron/TwigExtension-DataUri/issues)[1 PRs](https://github.com/romainneutron/TwigExtension-DataUri/pulls)3MITPHPPHP &gt;=5.3.2

Since Aug 25Pushed 7y ago2 watchersCompare

[ Source](https://github.com/romainneutron/TwigExtension-DataUri)[ Packagist](https://packagist.org/packages/data-uri/twig-extension)[ RSS](/packages/data-uri-twig-extension/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (4)Used By (3)

DataURI Twig Extension
======================

[](#datauri-twig-extension)

[![Build Status](https://camo.githubusercontent.com/6ff05c97d6217c023f2b2a12c1c884409f166b6341c05c253acd300daaf98bc0/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f726f6d61696e6e657574726f6e2f54776967457874656e73696f6e2d446174615572692e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/romainneutron/TwigExtension-DataUri)

This is an extension for [Twig Templating engine](http://twig.sensiolabs.org/)

This extension makes easy to use the dataURI scheme as specified in [RFC 2397](https://www.ietf.org/rfc/rfc2397.txt).

Be carefull, as explained in the RFC, it can not be used as a replacement for traditionnal URI sources all the time. DataURI scheme is length limited and can not handle all files / ressources.

\##Install

This extension requires PHP 5.3.2.

[Download and Install composer](http://getcomposer.org/download/), then add this to your `composer.json`:

```
{
    "require": {
        "data-uri/twig-extension": "dev-master"
    }
}
```

Then, register the extension in your twig environment:

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

Usage
-----

[](#usage)

DataURI extension works with **stream ressource**, **scalar value**, **binary string**, or a **pathname** for a file.

### Pathname

[](#pathname)

```
$twig->render('', array('image' => '/path/to/image.jpg'));
```

will render something like:

```

```

### Ressource

[](#ressource)

```
$file = fopen('/path/to/image.jpg', 'r');
$twig->render('', array('image' => $file));
```

### Binary string

[](#binary-string)

```
$file = file_get_contents('/path/to/image.jpg');
$twig->render('', array('image' => $file));
```

Options
-------

[](#options)

DataUri can take up to 3 parameters:

`dataUri(strictMode, mimeType, parameters)`

**strictMode** default value us `true`**mimeType** default value is `null` (autodetected when passing a pathname) **parameters** is an array of key/value parameters

\###Unlock data length restriction

As said above, the RFC is quite strict on the output max length. By default, the extension is 100% RFC compliant and the extension will log warnings if you render data too large, but you can unlock the limit with the first option of the filter:

```
$twig->render('', array('image' => '/path/to/BIGPICTURE.jpg'));
```

**note**: If you display errors, warning message will result in Twig throws Twig\_Error\_Runtime exception.

### Example Mimetype

[](#example-mimetype)

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

will render something like:

```
data:image/png;base64,oAYTUKHJKPPZ...F873=/SO

```

### Example Parameters

[](#example-parameters)

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

will render:

```
data:application/json;charset=utf-8,%7B%22Hello%22%3A%22World%20%21%22%7D

```

License
-------

[](#license)

This extension is released under the MIT License

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 92.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~313 days

Total

3

Last Release

3694d ago

Major Versions

0.1.0 → 1.0.02016-03-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c264f69af4c60cb9cc98a522b4663f4b6a2708629cabc14409478ab6fc35348?d=identicon)[romain](/maintainers/romain)

---

Top Contributors

[![romainneutron](https://avatars.githubusercontent.com/u/137574?v=4)](https://github.com/romainneutron "romainneutron (35 commits)")[![bes89](https://avatars.githubusercontent.com/u/1302733?v=4)](https://github.com/bes89 "bes89 (1 commits)")[![Chi-teck](https://avatars.githubusercontent.com/u/673139?v=4)](https://github.com/Chi-teck "Chi-teck (1 commits)")[![pskt](https://avatars.githubusercontent.com/u/2283090?v=4)](https://github.com/pskt "pskt (1 commits)")

---

Tags

twig-extensiondataURIdataURL

### Embed Badge

![Health badge](/badges/data-uri-twig-extension/health.svg)

```
[![Health](https://phpackages.com/badges/data-uri-twig-extension/health.svg)](https://phpackages.com/packages/data-uri-twig-extension)
```

###  Alternatives

[dms/twig-extension-bundle

DMS Twig Extension Bundle, leverages Fabien Potencier's extra twig extensions for your website.

19297.1k](/packages/dms-twig-extension-bundle)[bes/mobiledetect-twig-extension

MobileDetect-integration for Twig

18238.3k](/packages/bes-mobiledetect-twig-extension)[bluetel-solutions/twig-truncate-extension

Twig Extension to truncate nested HTML, safely!

11102.4k](/packages/bluetel-solutions-twig-truncate-extension)[wapmorgan/morphos-twig

Adds a {{ plural }}, {{ name }}, {{ numeral }}, {{ ordinal }} and {{ money }} filters to Twig templating engine for Russian pluralization and declenation.

1032.0k](/packages/wapmorgan-morphos-twig)[ex3v/formerrorsbundle

A service and Twig method that displays all form errors on all levels.

118.8k](/packages/ex3v-formerrorsbundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
