Module: FastImageInline

Defined in:
lib/fastimage_inline.rb

Instance Method Summary collapse

Instance Method Details

#inline_image_path(source) ⇒ Object

Returns the image path for use in a src parameter in an image tag Usage is the same as image_path, or path_to_image, except that if the browser making the request is capable of handling it then the image itself is included in a data uri rather than just the path to the image



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fastimage_inline.rb', line 62

def inline_image_path(source)
  public_path = respond_to?(:compute_public_path) ? compute_public_path(source, 'images') : asset_paths.compute_public_path(source, 'images')

  has_request = respond_to?(:request)

  if has_request && inline_capable_browser?
    inline_image_data(public_path)
  else
    public_path
  end
end

#inline_image_tag(source, options = {}) ⇒ Object

Returns a tag for the image Works just like image_tag, except that if the browser making the request being processed can handle data uris then the image itself is sent placed in the src attribute rather than just the path to it.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fastimage_inline.rb', line 40

def inline_image_tag(source, options = {})
  options.symbolize_keys!
  options[:alt] ||= File.basename(source, '.*').split('.').first.to_s.capitalize
  options[:src] = inline_image_path(source)

  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='#{inline_image_path(mouseover)}'"
    options[:onmouseout]  = "this.src='#{inline_image_path(options[:src])}'"
  end

  tag("img", options)
end