image8 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/Gemfile +1 -1
  2. data/README +4 -1
  3. data/image8.gemspec +2 -2
  4. data/lib/image8.rb +41 -20
  5. metadata +3 -3
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  gem "async_sinatra", "0.1.5"
4
4
  gem "em-http-request", "0.2.7"
5
- gem "rmagick", "2.13.1"
5
+ gem "image_science", "1.2.1"
data/README CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  image8 resizes images on-the-fly and stores them in couchdb for later
4
4
  retrival and easy replication. image8 should be used behind a varnish
5
- or squid. <work in progress>.
5
+ or squid. image8 uses rmagick. must be served with thin, because of its
6
+ async nature.
6
7
 
7
8
  == config.ru
8
9
 
@@ -21,6 +22,8 @@ proportionality.
21
22
 
22
23
  == misc
23
24
 
25
+ this has only been tested on ruby 1.9.1.
26
+
24
27
  image8 is released under the mit license. (c) 2010 harry vangberg
25
28
  <harry@vangberg.name>, memolane <info@memolane.com>.
26
29
 
data/image8.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "image8"
3
- s.version = "0.1"
4
- s.date = "2010-06-01"
3
+ s.version = "0.2"
4
+ s.date = "2010-06-17"
5
5
  s.summary = "dynamic image resizing."
6
6
  s.email = "harry@vangberg.name"
7
7
  s.homepage = "http://github.com/ichverstehe/image8"
data/lib/image8.rb CHANGED
@@ -15,24 +15,27 @@ class Image8 < Sinatra::Base
15
15
  uri += "?#{request.query_string}"
16
16
  end
17
17
 
18
- doc_id = encode_uri("#{format}/#{uri}")
19
- doc_uri = settings.couchdb + "/#{doc_id}/image"
20
- doc_http = EventMachine::HttpRequest.new(doc_uri)
18
+ doc_id = encode_uri(uri)
19
+ doc_uri = settings.couchdb + "/#{doc_id}"
20
+ doc_http = EventMachine::HttpRequest.new(doc_uri + "/" + format)
21
21
 
22
22
  request = doc_http.get(:timeout => 5)
23
23
  request.callback {
24
24
  expires 31_536_000 # 1 year
25
25
  if request.response_header.status == 200
26
+ p "Serving straight from cache.."
26
27
  content_type request.response_header["CONTENT_TYPE"]
27
28
  body request.response
28
29
  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
30
+ download_original(uri, doc_uri) {|blob|
31
+ resize_image(blob, format) {|img|
32
+ doc_http.put(
33
+ :head => {'Content-Type' => img.mime_type},
34
+ :body => img.to_blob
35
+ )
36
+ content_type img.mime_type
37
+ body img.to_blob
38
+ }
36
39
  }
37
40
  end
38
41
  }
@@ -44,18 +47,36 @@ class Image8 < Sinatra::Base
44
47
  uri
45
48
  end
46
49
 
47
- def resize_image uri, format, &block
48
- request = EventMachine::HttpRequest.new(uri).get
50
+ def download_original uri, doc_uri
51
+ cache = EventMachine::HttpRequest.new(doc_uri + "/full")
52
+ request = cache.get
49
53
  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
54
+ if request.response_header.status == 200
55
+ p "Serving original from cache.."
56
+ yield request.response if block_given?
57
+ else
58
+ p "Downloading original.."
59
+ original = EventMachine::HttpRequest.new(uri).get
60
+ original.callback {
61
+ req = cache.put(
62
+ :head => {'Content-Type' => original.response_header["CONTENT_TYPE"]},
63
+ :body => original.response
64
+ )
65
+ req.callback { yield original.response if block_given? }
57
66
  }
58
- }
67
+ end
68
+ }
69
+ end
70
+
71
+ def resize_image blob, format, &block
72
+ p "Resizing image.."
73
+ t1 = Time.now
74
+ img = Magick::Image.from_blob(blob).first
75
+ img.change_geometry!(format) {|width, height|
76
+ img.resize! width, height
59
77
  }
78
+ t2 = Time.now
79
+ p t2 - t1
80
+ block.call(img) if block
60
81
  end
61
82
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Harry Vangberg
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-06-01 00:00:00 +02:00
16
+ date: 2010-06-17 00:00:00 +02:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency