1tomany/data-uri

A simple, low dependency library to parse and decode data URIs defined in RFC 2397

Installs: 447

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/1tomany/data-uri

v6.0.2 2026-01-28 19:24 UTC

README

This simple library allows you to easily convert a wide variety of data into a temporary file represented by a lightweight immutable value object.

Installation

composer require 1tomany/data-uri

API Overview

The three methods exposed by this library are:

  • OneToMany\DataUri\DataDecoder::decode()
  • OneToMany\DataUri\DataDecoder::decodeBase64()
  • OneToMany\DataUri\DataDecoder::decodeText()

Each method returns an object that implements the OneToMany\DataUri\Contract\Record\DataUriInterface interface. In this implementation, the value object will automatically delete the file it represents when it is destructed or garbage collected.

The DataDecoder::decode() method is the most versatile as it allows for a wide variety of inputs:

  • A data URI defined in RFC2397
  • A public HTTP or HTTPS URL
  • A readable file

DataDecoder::decode()

The DataDecoder::decode() method has the following parameters:

  • mixed $data The data to decode
  • ?string $name The display name for the temporary file. This is useful for handling file uploads where the original filename is preferred over the random name PHP assigns. A randomly generated name will be used if this is empty and a name cannot be resolved. This is null by default.

Inside DataDecoder::decode()

Under the hood, DataDecoder::decode() uses the fopen() and stream_get_contents() functions, which means the data passed to it can be any stream that PHP supports.

DataDecoder::decodeBase64()

This method is to be used when the data is known to be base64 encoded but NOT encoded as a data URI.

The DataDecoder::decodeBase64() method has the following parameters:

  • string $data The base64 encoded string
  • string $format The format of the data represented as a MIME type
  • ?string $name See DataDecoder::decode()

DataDecoder::decodeText()

This method is to be used when the data is known to be plaintext.

The DataDecoder::decodeText() method has the following arguments:

  • string $text The plaintext string
  • ?string $name See DataDecoder::decode(). The extension .txt will be appended to the $name if the value provided does not already use it.

Examples

See the parse_example.php file for examples on how to use the DataDecoder::decode() method.

Credits

License

The MIT License