Sha256: c194cef4df61244132e4a79c9e6d45c3b8998fae3769a0b4245831e1e48dbc79

Contents?: true

Size: 1002 Bytes

Versions: 32

Compression:

Stored size: 1002 Bytes

Contents

require 'base64'
module Compass::SassExtensions::Functions::InlineImage

  def inline_image(path, mime_type = nil)
    path = path.value
    real_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path)
    url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')"
    Sass::Script::String.new(url)
  end

private
  def compute_mime_type(path, mime_type)
    return mime_type if mime_type
    case path
    when /\.png$/i
      'image/png'
    when /\.jpe?g$/i
      'image/jpeg'
    when /\.gif$/i
      'image/gif'
    when /\.([a-zA-Z]+)$/
      "image/#{Regexp.last_match(1).downcase}"
    else
      raise Compass::Error, "A mime type could not be determined for #{path}, please specify one explicitly."
    end
  end

  def data(real_path)
    if File.readable?(real_path)
      Base64.encode64(File.read(real_path)).gsub("\n","")
    else
      raise Compass::Error, "File not found or cannot be read: #{real_path}"
    end
  end
end

Version data entries

32 entries across 32 versions & 5 rubygems

Version Path
alphasights-compass-0.8.12 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.7.0 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.0 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.1 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.10 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.11 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.12 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.13 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.14 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.15 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.16 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.17 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.2 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.3 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.4 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.5 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.6 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.7 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.8 lib/compass/sass_extensions/functions/inline_image.rb
chriseppstein-compass-0.8.9 lib/compass/sass_extensions/functions/inline_image.rb