Analysers
An Analyser analyses a particular property of a piece of content, e.g. the width of an image, the bitrate of an audio file, etc.
One can be added using a block
Dragonfly.app.configure do
analyser :depth do |content|
# ...
end
# ...
end
or providing an object that responds to call
(MyAnalyser
in this case)
Dragonfly.app.configure do
analyser :depth, MyAnalyser
# ...
end
Using the analyser
The analyser is available as a method to Job
objects
image = Dragonfly.app.fetch('some/uid')
image.depth
and Attachment
objects
image = my_model.photo
image.depth
Implementing the analyser
The content
object yielded to the block/call
method is a Dragonfly::Content - see the doc for methods it provides.
Returning the property
Simply return the calculated property. You will probably want to use one of the Content
methods for getting the data such as data
(String), file
, path
, etc.
analyser :depth do |content|
SomeLibrary.get_depth(content.data)
end
Using shell commands
To use the shell, you can use Content#shell_eval
analyser :depth do |content|
content.shell_eval do |path|
"/usr/bin/get_depth #{path}" # The command sent to the command line
end
end
The yielded path
above will always exist.
Using pre-registered analysers
To use a pre-registered analyser, use Content#analyse
analyser :bytes_per_pixel do |content|
num_pixels = content.analyse(:width) * content.analyse(:height)
content.size.to_f / num_pixels
end
ImageMagick
The ImageMagick plugin adds a few analysers - see the doc for more details.
“Magic” Model Attributes
To automatically store analysed properties in your model see Models - Magic Attributes
Derived from theme by orderedlist