Class: Mapel::Engine::ImageMagick

Inherits:
Mapel::Engine show all
Defined in:
lib/mapel.rb

Instance Attribute Summary

Attributes inherited from Mapel::Engine

#command, #commands, #output, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mapel::Engine

#initialize, #success?

Constructor Details

This class inherits a constructor from Mapel::Engine

Class Method Details

.info(source) ⇒ Object



36
37
38
39
40
41
# File 'lib/mapel.rb', line 36

def self.info(source)
  im = new(source)
  im.commands << 'identify'
  im.commands << source
  im.run.to_info_hash
end

.list(type = nil) ⇒ Object



50
51
52
53
54
# File 'lib/mapel.rb', line 50

def self.list(type = nil)
  im = new
  im.commands << 'convert -list'
  im.run
end

.render(source = nil) ⇒ Object



43
44
45
46
47
48
# File 'lib/mapel.rb', line 43

def self.render(source = nil)
  im = new(source)
  im.commands << 'convert'
  im.commands << source unless source.nil?
  im
end

Instance Method Details

#crop(*args) ⇒ Object



56
57
58
59
# File 'lib/mapel.rb', line 56

def crop(*args)
  @commands << "-crop \"#{Geometry.new(*args).to_s(true)}\""
  self
end

#gravity(type = :center) ⇒ Object



61
62
63
64
# File 'lib/mapel.rb', line 61

def gravity(type = :center)
  @commands << "-gravity #{type}"
  self
end

#repageObject



66
67
68
69
# File 'lib/mapel.rb', line 66

def repage
  @commands << "+repage"
  self
end

#resize(*args) ⇒ Object



71
72
73
74
# File 'lib/mapel.rb', line 71

def resize(*args)
  @commands << "-resize \"#{Geometry.new(*args)}\""
  self
end

#resize!(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mapel.rb', line 76

def resize!(*args)
  @commands << lambda {
    cmd = self.class.new
    width, height = Geometry.new(*args).dimensions
    if @source
      origin_width, origin_height = Mapel.info(@source)[:dimensions]

      # Crop dimensions should not exceed original dimensions.
      width = [width, origin_width].min
      height = [height, origin_height].min

      if width != origin_width || height != origin_height
        scale = [width/origin_width.to_f, height/origin_height.to_f].max
        cmd.resize((scale*(origin_width+0.5)), (scale*(origin_height+0.5)))
      end
      cmd.crop(width, height).to_preview
    else
      "\{crop_resized #{args}\}"
    end
  }
  self
end

#runObject



114
115
116
117
118
# File 'lib/mapel.rb', line 114

def run
  @output = `#{to_preview}`
  @status = ($? == 0)
  self
end

#scale(*args) ⇒ Object



99
100
101
102
# File 'lib/mapel.rb', line 99

def scale(*args)
  @commands << "-scale \"#{Geometry.new(*args)}\""
  self
end

#to(path) ⇒ Object



104
105
106
107
# File 'lib/mapel.rb', line 104

def to(path)
  @commands << path
  self
end

#to_info_hashObject



124
125
126
127
128
# File 'lib/mapel.rb', line 124

def to_info_hash
  meta = {}
  meta[:dimensions] = @output.split(' ')[2].split('x').map { |d| d.to_i }
  meta
end

#to_previewObject



120
121
122
# File 'lib/mapel.rb', line 120

def to_preview
  @commands.map { |cmd| cmd.respond_to?(:call) ? cmd.call : cmd }.join(' ')
end

#undoObject



109
110
111
112
# File 'lib/mapel.rb', line 109

def undo
  @commands.pop
  self
end