dragonfly 0.9.15 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of dragonfly might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +1 -8
- data/.travis.yml +11 -0
- data/Gemfile +1 -0
- data/History.md +52 -2
- data/LICENSE +1 -1
- data/README.md +38 -95
- data/dev/grid.jpg +0 -0
- data/dev/irbrc.rb +27 -0
- data/dev/rails_template.rb +38 -0
- data/dev/test.ru +56 -0
- data/dev/test_rails +19 -0
- data/dragonfly.gemspec +3 -21
- data/lib/dragonfly.rb +45 -44
- data/lib/dragonfly/app.rb +175 -96
- data/lib/dragonfly/configurable.rb +71 -170
- data/lib/dragonfly/content.rb +211 -0
- data/lib/dragonfly/core_ext/object.rb +1 -6
- data/lib/dragonfly/file_data_store.rb +197 -0
- data/lib/dragonfly/image_magick/analysers/image_properties.rb +23 -0
- data/lib/dragonfly/image_magick/generators/convert.rb +19 -0
- data/lib/dragonfly/image_magick/generators/plain.rb +26 -0
- data/lib/dragonfly/image_magick/generators/plasma.rb +25 -0
- data/lib/dragonfly/image_magick/generators/text.rb +127 -0
- data/lib/dragonfly/image_magick/plugin.rb +83 -0
- data/lib/dragonfly/image_magick/processors/convert.rb +29 -0
- data/lib/dragonfly/image_magick/processors/encode.rb +18 -0
- data/lib/dragonfly/image_magick/processors/thumb.rb +76 -0
- data/lib/dragonfly/job.rb +118 -134
- data/lib/dragonfly/job_endpoint.rb +2 -0
- data/lib/dragonfly/memory_data_store.rb +34 -0
- data/lib/dragonfly/middleware.rb +5 -3
- data/lib/dragonfly/{active_model_extensions.rb → model.rb} +5 -3
- data/lib/dragonfly/{active_model_extensions → model}/attachment.rb +40 -35
- data/lib/dragonfly/{active_model_extensions → model}/attachment_class_methods.rb +36 -40
- data/lib/dragonfly/model/class_methods.rb +109 -0
- data/lib/dragonfly/{active_model_extensions → model}/instance_methods.rb +2 -2
- data/lib/dragonfly/{active_model_extensions → model}/validations.rb +17 -12
- data/lib/dragonfly/railtie.rb +8 -6
- data/lib/dragonfly/register.rb +27 -0
- data/lib/dragonfly/response.rb +47 -52
- data/lib/dragonfly/routed_endpoint.rb +4 -0
- data/lib/dragonfly/serializer.rb +15 -5
- data/lib/dragonfly/server.rb +56 -29
- data/lib/dragonfly/shell.rb +12 -23
- data/lib/dragonfly/spec/data_store_examples.rb +64 -0
- data/lib/dragonfly/temp_object.rb +32 -47
- data/lib/dragonfly/url_attributes.rb +19 -22
- data/lib/dragonfly/url_mapper.rb +3 -0
- data/lib/dragonfly/utils.rb +12 -0
- data/lib/dragonfly/version.rb +1 -1
- data/lib/dragonfly/whitelist.rb +19 -0
- data/lib/rails/generators/dragonfly/USAGE +8 -0
- data/lib/rails/generators/dragonfly/dragonfly_generator.rb +24 -0
- data/lib/rails/generators/dragonfly/templates/initializer.rb.erb +27 -0
- data/samples/gif.gif +0 -0
- data/spec/dragonfly/app_spec.rb +270 -64
- data/spec/dragonfly/configurable_spec.rb +142 -418
- data/spec/dragonfly/content_spec.rb +353 -0
- data/spec/dragonfly/cookie_monster_spec.rb +2 -1
- data/spec/dragonfly/file_data_store_spec.rb +301 -0
- data/spec/dragonfly/image_magick/analysers/image_properties_spec.rb +20 -0
- data/spec/dragonfly/image_magick/generators/convert_spec.rb +19 -0
- data/spec/dragonfly/image_magick/generators/plain_spec.rb +50 -0
- data/spec/dragonfly/image_magick/generators/plasma_spec.rb +32 -0
- data/spec/dragonfly/image_magick/generators/text_spec.rb +77 -0
- data/spec/dragonfly/image_magick/plugin_spec.rb +131 -0
- data/spec/dragonfly/image_magick/processors/convert_spec.rb +56 -0
- data/spec/dragonfly/image_magick/processors/thumb_spec.rb +173 -0
- data/spec/dragonfly/job_endpoint_spec.rb +30 -73
- data/spec/dragonfly/job_spec.rb +280 -608
- data/spec/dragonfly/memory_data_store_spec.rb +20 -0
- data/spec/dragonfly/middleware_spec.rb +47 -27
- data/spec/dragonfly/{active_model_extensions → model}/model_spec.rb +331 -555
- data/spec/dragonfly/model/validations_spec.rb +196 -0
- data/spec/dragonfly/register_spec.rb +35 -0
- data/spec/dragonfly/routed_endpoint_spec.rb +6 -6
- data/spec/dragonfly/serializer_spec.rb +13 -15
- data/spec/dragonfly/server_spec.rb +122 -46
- data/spec/dragonfly/shell_spec.rb +43 -24
- data/spec/dragonfly/temp_object_spec.rb +69 -94
- data/spec/dragonfly/url_attributes_spec.rb +49 -0
- data/spec/dragonfly/utils_spec.rb +32 -0
- data/spec/dragonfly/whitelist_spec.rb +30 -0
- data/spec/dragonfly_spec.rb +43 -0
- data/spec/fixtures/deprecated_stored_content/eggs.bonus +1 -0
- data/spec/fixtures/deprecated_stored_content/eggs.bonus.meta +1 -0
- data/spec/functional/configuration_spec.rb +19 -0
- data/spec/functional/model_urls_spec.rb +43 -41
- data/spec/functional/remote_on_the_fly_spec.rb +14 -16
- data/spec/functional/shell_commands_spec.rb +24 -14
- data/spec/functional/to_response_spec.rb +10 -10
- data/spec/functional/urls_spec.rb +5 -3
- data/spec/spec_helper.rb +23 -28
- data/spec/support/argument_matchers.rb +7 -8
- data/spec/support/image_matchers.rb +14 -36
- data/spec/support/model_helpers.rb +97 -0
- data/spec/support/simple_matchers.rb +12 -0
- metadata +92 -393
- data/.yardopts +0 -29
- data/Rakefile +0 -36
- data/config.ru +0 -14
- data/docs.watchr +0 -1
- data/extra_docs/Analysers.md +0 -68
- data/extra_docs/Caching.md +0 -23
- data/extra_docs/Configuration.md +0 -149
- data/extra_docs/Couch.md +0 -49
- data/extra_docs/DataStorage.md +0 -226
- data/extra_docs/Encoding.md +0 -67
- data/extra_docs/ExampleUseCases.md +0 -116
- data/extra_docs/GeneralUsage.md +0 -105
- data/extra_docs/Generators.md +0 -68
- data/extra_docs/Heroku.md +0 -50
- data/extra_docs/ImageMagick.md +0 -136
- data/extra_docs/Index.md +0 -33
- data/extra_docs/MimeTypes.md +0 -40
- data/extra_docs/Models.md +0 -441
- data/extra_docs/Mongo.md +0 -42
- data/extra_docs/Processing.md +0 -77
- data/extra_docs/Rack.md +0 -52
- data/extra_docs/Rails2.md +0 -57
- data/extra_docs/Rails3.md +0 -56
- data/extra_docs/ServingRemotely.md +0 -104
- data/extra_docs/Sinatra.md +0 -25
- data/extra_docs/URLs.md +0 -203
- data/features/images.feature +0 -47
- data/features/no_processing.feature +0 -14
- data/features/rails.feature +0 -8
- data/features/steps/common_steps.rb +0 -8
- data/features/steps/dragonfly_steps.rb +0 -66
- data/features/steps/rails_steps.rb +0 -40
- data/features/support/env.rb +0 -13
- data/features/support/setup.rb +0 -41
- data/fixtures/rails/files/app/models/album.rb +0 -6
- data/fixtures/rails/files/app/views/albums/new.html.erb +0 -7
- data/fixtures/rails/files/app/views/albums/show.html.erb +0 -6
- data/fixtures/rails/files/config/initializers/dragonfly.rb +0 -4
- data/fixtures/rails/files/features/manage_album_images.feature +0 -38
- data/fixtures/rails/files/features/step_definitions/helper_steps.rb +0 -7
- data/fixtures/rails/files/features/step_definitions/image_steps.rb +0 -25
- data/fixtures/rails/files/features/step_definitions/web_steps.rb +0 -189
- data/fixtures/rails/files/features/support/paths.rb +0 -17
- data/fixtures/rails/files/features/text_images.feature +0 -7
- data/fixtures/rails/template.rb +0 -20
- data/irbrc.rb +0 -19
- data/lib/dragonfly/active_model_extensions/class_methods.rb +0 -98
- data/lib/dragonfly/analyser.rb +0 -58
- data/lib/dragonfly/analysis/file_command_analyser.rb +0 -33
- data/lib/dragonfly/analysis/image_magick_analyser.rb +0 -6
- data/lib/dragonfly/config/heroku.rb +0 -26
- data/lib/dragonfly/config/image_magick.rb +0 -6
- data/lib/dragonfly/config/rails.rb +0 -20
- data/lib/dragonfly/data_storage.rb +0 -11
- data/lib/dragonfly/data_storage/couch_data_store.rb +0 -83
- data/lib/dragonfly/data_storage/file_data_store.rb +0 -144
- data/lib/dragonfly/data_storage/mongo_data_store.rb +0 -96
- data/lib/dragonfly/data_storage/s3data_store.rb +0 -168
- data/lib/dragonfly/encoder.rb +0 -13
- data/lib/dragonfly/encoding/image_magick_encoder.rb +0 -6
- data/lib/dragonfly/function_manager.rb +0 -67
- data/lib/dragonfly/generation/image_magick_generator.rb +0 -6
- data/lib/dragonfly/generator.rb +0 -9
- data/lib/dragonfly/image_magick/analyser.rb +0 -53
- data/lib/dragonfly/image_magick/config.rb +0 -44
- data/lib/dragonfly/image_magick/encoder.rb +0 -57
- data/lib/dragonfly/image_magick/generator.rb +0 -147
- data/lib/dragonfly/image_magick/processor.rb +0 -114
- data/lib/dragonfly/image_magick/utils.rb +0 -46
- data/lib/dragonfly/image_magick_utils.rb +0 -4
- data/lib/dragonfly/job_builder.rb +0 -39
- data/lib/dragonfly/job_definitions.rb +0 -30
- data/lib/dragonfly/loggable.rb +0 -28
- data/lib/dragonfly/processing/image_magick_processor.rb +0 -6
- data/lib/dragonfly/processor.rb +0 -9
- data/lib/dragonfly/rails/images.rb +0 -32
- data/lib/dragonfly/simple_cache.rb +0 -23
- data/spec/dragonfly/active_model_extensions/spec_helper.rb +0 -95
- data/spec/dragonfly/analyser_spec.rb +0 -123
- data/spec/dragonfly/analysis/file_command_analyser_spec.rb +0 -49
- data/spec/dragonfly/data_storage/couch_data_store_spec.rb +0 -84
- data/spec/dragonfly/data_storage/file_data_store_spec.rb +0 -308
- data/spec/dragonfly/data_storage/mongo_data_store_spec.rb +0 -81
- data/spec/dragonfly/data_storage/s3_data_store_spec.rb +0 -277
- data/spec/dragonfly/data_storage/shared_data_store_examples.rb +0 -77
- data/spec/dragonfly/function_manager_spec.rb +0 -154
- data/spec/dragonfly/image_magick/analyser_spec.rb +0 -73
- data/spec/dragonfly/image_magick/encoder_spec.rb +0 -46
- data/spec/dragonfly/image_magick/generator_spec.rb +0 -178
- data/spec/dragonfly/image_magick/processor_spec.rb +0 -293
- data/spec/dragonfly/job_builder_spec.rb +0 -37
- data/spec/dragonfly/job_definitions_spec.rb +0 -57
- data/spec/dragonfly/loggable_spec.rb +0 -80
- data/spec/dragonfly/simple_cache_spec.rb +0 -27
- data/spec/dragonfly/url_attributes.rb +0 -47
- data/spec/functional/deprecations_spec.rb +0 -51
- data/spec/functional/image_magick_app_spec.rb +0 -27
- data/spec/test_imagemagick.ru +0 -49
- data/yard/handlers/configurable_attr_handler.rb +0 -38
- data/yard/setup.rb +0 -15
- data/yard/templates/default/fulldoc/html/css/common.css +0 -109
- data/yard/templates/default/layout/html/layout.erb +0 -93
- data/yard/templates/default/module/html/configuration_summary.erb +0 -31
- data/yard/templates/default/module/setup.rb +0 -17
data/extra_docs/Encoding.md
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
Encoding
|
2
|
-
========
|
3
|
-
Registered encoders change the format of data, e.g. a jpeg image to a png image.
|
4
|
-
|
5
|
-
You can register as many encoders as you like.
|
6
|
-
|
7
|
-
Let's say we have a Dragonfly app
|
8
|
-
|
9
|
-
app = Dragonfly[:images]
|
10
|
-
|
11
|
-
and an image object (actually a {Dragonfly::Job Job} object)...
|
12
|
-
|
13
|
-
image = app.fetch('some/uid')
|
14
|
-
|
15
|
-
...OR a Dragonfly model accessor...
|
16
|
-
|
17
|
-
image = @album.cover_image
|
18
|
-
|
19
|
-
We can encode it to any format registered with the encoder.
|
20
|
-
|
21
|
-
Lazy evaluation
|
22
|
-
---------------
|
23
|
-
|
24
|
-
gif_image = image.encode(:gif)
|
25
|
-
|
26
|
-
doesn't actually do anything until you call something on the returned {Dragonfly::Job Job} object, like `url`, `data`, etc.
|
27
|
-
|
28
|
-
Bang method
|
29
|
-
-----------
|
30
|
-
|
31
|
-
image.encode!(:gif)
|
32
|
-
|
33
|
-
modifies the image object itself, rather than returning a new object.
|
34
|
-
|
35
|
-
ImageMagick Encoder
|
36
|
-
-------------------
|
37
|
-
See {file:ImageMagick}.
|
38
|
-
|
39
|
-
Custom Encoders
|
40
|
-
---------------
|
41
|
-
|
42
|
-
To register a custom encoder, for e.g. pdf format:
|
43
|
-
|
44
|
-
app.encoder.add do |temp_object, format|
|
45
|
-
throw :unable_to_handle unless format == :pdf
|
46
|
-
# use temp_object.data, temp_object.path, temp_object.file, etc.
|
47
|
-
SomeLibrary.convert_to_pdf(temp_object.data)
|
48
|
-
# return a String, Pathname, File or Tempfile
|
49
|
-
end
|
50
|
-
|
51
|
-
pdf_image = image.encode(:pdf)
|
52
|
-
|
53
|
-
If `:unable_to_handle` is thrown, the next most recently registered encoder is used, and so on.
|
54
|
-
|
55
|
-
Alternatively you can create a class like the ImageMagick one above, which implements the method `encode`, and register this.
|
56
|
-
|
57
|
-
class MyEncoder
|
58
|
-
|
59
|
-
def encode(temp_object, format, *args)
|
60
|
-
SomeLib.encode(temp_object.data, format, *args)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
app.encoder.register(MyEncoder)
|
66
|
-
|
67
|
-
pdf_image = image.encode(:pdf, :some => :args)
|
@@ -1,116 +0,0 @@
|
|
1
|
-
Example Use Cases
|
2
|
-
=================
|
3
|
-
Below are a number of examples of uses for Dragonfly which differ slightly from the standard image resizing with model attachments.
|
4
|
-
|
5
|
-
Non-image attachments in Rails
|
6
|
-
------------------------------
|
7
|
-
When using `'dragonfly/rails'images'` or similar configuration, if you're not calling `thumb`, `width`
|
8
|
-
or other image-related methods on your content, then non-image attachments should _just work_.
|
9
|
-
|
10
|
-
class User
|
11
|
-
image_accessor :mugshot
|
12
|
-
end
|
13
|
-
|
14
|
-
user.mugshot = Rails.root.join('some/text_file.txt')
|
15
|
-
user.save!
|
16
|
-
user.mugshot.url # /media/BAsfsdfajkl....
|
17
|
-
|
18
|
-
Furthermore, the imagemagick configuration gives you an `image?` analyser method which always returns a boolean
|
19
|
-
so you can still make a thumbnail if it is an image
|
20
|
-
|
21
|
-
user.mugshot.thumb!('400x300#') if user.mugshot.image?
|
22
|
-
|
23
|
-
`'dragonfly/rails/images'` also gives you a `file_accessor` macro which is actually just the same as `image_accessor`,
|
24
|
-
but a bit more meaningful when not dealing with images
|
25
|
-
|
26
|
-
class User
|
27
|
-
file_accessor :mugshot
|
28
|
-
end
|
29
|
-
|
30
|
-
You can always define your own macro for dealing with non-image attachments:
|
31
|
-
|
32
|
-
Dragonfly[:my_app].define_macro(ActiveRecord::Base, :attachment_accessor)
|
33
|
-
|
34
|
-
class User
|
35
|
-
attachment_accessor :mugshot
|
36
|
-
end
|
37
|
-
|
38
|
-
see {file:Models} for how to define macros with non-ActiveRecord libraries.
|
39
|
-
|
40
|
-
Quick custom image processing in Rails
|
41
|
-
--------------------------------------
|
42
|
-
With the imagemagick configuration, we can easily use `convert` to do some custom processing, e.g. in the view:
|
43
|
-
|
44
|
-
<%= image_tag @user.mugshot.thumb('300x300#').convert('-blur 4x2').url %>
|
45
|
-
|
46
|
-
If we use this a lot, we can define a shortcut for it - in config/initializers/dragonfly.rb:
|
47
|
-
|
48
|
-
Dragonfly[:images].configure do |c|
|
49
|
-
c.job :blurred_square do |size|
|
50
|
-
process :resize_and_crop, :width => size, :height => size
|
51
|
-
process :convert, '-blur 4x2'
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
then in the view:
|
56
|
-
|
57
|
-
<%= image_tag @user.mugshot.blurred_square(300).url %>
|
58
|
-
|
59
|
-
See {file:ImageMagick} for more info.
|
60
|
-
|
61
|
-
Using Javascript to generate on-the-fly thumbnails in Rails
|
62
|
-
-----------------------------------------------------------
|
63
|
-
Supposing we have a `Pancake` model with an image attachment
|
64
|
-
|
65
|
-
pancake = Pancake.create! :image => Pathname.new('path/to/pancake.png')
|
66
|
-
|
67
|
-
Setting the attachment sets the uid field (this example uses the {file:DataStorage#File\_datastore FileDataStore})
|
68
|
-
|
69
|
-
pancake.image_uid # '2011/04/27/17_04_32_705_pancake.png'
|
70
|
-
|
71
|
-
We can set up a Dragonfly endpoint in routes.rb for generating thumbnails:
|
72
|
-
|
73
|
-
match '/thumbs/:geometry' => app.endpoint { |params, app|
|
74
|
-
app.fetch(params[:uid]).thumb(params[:geometry])
|
75
|
-
}
|
76
|
-
|
77
|
-
NOTE: if you use `do`...`end` here instead of curly braces, make sure you put brackets around the arguments to `match`,
|
78
|
-
otherwise Ruby will parse it incorrectly
|
79
|
-
|
80
|
-
If we have access to the image uid in javascript, we can create the url like so:
|
81
|
-
|
82
|
-
var url = '/thumbs/400x300?uid=' + uid
|
83
|
-
|
84
|
-
Then we can get the content with ajax, create an img tag, etc.
|
85
|
-
|
86
|
-
NOTE: in the above example we've put the uid in the query string and not the path because the dot in it confuses Rails' pattern recognition.
|
87
|
-
You could always put it in the path and escape/unescape it either side of the request.
|
88
|
-
|
89
|
-
Also javascript's built-in `encodeURIComponent` function may be useful when Rails has difficulty matching routes due to special characters like '#' and '/'.
|
90
|
-
|
91
|
-
Text generation with Sinatra
|
92
|
-
----------------------------
|
93
|
-
We can easily generate on-the-fly text with Sinatra and the {Dragonfly::ImageMagick::Generator ImageMagick Generator}:
|
94
|
-
|
95
|
-
require 'rubygems'
|
96
|
-
require 'sinatra'
|
97
|
-
require 'dragonfly'
|
98
|
-
|
99
|
-
app = Dragonfly[:images].configure_with(:imagemagick)
|
100
|
-
|
101
|
-
get '/:text' do |text|
|
102
|
-
app.generate(:text, text, :font_size => 30).to_response(env)
|
103
|
-
end
|
104
|
-
|
105
|
-
When we visit '/hello!' we get a generated image of the text "hello!".
|
106
|
-
|
107
|
-
See {file:ImageMagick#Generator} for more details.
|
108
|
-
|
109
|
-
Creating a Dragonfly plugin
|
110
|
-
---------------------------
|
111
|
-
You can create custom {file:DataStorage#Custom\_datastore data stores}, {file:Processing#Custom\_Processors processors},
|
112
|
-
{file:Encoding#Custom\_Encoders encoders}, {file:Analysers#Custom\_Analysers analysers} and {file:Generators#Custom\_Generators generators}, and
|
113
|
-
then tie them all together with a {file:Configuration#Custom\_Saved\_Configuration saved configuration}.
|
114
|
-
|
115
|
-
See [Dragonfly-RMagick](http://github.com/markevans/dragonfly-rmagick) for an example.
|
116
|
-
NOTE: you will probably want to create classes and modules in your own namespace, rather than the `Dragonfly` namespace (even though Dragonfly-RMagick uses it).
|
data/extra_docs/GeneralUsage.md
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
General Usage
|
2
|
-
=============
|
3
|
-
|
4
|
-
You can have multiple dragonfly apps, each with their own configuration.
|
5
|
-
Each app has a name, and is referred to by that name.
|
6
|
-
|
7
|
-
Dragonfly[:images] # ===> Creates an app called 'images'
|
8
|
-
Dragonfly[:images] # ===> Refers to the already created app 'images'
|
9
|
-
|
10
|
-
app = Dragonfly[:images]
|
11
|
-
|
12
|
-
Getting/generating content
|
13
|
-
--------------------------
|
14
|
-
A number of methods can be used to get content:
|
15
|
-
|
16
|
-
app.fetch('some_uid') # Fetch from datastore (default filesystem)
|
17
|
-
|
18
|
-
app.fetch_file('~/path/to/file.png') # Fetch from a local file
|
19
|
-
|
20
|
-
app.fetch_url('somewhere.com/img.png') # Fetch from a url (will work with http, https)
|
21
|
-
|
22
|
-
app.generate(:plasma, 400, 300) # Generates using a method from the configured
|
23
|
-
# generator (in this case a plasma image)
|
24
|
-
|
25
|
-
app.create("CONTENT") # Can pass in a String, Pathname, File or Tempfile
|
26
|
-
|
27
|
-
These all return {Dragonfly::Job Job} objects. These objects are lazy - they don't do any fetching/generating until
|
28
|
-
some other method is called on them.
|
29
|
-
|
30
|
-
Using the content
|
31
|
-
-----------------
|
32
|
-
Once we have a {Dragonfly::Job Job} object:
|
33
|
-
|
34
|
-
image = app.fetch('some_uid')
|
35
|
-
|
36
|
-
We can get the data a number of ways...
|
37
|
-
|
38
|
-
image.data # => "\377???JFIF\000\..."
|
39
|
-
image.to_file('out.png') # writes to file 'out.png' and returns a readable file object
|
40
|
-
image.tempfile # => #<File:/var/folders/st/strHv74sH044JPabSiODz... a closed Tempfile object
|
41
|
-
image.file # => #<File:/var/folders/st/strHv74sH044JPabSiODz... a readable (open) File object
|
42
|
-
image.file do |f| # Yields an open file object, returns the return value of
|
43
|
-
data = f.read(256) # the block, and closes the file object
|
44
|
-
end
|
45
|
-
image.path # => '/var/folders/st/strHv74sH044JPabSiODz...' i.e. the path of the tempfile
|
46
|
-
image.size # => 134507 (size in bytes)
|
47
|
-
|
48
|
-
We can get its url...
|
49
|
-
|
50
|
-
image.url # => "/media/BAhbBlsHOgZmIg9hc..."
|
51
|
-
# this won't work if we've used create to get the content
|
52
|
-
|
53
|
-
We can analyse it (see {file:Analysers} for more info) ...
|
54
|
-
|
55
|
-
image.width # => 280
|
56
|
-
|
57
|
-
We can process it (see {file:Processing} for more info) ...
|
58
|
-
|
59
|
-
new_image = image.process(:thumb, '40x30') # returns another 'Job' object
|
60
|
-
|
61
|
-
We can encode it (see {file:Encoding} for more info) ...
|
62
|
-
|
63
|
-
new_image = image.encode(:gif) # returns another 'Job' object
|
64
|
-
|
65
|
-
Chaining
|
66
|
-
--------
|
67
|
-
Because the methods `fetch`, `fetch_file`, `fetch_url`, `generate`, `create`, `process` and `encode`
|
68
|
-
all return {Dragonfly::Job Job} objects, we can chain them as much as we want...
|
69
|
-
|
70
|
-
image = app.fetch('some_uid').process(:greyscale).process(:thumb, '40x20#').encode(:gif)
|
71
|
-
|
72
|
-
... and because they're lazy, we don't actually do any processing/encoding until either `apply` is called
|
73
|
-
|
74
|
-
image.apply # actually 'does' the processing and returns self
|
75
|
-
|
76
|
-
... or a method is called like `data`, `to_file`, etc.
|
77
|
-
|
78
|
-
This means we can cheaply generate urls for processed data without doing any fetching or processing:
|
79
|
-
|
80
|
-
url = app.fetch('some_uid').process(:thumb, '40x20#').encode(:gif).url
|
81
|
-
|
82
|
-
and then visit that url in a browser to get the actual processed image.
|
83
|
-
|
84
|
-
Shortcuts
|
85
|
-
---------
|
86
|
-
Commonly used processing/encoding steps can be shortened, so instead of
|
87
|
-
|
88
|
-
app.fetch('some_uid').process(:greyscale).process(:thumb, '40x20#').encode(:jpg)
|
89
|
-
|
90
|
-
we could use something like
|
91
|
-
|
92
|
-
app.fetch('some_uid').grey('40x20#')
|
93
|
-
|
94
|
-
This does exactly the same, returning a {Dragonfly::Job Job} object.
|
95
|
-
|
96
|
-
To define this shortcut:
|
97
|
-
|
98
|
-
app.configure do |c|
|
99
|
-
c.job :grey do |size|
|
100
|
-
process :greyscale
|
101
|
-
process :thumb, size
|
102
|
-
encode :jpg
|
103
|
-
end
|
104
|
-
# ...
|
105
|
-
end
|
data/extra_docs/Generators.md
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
Generators
|
2
|
-
==========
|
3
|
-
|
4
|
-
Unlike processors and encoders, generators create content out of nothing, rather than modifying already existing content, for example text image generation.
|
5
|
-
|
6
|
-
You can register as many generators as you like.
|
7
|
-
|
8
|
-
Given a Dragonfly app
|
9
|
-
|
10
|
-
app = Dragonfly[:images]
|
11
|
-
|
12
|
-
we can get generated content using
|
13
|
-
|
14
|
-
image = app.generate(:some_method, :some => :args)
|
15
|
-
|
16
|
-
where `:some_method` is added by the configured generators.
|
17
|
-
|
18
|
-
ImageMagick Generator
|
19
|
-
---------------------
|
20
|
-
See {file:ImageMagick}.
|
21
|
-
|
22
|
-
Custom Generators
|
23
|
-
-----------------
|
24
|
-
To register a single custom generator:
|
25
|
-
|
26
|
-
app.generator.add :triangle do |height|
|
27
|
-
SomeLibrary.create_triangle(height) # return a String, Pathname, File or Tempfile
|
28
|
-
end
|
29
|
-
|
30
|
-
app.generate(:triangle, 10) # => 'Job' object which we can get data, etc.
|
31
|
-
|
32
|
-
Or create a class like the ImageMagick one above, in which case all public methods will be counted as generator methods.
|
33
|
-
|
34
|
-
class RoundedCornerGenerator
|
35
|
-
|
36
|
-
def top_left_corner(opts={})
|
37
|
-
SomeLib.tlc(opts)
|
38
|
-
end
|
39
|
-
|
40
|
-
def bottom_right_corner(opts={})
|
41
|
-
tempfile = Tempfile.new('brc')
|
42
|
-
`some_command -c #{opts[:colour]} -o #{tempfile.path}`
|
43
|
-
tempfile
|
44
|
-
end
|
45
|
-
|
46
|
-
# ...
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def my_helper_method
|
51
|
-
# do stuff
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
app.generator.register(RoundedCornerGenerator)
|
57
|
-
|
58
|
-
app.generate(:top_left_corner, :colour => 'green')
|
59
|
-
app.generate(:bottom_right_corner, :colour => 'mauve')
|
60
|
-
|
61
|
-
You can also return meta data like name and format if you return an array from the generator
|
62
|
-
|
63
|
-
app.generator.add :triangle do |height|
|
64
|
-
[
|
65
|
-
SomeLibrary.create_triangle(height),
|
66
|
-
{:name => 'triangle.png', :format => :png}
|
67
|
-
]
|
68
|
-
end
|
data/extra_docs/Heroku.md
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
Heroku
|
2
|
-
======
|
3
|
-
|
4
|
-
The default configuration won't work out of the box for Heroku, because the platform doesn't allow saving files to the filesystem (although it does use tempfiles).
|
5
|
-
|
6
|
-
Instead of the normal {file:DataStorage#File\_datastore FileDataStore}, we can use the {file:DataStorage#S3\_datastore S3DataStore}.
|
7
|
-
|
8
|
-
Assuming you have an S3 account set up...
|
9
|
-
|
10
|
-
Gem dependencies:
|
11
|
-
|
12
|
-
- fog
|
13
|
-
- dragonfly
|
14
|
-
|
15
|
-
Initializer (e.g. config/initializers/dragonfly.rb in Rails):
|
16
|
-
|
17
|
-
require 'dragonfly'
|
18
|
-
app = Dragonfly[:images]
|
19
|
-
|
20
|
-
app.configure_with(:imagemagick)
|
21
|
-
app.configure_with(:rails)
|
22
|
-
if Rails.env.production?
|
23
|
-
app.configure do |c|
|
24
|
-
c.datastore = Dragonfly::DataStorage::S3DataStore.new(
|
25
|
-
:bucket_name => 'my-bucket-name',
|
26
|
-
:access_key_id => ENV['S3_KEY'],
|
27
|
-
:secret_access_key => ENV['S3_SECRET']
|
28
|
-
)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
app.define_macro(ActiveRecord::Base, :image_accessor)
|
33
|
-
|
34
|
-
The datastore remains as the {Dragonfly::DataStorage::FileDataStore FileDataStore} for non-production environments.
|
35
|
-
|
36
|
-
application.rb if using with Rails:
|
37
|
-
|
38
|
-
config.middleware.insert 1, 'Dragonfly::Middleware', :images
|
39
|
-
|
40
|
-
We don't store the S3 access key and secret in the repository, rather we use Heroku's
|
41
|
-
{http://docs.heroku.com/config-vars config variables} using the command line (we only have to do this once).
|
42
|
-
|
43
|
-
From your app's directory:
|
44
|
-
|
45
|
-
heroku config:add S3_KEY=XXXXXXXXX S3_SECRET=XXXXXXXXXX
|
46
|
-
|
47
|
-
Replace 'XXXXXXXXX' with your access key and secret.
|
48
|
-
|
49
|
-
If this is an issue, you may want to look into storing thumbnails on S3 (see {file:ServingRemotely}), or maybe generating thumbnails _on upload_ (see {file:Models#Up-front_thumbnailing}), or maybe an after-deploy hook for hitting specific Dragonfly urls you want to cache, etc.
|
50
|
-
It won't be a problem for most sites though.
|
data/extra_docs/ImageMagick.md
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
ImageMagick
|
2
|
-
===========
|
3
|
-
Either `require 'dragonfly/rails/images'` or `Dragonfly[:images].configure_with(:imagemagick)`
|
4
|
-
gives us an ImageMagick {Dragonfly::ImageMagick::Processor Processor}, {Dragonfly::ImageMagick::Encoder Encoder},
|
5
|
-
{Dragonfly::ImageMagick::Analyser Analyser} and {Dragonfly::ImageMagick::Generator Generator}.
|
6
|
-
|
7
|
-
Given a {Dragonfly::Job Job} object
|
8
|
-
|
9
|
-
image = app.fetch('some/uid')
|
10
|
-
|
11
|
-
...OR a Dragonfly model accessor...
|
12
|
-
|
13
|
-
image = @album.cover_image
|
14
|
-
|
15
|
-
we have the following:
|
16
|
-
|
17
|
-
Shortcuts
|
18
|
-
---------
|
19
|
-
image.thumb('40x30') # same as image.process(:thumb, '40x30')
|
20
|
-
image.jpg # same as image.encode(:jpg)
|
21
|
-
image.png # same as image.encode(:png)
|
22
|
-
image.gif # same as image.encode(:gif)
|
23
|
-
image.strip # same as image.process(:strip)
|
24
|
-
image.convert('-scale 30x30') # same as image.process(:convert, '-scale 30x30')
|
25
|
-
|
26
|
-
`thumb` and `convert` can optionally take a format (e.g. :gif) as the second argument.
|
27
|
-
Bang methods like `image.thumb!('40x30')`, `image.png!` etc. will operate on `self`.
|
28
|
-
|
29
|
-
Below are some examples of geometry strings for `thumb`:
|
30
|
-
|
31
|
-
'400x300' # resize, maintain aspect ratio
|
32
|
-
'400x300!' # force resize, don't maintain aspect ratio
|
33
|
-
'400x' # resize width, maintain aspect ratio
|
34
|
-
'x300' # resize height, maintain aspect ratio
|
35
|
-
'400x300>' # resize only if the image is larger than this
|
36
|
-
'400x300<' # resize only if the image is smaller than this
|
37
|
-
'50x50%' # resize width and height to 50%
|
38
|
-
'400x300^' # resize width, height to minimum 400,300, maintain aspect ratio
|
39
|
-
'2000@' # resize so max area in pixels is 2000
|
40
|
-
'400x300#' # resize, crop if necessary to maintain aspect ratio (centre gravity)
|
41
|
-
'400x300#ne' # as above, north-east gravity
|
42
|
-
'400x300se' # crop, with south-east gravity
|
43
|
-
'400x300+50+100' # crop from the point 50,100 with width, height 400,300
|
44
|
-
|
45
|
-
Processor
|
46
|
-
---------
|
47
|
-
|
48
|
-
image.process(:crop, :width => 40, :height => 50, :x => 20, :y => 30)
|
49
|
-
image.process(:crop, :width => 40, :height => 50, :gravity => 'ne')
|
50
|
-
|
51
|
-
image.process(:flip) # flips it vertically
|
52
|
-
image.process(:flop) # flips it horizontally
|
53
|
-
|
54
|
-
image.process(:greyscale, :depth => 128) # default depth 256
|
55
|
-
|
56
|
-
image.process(:resize, '40x40')
|
57
|
-
image.process(:resize_and_crop, :width => 40, :height=> 50, :gravity => 'ne')
|
58
|
-
|
59
|
-
image.process(:rotate, 45, :background_colour => 'transparent') # default bg black
|
60
|
-
|
61
|
-
The method `thumb` takes a geometry string and calls `resize`, `resize_and_crop` or `crop` accordingly.
|
62
|
-
|
63
|
-
image.process(:thumb, '400x300') # calls resize
|
64
|
-
|
65
|
-
Encoder
|
66
|
-
-------
|
67
|
-
The {Dragonfly::ImageMagick::Encoder ImageMagick Encoder} gives us:
|
68
|
-
|
69
|
-
image.encode(:jpg)
|
70
|
-
image.encode(:gif)
|
71
|
-
image.encode(:png)
|
72
|
-
image.encode(:tiff)
|
73
|
-
|
74
|
-
and various other formats (see {Dragonfly::ImageMagick::Encoder ImageMagick Encoder}).
|
75
|
-
|
76
|
-
You can also pass additional options to the imagemagick command line:
|
77
|
-
|
78
|
-
image.encode(:jpg, '-quality 10')
|
79
|
-
|
80
|
-
Analyser
|
81
|
-
--------
|
82
|
-
The {Dragonfly::ImageMagick::Analyser ImageMagick Analyser} gives us these methods:
|
83
|
-
|
84
|
-
image.width # => 280
|
85
|
-
image.height # => 355
|
86
|
-
image.aspect_ratio # => 0.788732394366197
|
87
|
-
image.portrait? # => true
|
88
|
-
image.landscape? # => false
|
89
|
-
image.depth # => 8
|
90
|
-
image.number_of_colours # => 34703
|
91
|
-
image.format # => :png
|
92
|
-
image.image? # => true - will return true or false for any content
|
93
|
-
|
94
|
-
Generator
|
95
|
-
---------
|
96
|
-
The {Dragonfly::ImageMagick::Generator ImageMagick Generator} gives us these methods:
|
97
|
-
|
98
|
-
image = app.generate(:plain, 600, 400, 'rgba(40,200,30,0.5)')
|
99
|
-
image = app.generate(:plain, 600, 400, '#ccc', :format => :gif)
|
100
|
-
# generate a 600x400 plain image
|
101
|
-
# any css-style colour should work
|
102
|
-
|
103
|
-
image = app.generate(:plasma, 600, 400, :gif) # generate a 600x400 plasma image
|
104
|
-
# last arg defaults to :png
|
105
|
-
|
106
|
-
image = app.generate(:text, "Hello there") # an image of the text "Hello there"
|
107
|
-
|
108
|
-
image = app.generate(:text, "Hello there",
|
109
|
-
:font_size => 30, # defaults to 12
|
110
|
-
:font_family => 'Monaco',
|
111
|
-
:stroke_color => '#ddd',
|
112
|
-
:color => 'red',
|
113
|
-
:font_style => 'italic',
|
114
|
-
:font_stretch => 'expanded',
|
115
|
-
:font_weight => 'bold',
|
116
|
-
:padding => '30 20 10',
|
117
|
-
:background_color => '#efefef', # defaults to transparent
|
118
|
-
:format => :gif # defaults to png
|
119
|
-
)
|
120
|
-
|
121
|
-
Note that the text generation options are meant to resemble css as much as possible. You can also use, for example, `'font-family'` instead of `:font_family`.
|
122
|
-
|
123
|
-
You can use `padding-top`, `padding-left`, etc., as well as the standard css shortcuts for `padding` (it assumes unit is px).
|
124
|
-
|
125
|
-
An alternative for `:font_family` is `:font` (see {http://www.imagemagick.org/script/command-line-options.php#font the imagemagick docs}), which could be a complete filename.
|
126
|
-
Available fonts are those available on your system.
|
127
|
-
|
128
|
-
Configuration
|
129
|
-
-------------
|
130
|
-
There are some options that can be set, e.g. if the imagemagick convert command can't be found:
|
131
|
-
|
132
|
-
app.configure do |c|
|
133
|
-
c.convert_command = "/opt/local/bin/convert" # defaults to "convert"
|
134
|
-
c.identify_command = "/opt/local/bin/identify" # defaults to "identify"
|
135
|
-
c.log_commands = true # defaults to false
|
136
|
-
end
|