Last active
February 19, 2021 23:27
-
-
Save KonnorRogers/c930530e3ba011113de8befc7a6fefc7 to your computer and use it in GitHub Desktop.
Inline SVGs and Webp picture tags
This file contains 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
# frozen_string_literal: true | |
# Useful methods for dealing with images | |
module Imageable | |
def inline_svg(filename, **options) | |
filepath = File.join(Rails.root, 'public', asset_pack_path("media/images/#{filename}.svg")) | |
Rails.cache.fetch(filepath) do | |
file = File.read(filepath) | |
doc = Nokogiri::HTML::DocumentFragment.parse file | |
svg = doc.at_css 'svg' | |
options.each { |attr, value| svg[attr.to_s] = value } | |
doc.to_html.html_safe | |
end | |
end | |
# @param filepath [String] A filepath with an extension. Will automatically search for webp | |
def picture_tag(filepath, **options) | |
path = proc { |path_to_asset| asset_pack_path(File.join("media/images/#{path_to_asset}")) } | |
webp_file = path.call(to_webp(filepath)) | |
tag.picture do | |
concat( | |
tag.source(srcset: webp_file, type: 'image/webp') + | |
image_pack_tag(filepath, **options) | |
) | |
end | |
end | |
def to_webp(filepath) | |
basename = File.basename(filepath, '.*') | |
dirname = File.dirname(filepath) | |
if dirname == '.' | |
dirname = '' | |
else | |
dirname += '/' | |
end | |
File.join("#{dirname}#{basename}.webp") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment