generate_data_uri 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +33 -0
- data/Rakefile +35 -0
- data/lib/generate_data_uri.rb +21 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Nick Ragaz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GenerateDataUri
|
2
|
+
===============
|
3
|
+
|
4
|
+
Generate data-uris from files -- for example, files attached to a model.
|
5
|
+
|
6
|
+
These data-uris can be used as the "src" attribute on img tags, to avoid [triggering extra server requests](http://developer.yahoo.com/performance/rules.html#num_http). They can also be used in CSS as the content of the `url()` attribute for background images.
|
7
|
+
|
8
|
+
Most browsers (IE8+ and everything else) support data-uris that are smaller than 32k. Modern browsers support data-uris of any size. Obviously this can increase the size of your HTML page substantially (more than the size of the original image, in fact). However the tradeoff of increased size vs. more HTTP requests is often worth it for responsiveness, particularly if the images cannot be cached. Gzipping the response using your webserver will reduce the overhead to a few percent.
|
9
|
+
|
10
|
+
I've benchmarked the URI generation itself and it only adds a dozen or so milliseconds per file, even for large images.
|
11
|
+
|
12
|
+
See this Wikipedia page for details:
|
13
|
+
|
14
|
+
http://en.wikipedia.org/wiki/Data_URI_scheme
|
15
|
+
|
16
|
+
Requires Ruby 1.9.2.
|
17
|
+
|
18
|
+
Usage (Rails)
|
19
|
+
-------------
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
include GenerateDataUri
|
23
|
+
|
24
|
+
has_attached_file :avatar
|
25
|
+
|
26
|
+
def to_uri(style=:original)
|
27
|
+
generate_data_uri avatar.path(style)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# in view:
|
32
|
+
|
33
|
+
tag :img, src: User.find(1).to_uri, alt: 'My Avatar'
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'rdoc/task'
|
10
|
+
rescue LoadError
|
11
|
+
require 'rdoc/rdoc'
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
RDoc::Task = Rake::RDocTask
|
14
|
+
end
|
15
|
+
|
16
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'GenerateDataUri'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
|
27
|
+
Rake::TestTask.new(:test) do |t|
|
28
|
+
t.libs << 'lib'
|
29
|
+
t.libs << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
task :default => :test
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'mime/types'
|
3
|
+
|
4
|
+
module GenerateDataUri
|
5
|
+
private
|
6
|
+
|
7
|
+
# Use this in callbacks when the file is updated.
|
8
|
+
def clear_data_uri_cache
|
9
|
+
@_uri_cache = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_data_uri(path)
|
13
|
+
return @_uri_cache[path] if @_uri_cache && @_uri_cache[path]
|
14
|
+
|
15
|
+
@_uri_cache ||= {}
|
16
|
+
data = Base64.encode64(File.open(path, 'rb') {|f| f.read }).gsub(/\n/, '')
|
17
|
+
content_type = MIME::Types.type_for(path)
|
18
|
+
|
19
|
+
@_uri_cache[path] = "data:#{content_type};charset=utf-8;base64,#{data}"
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: generate_data_uri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Ragaz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-29 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mime-types
|
17
|
+
requirement: &2165493980 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2165493980
|
26
|
+
description: Turn a file into a string to embed as a data-uri in your HTML.
|
27
|
+
email: nick.ragaz@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- lib/generate_data_uri.rb
|
33
|
+
- MIT-LICENSE
|
34
|
+
- Rakefile
|
35
|
+
- README.md
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/nragaz/generate_data_uri
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.6.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Turn a file into a string to embed as a data-uri in your HTML.
|
61
|
+
test_files: []
|