Created
August 12, 2010 05:19
-
-
Save jkotchoff/520361 to your computer and use it in GitHub Desktop.
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
require 'net/http' | |
# Extend img tag to optionally embed images in-line into HTML instead of referencing as a URI | |
# | |
# This is a useful technique when sending HTML emails as it circumvents the prompt many mail clients | |
# present seeking permission to 'display images from this sender?' when an email of mime-type 'html' | |
# contains <img> tags. | |
# | |
# ie. <%= image_tag('red_dot.gif', :embed_inline => "true") %> | |
# | |
# will produce: | |
# | |
# <img src="data:image/png;base64, | |
# iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP | |
# C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA | |
# AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J | |
# REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq | |
# ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 | |
# vr4MkhoXe0rZigAAAABJRU5ErkJggg==" /> | |
# | |
# instead of: | |
# | |
# <img src="red_dot.gif" /> | |
# | |
module ActionView::Helpers::AssetTagHelper | |
alias_method :orig_image_tag, :image_tag | |
def image_tag(source, options = {}) | |
# If an 'embed_inline' argument has been specified and is 'true', write the image data into | |
# the tag as a data URI | |
# | |
# Example Usage: | |
# <%= image_tag('red_dot.gif', :embed_inline => "true") %> | |
options.symbolize_keys! | |
if options[:embed_inline] and options[:embed_inline].to_s.downcase == 'true' | |
include_host = true | |
full_image_path = compute_public_path(source, 'images', nil, include_host) | |
#TODO: improve! | |
image_contents = if full_image_path.starts_with?("http") | |
Net::HTTP.get(URI.parse(full_image_path)) | |
else | |
File.open(File.join(ASSETS_DIR, full_image_path).split("?").first).read | |
end | |
image_mime_type = "image/" + full_image_path.downcase.split(".").last.gsub('jpg', 'jpeg') | |
encoded_image_contents = ActiveSupport::Base64.encode64(image_contents) | |
# Make the magic happen - refer: http://en.wikipedia.org/wiki/Data_URI_scheme | |
options[:src] = "data:#{image_mime_type};base64,#{encoded_image_contents}" | |
# Begin duplication of rails image_tag - passing :src through to the orig_image_tag would get overridden | |
options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize | |
if size = options.delete(:size) | |
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} | |
end | |
if mouseover = options.delete(:mouseover) | |
options[:onmouseover] = "this.src='#{image_path(mouseover)}'" | |
options[:onmouseout] = "this.src='#{image_path(options[:src])}'" | |
end | |
return tag("img", options) | |
end | |
return orig_image_tag(source, options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment