Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sass Extensions] The inline_image() function can now be used to gene…
…rate a data url that embeds the image data in the generated css file. This function works like image_url() in that it expects the image to be a path relative to the images directory. There are clear advantages and disadvantages to this approach. See http://en.wikipedia.org/wiki/Data_URI_scheme for more details. NOTE: Neither IE6 nor IE7 support data urls. Using this approach with large images is discouraged.
- Loading branch information
1 parent
2ecf9f7
commit 5a015b3
Showing
5 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.showgrid { background-image: url(http://assets2.example.com/images/grid.png?busted=true); } | ||
|
||
.inlinegrid { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAUEAYAAACv1qP4AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAZ0lEQVRYw+3QwQ2AIBAFUTEUwI3+uzN7gDscsIgxEuO8An52J11X73OudfxMraXkzHfO3Y98nQEhA0IGhAwIGRAyIGRAyICQASEDQgaEDAgZEDIgZEDIgJABoZzSGK3tPuN9ERFP7Nw4fg+c5g8V1wAAAABJRU5ErkJggg=='); } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
.showgrid | ||
background-image= image_url("grid.png") | ||
|
||
.inlinegrid | ||
background-image= inline_image("grid.png") | ||
|
5a015b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Holy cow that is cool! Which browsers support inline images?
5a015b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All modern browsers except ie6 and 7.
5a015b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍