image8 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/README +27 -0
- data/image8.gemspec +21 -0
- data/lib/image8.rb +61 -0
- metadata +118 -0
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
== image8
|
2
|
+
|
3
|
+
image8 resizes images on-the-fly and stores them in couchdb for later
|
4
|
+
retrival and easy replication. image8 should be used behind a varnish
|
5
|
+
or squid. <work in progress>.
|
6
|
+
|
7
|
+
== config.ru
|
8
|
+
|
9
|
+
require "image8"
|
10
|
+
Image8.couchdb = "http://127.0.0.1:5984/image8"
|
11
|
+
run Image8
|
12
|
+
|
13
|
+
== usage
|
14
|
+
|
15
|
+
the url
|
16
|
+
|
17
|
+
http://image8/200x250/http://ny.ti/some/image.jpg
|
18
|
+
|
19
|
+
produces an image no larger than 200x250 pixels in size. scales
|
20
|
+
proportionality.
|
21
|
+
|
22
|
+
== misc
|
23
|
+
|
24
|
+
image8 is released under the mit license. (c) 2010 harry vangberg
|
25
|
+
<harry@vangberg.name>, memolane <info@memolane.com>.
|
26
|
+
|
27
|
+
image8 is inspired by imagery <http://github.com/tobi/imagery>
|
data/image8.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "image8"
|
3
|
+
s.version = "0.1"
|
4
|
+
s.date = "2010-06-01"
|
5
|
+
s.summary = "dynamic image resizing."
|
6
|
+
s.email = "harry@vangberg.name"
|
7
|
+
s.homepage = "http://github.com/ichverstehe/image8"
|
8
|
+
s.description = "dynamic image resizing."
|
9
|
+
s.authors = ["Harry Vangberg"]
|
10
|
+
s.files = [
|
11
|
+
"README",
|
12
|
+
"Gemfile",
|
13
|
+
"image8.gemspec",
|
14
|
+
"lib/image8.rb",
|
15
|
+
]
|
16
|
+
s.add_dependency "sinatra", ">= 1.0"
|
17
|
+
s.add_dependency "async_sinatra", ">= 0.1.5"
|
18
|
+
s.add_dependency "em-http-request", ">= 0.2.7"
|
19
|
+
s.add_dependency "rmagick", ">= 2.13.1"
|
20
|
+
end
|
21
|
+
|
data/lib/image8.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "sinatra/async"
|
3
|
+
require "em-http"
|
4
|
+
require "RMagick"
|
5
|
+
|
6
|
+
class Image8 < Sinatra::Base
|
7
|
+
set :couchdb, "http://127.0.0.1:5984/image8"
|
8
|
+
enable :raise_errors
|
9
|
+
|
10
|
+
register Sinatra::Async
|
11
|
+
|
12
|
+
aget %r|/([0-9x]+)/(.*)| do |format, uri|
|
13
|
+
# This is retarded.
|
14
|
+
if !request.query_string.empty?
|
15
|
+
uri += "?#{request.query_string}"
|
16
|
+
end
|
17
|
+
|
18
|
+
doc_id = encode_uri("#{format}/#{uri}")
|
19
|
+
doc_uri = settings.couchdb + "/#{doc_id}/image"
|
20
|
+
doc_http = EventMachine::HttpRequest.new(doc_uri)
|
21
|
+
|
22
|
+
request = doc_http.get(:timeout => 5)
|
23
|
+
request.callback {
|
24
|
+
expires 31_536_000 # 1 year
|
25
|
+
if request.response_header.status == 200
|
26
|
+
content_type request.response_header["CONTENT_TYPE"]
|
27
|
+
body request.response
|
28
|
+
else
|
29
|
+
resize_image(uri, format) {|img|
|
30
|
+
doc_http.put(
|
31
|
+
:head => {'Content-Type' => img.mime_type},
|
32
|
+
:body => img.to_blob
|
33
|
+
)
|
34
|
+
content_type img.mime_type
|
35
|
+
body img.to_blob
|
36
|
+
}
|
37
|
+
end
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def encode_uri uri
|
42
|
+
uri = URI.encode(uri)
|
43
|
+
uri.gsub! "/", "%2F"
|
44
|
+
uri
|
45
|
+
end
|
46
|
+
|
47
|
+
def resize_image uri, format, &block
|
48
|
+
request = EventMachine::HttpRequest.new(uri).get
|
49
|
+
request.callback {
|
50
|
+
EM.defer {
|
51
|
+
img = Magick::Image.from_blob(request.response).first
|
52
|
+
img.change_geometry!(format) {|width, height|
|
53
|
+
img.resize! width, height
|
54
|
+
}
|
55
|
+
EM.next_tick {
|
56
|
+
block.call(img) if block
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Harry Vangberg
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-06-01 00:00:00 +02:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: sinatra
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 1
|
28
|
+
- 0
|
29
|
+
version: "1.0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: async_sinatra
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
- 1
|
42
|
+
- 5
|
43
|
+
version: 0.1.5
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: em-http-request
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
- 2
|
56
|
+
- 7
|
57
|
+
version: 0.2.7
|
58
|
+
type: :runtime
|
59
|
+
version_requirements: *id003
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rmagick
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 2
|
69
|
+
- 13
|
70
|
+
- 1
|
71
|
+
version: 2.13.1
|
72
|
+
type: :runtime
|
73
|
+
version_requirements: *id004
|
74
|
+
description: dynamic image resizing.
|
75
|
+
email: harry@vangberg.name
|
76
|
+
executables: []
|
77
|
+
|
78
|
+
extensions: []
|
79
|
+
|
80
|
+
extra_rdoc_files: []
|
81
|
+
|
82
|
+
files:
|
83
|
+
- README
|
84
|
+
- Gemfile
|
85
|
+
- image8.gemspec
|
86
|
+
- lib/image8.rb
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://github.com/ichverstehe/image8
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
requirements: []
|
111
|
+
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.3.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: dynamic image resizing.
|
117
|
+
test_files: []
|
118
|
+
|