Class: Dragonfly::ImageMagick::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/image_magick/plugin.rb

Overview

The ImageMagick Plugin registers an app with generators, analysers and processors. Look at the source code for #call to see exactly how it configures the app.

Instance Method Summary collapse

Instance Method Details

#call(app, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dragonfly/image_magick/plugin.rb', line 16

def call(app, opts = {})
  # ENV
  app.env[:convert_command] = opts[:convert_command] || "convert"
  app.env[:identify_command] = opts[:identify_command] || "identify"

  # Analysers
  app.add_analyser :image_properties, ImageMagick::Analysers::ImageProperties.new
  app.add_analyser :width do |content|
    content.analyse(:image_properties)["width"]
  end
  app.add_analyser :height do |content|
    content.analyse(:image_properties)["height"]
  end
  app.add_analyser :format do |content|
    content.analyse(:image_properties)["format"]
  end
  app.add_analyser :aspect_ratio do |content|
    attrs = content.analyse(:image_properties)
    attrs["width"].to_f / attrs["height"]
  end
  app.add_analyser :portrait do |content|
    attrs = content.analyse(:image_properties)
    attrs["width"] <= attrs["height"]
  end
  app.add_analyser :landscape do |content|
    !content.analyse(:portrait)
  end
  app.add_analyser :image do |content|
    begin
      content.analyse(:image_properties)["format"] != "pdf"
    rescue Shell::CommandFailed
      false
    end
  end

  # Aliases
  app.define(:portrait?) { portrait }
  app.define(:landscape?) { landscape }
  app.define(:image?) { image }

  # Generators
  app.add_generator :plain, ImageMagick::Generators::Plain.new
  app.add_generator :plasma, ImageMagick::Generators::Plasma.new
  app.add_generator :text, ImageMagick::Generators::Text.new
  app.add_generator :convert do
    raise "The convert generator is deprecated for better security - use Dragonfly::ImageMagick::Commands.generate(content, args, format) instead."
  end

  # Processors
  app.add_processor :encode, Processors::Encode.new
  app.add_processor :thumb, Processors::Thumb.new
  app.add_processor :rotate do |content, amount|
    ParamValidators.validate!(amount, &ParamValidators.is_number)
    Commands.convert(content, "-rotate #{amount}")
  end
  app.add_processor :convert do
    raise "The convert processor is deprecated for better security - use Dragonfly::ImageMagick::Commands.convert(content, args, opts) instead."
  end

  # Extra methods
  app.define :identify do |cli_args = nil|
    shell_eval do |path|
      "#{app.env[:identify_command]} #{cli_args} #{path}"
    end
  end
end