couch_photo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +80 -0
- data/lib/couch_photo.rb +5 -0
- data/lib/couch_photo/attachment.rb +9 -0
- data/lib/couch_photo/couch_photo.rb +44 -0
- data/lib/couch_photo/variations.rb +39 -0
- metadata +87 -0
data/README.markdown
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
#Introduction
|
2
|
+
|
3
|
+
A mixin for auto-generating image variations on Image documents with CouchDB.
|
4
|
+
|
5
|
+
##Installation
|
6
|
+
|
7
|
+
$ gem install couch_photo
|
8
|
+
|
9
|
+
## Documentation
|
10
|
+
|
11
|
+
Browse the documentation on rdoc.info: http://rdoc.info/github/moonmaster9000/couch_photo
|
12
|
+
|
13
|
+
##How does it work?
|
14
|
+
|
15
|
+
Just "include CouchPhoto" in your "CouchRest::Model::Base" classes and define some variations.
|
16
|
+
|
17
|
+
###Simple Variations
|
18
|
+
|
19
|
+
Let's imagine we want to auto-generate small, medium, and large variations of our image everytime it gets updated.
|
20
|
+
|
21
|
+
First, define an Image document and set the versions on it:
|
22
|
+
|
23
|
+
class Image < CouchRest::Model::Base
|
24
|
+
use_database IMAGE_DB
|
25
|
+
include CouchPhoto
|
26
|
+
|
27
|
+
override_id! # the id of the document will be the basename of the original image file
|
28
|
+
|
29
|
+
variations do
|
30
|
+
small "20x20"
|
31
|
+
medium "100x100"
|
32
|
+
large "500x500"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Next, create an instance of the image document and set the "original" image on it.
|
37
|
+
|
38
|
+
i = Image.new
|
39
|
+
i.original "avatar.jpg"
|
40
|
+
i.save
|
41
|
+
|
42
|
+
By calling the original method with a string, CouchPhoto will attempt to open a file with the same name and use that as the original image. If you don't have
|
43
|
+
an actual file (e.g., maybe this was a form upload), then simply pass a blob as the second parameter. For example,
|
44
|
+
|
45
|
+
blob = File.read "avatar.jpg"
|
46
|
+
i.original "avatar.jpg", blob
|
47
|
+
|
48
|
+
Now, if you look in your image document in CouchDB, you'd see the following attachments:
|
49
|
+
|
50
|
+
http://your_couch_server/your_image_database/avatar.jpg/original.jpg
|
51
|
+
http://your_couch_server/your_image_database/avatar.jpg/variations/small.jpg
|
52
|
+
http://your_couch_server/your_image_database/avatar.jpg/variations/medium.jpg
|
53
|
+
http://your_couch_server/your_image_database/avatar.jpg/variations/large.jpg
|
54
|
+
|
55
|
+
Notice that, because we called the `override_id!` method, `CouchPhoto` set the ID of your image document to the basename of your original image.
|
56
|
+
|
57
|
+
###Complex Variations
|
58
|
+
|
59
|
+
The previous variations were all simple image resizings. What if we wanted to do something a little more complex? Like grayscale the original image? Or create a watermark? No prob!
|
60
|
+
|
61
|
+
class Image < CouchRest::Model::Base
|
62
|
+
use_database IMAGE_DB
|
63
|
+
include CouchPhoto
|
64
|
+
|
65
|
+
override_id! # the id of the document will be the basename of the original image file
|
66
|
+
|
67
|
+
variations do
|
68
|
+
grayscale do |original_image|
|
69
|
+
original_image.monochrome
|
70
|
+
end
|
71
|
+
|
72
|
+
watermark do |original_image|
|
73
|
+
original_image.composite(MiniMagick::Image.open("watermark.png", "jpg") do |c|
|
74
|
+
c.gravity "center"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
The `original_image` variable in the blocks is simply the MiniMagick::Image instance of your original image.
|
data/lib/couch_photo.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module CouchPhoto
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
def original(filepath, blob=nil)
|
7
|
+
self["_id"] = File.basename filepath if self.class.override_id?
|
8
|
+
blob ||= File.read filepath
|
9
|
+
image_format = filepath.match(/^.*\.([^\.]*)$/)[1]
|
10
|
+
attachment_name = "original.#{image_format}"
|
11
|
+
attachment = {:name => attachment_name, :file => Attachment.new(blob, attachment_name)}
|
12
|
+
update_or_create_attachment attachment
|
13
|
+
self.class.variation_definitions.run_variations(blob).each do |variation_name, variation_blob|
|
14
|
+
update_or_create_attachment :name => "variations/#{variation_name}.#{image_format}", :file => Attachment.new(variation_blob, attachment_name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_or_create_attachment(attachment)
|
19
|
+
if self.has_attachment? attachment[:name]
|
20
|
+
self.update_attachment attachment
|
21
|
+
else
|
22
|
+
self.create_attachment attachment
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def variations(&block)
|
28
|
+
raise "You must pass a block to the `variations' method." unless block
|
29
|
+
variation_definitions.instance_eval(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def variation_definitions
|
33
|
+
@variation_definitions ||= Variations.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def override_id!
|
37
|
+
@override_id = true
|
38
|
+
end
|
39
|
+
|
40
|
+
def override_id?
|
41
|
+
@override_id
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CouchPhoto
|
2
|
+
class Variations
|
3
|
+
def initialize
|
4
|
+
@variations = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(method_name, *args, &block)
|
8
|
+
if block
|
9
|
+
@variations[method_name.to_sym] = Variation.new method_name, *args, &block
|
10
|
+
else
|
11
|
+
@variations[method_name.to_sym] = Variation.new method_name, *args
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def run_variations(blob)
|
16
|
+
results = {}
|
17
|
+
@variations.each do |variation_name, variation|
|
18
|
+
results[variation_name] = variation.create_variation blob
|
19
|
+
end
|
20
|
+
results
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Variation
|
25
|
+
def initialize(name, format=nil, &block)
|
26
|
+
raise "A variation can not have both a format and a block. Choose one." if format and block
|
27
|
+
raise "A variation must have either a format (e.g., '20x20>') or a block." if !format and !block
|
28
|
+
@name = name
|
29
|
+
@format = format
|
30
|
+
@block = block
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_variation(blob)
|
34
|
+
image = MiniMagick::Image.read(blob)
|
35
|
+
@format ? image.resize(@format) : @block.call(image)
|
36
|
+
image.to_blob
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: couch_photo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Parker
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-27 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: couchrest_model
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: -1848230057
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta7
|
35
|
+
version: 1.0.0.beta7
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
description: Automatically create image variations on your CouchDB image documents.
|
39
|
+
email: moonmaster9000@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README.markdown
|
46
|
+
files:
|
47
|
+
- lib/couch_photo.rb
|
48
|
+
- lib/couch_photo/attachment.rb
|
49
|
+
- lib/couch_photo/couch_photo.rb
|
50
|
+
- lib/couch_photo/variations.rb
|
51
|
+
- README.markdown
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/moonmaster9000/couch_photo
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.7
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Mixin for CouchDB/couchrest_model image models.
|
86
|
+
test_files: []
|
87
|
+
|