The project is in a healthy, maintained state
A Ruby gem that converts files or file-like objects to data URI strings suitable for use in HTML elements like img tags.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 0.1, >= 0.1.0
 Project Readme

file_to_data_uri

A Ruby gem that converts files or file-like objects to data URI strings (e.g. image files to strings suitable for use as the src of an <img>).

Installation

Gemfile way (preferred)

Add this line to your application's Gemfile:

gem "file_to_data_uri", "< 1.0.0"

And then, at a command prompt:

bundle install

Direct way

Or, install it directly with:

gem install file_to_data_uri

Usage

# With a file path
@data_uri = DataURI.convert("path/to/image.jpg") # => "data:image/jpeg;base64,..."

# With a file-like object, e.g. an uploaded file
@data_uri = DataURI.convert(params[:image]) # => "data:image/png;base64,..."

# Then you can use it in an ERB template
# <img src="<%= @data_uri %>">

Supported Input Types

  • File paths: Pass a string with a path to a local file.
  • File-like objects: Pass an object that responds to read (like File.open("image.jpg") or a Rails uploaded file).

License

This gem is available as open source under the terms of the MIT License.