Created
January 21, 2021 21:20
-
-
Save jaredcwhite/40db3cf01e66b4953c6567be449b338a to your computer and use it in GitHub Desktop.
Embed PNG images as data URIs right in your Bridgetown website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use in Liquid or ERB templates like this: | |
# | |
# Liquid: <img src="{% datauri myimage.png %}"> | |
# ERB: <img src="<%= datauri "myimage.png" %>"> | |
class DatauriBuilder < SiteBuilder | |
def build | |
liquid_tag "datauri", :datauri | |
helper "datauri", :datauri | |
end | |
def datauri(image_path, tag=nil) | |
full_path = site.in_source_dir("images", image_path.strip) | |
data = File.read(full_path) | |
encoded_data = Base64.strict_encode64(data) | |
"data:image/png;base64,#{encoded_data}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just drop this in
plugins/builders
and you're off to the races! (If your images aren't insrc/images
, you can just adjust thesite.in_source_dir
line to pull from whatever folder you need.)