Class: Anisoptera::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/anisoptera/commander.rb

Overview

Simple interface to ImageMagick main commands Most of it borrowed from DragonFly github.com/markevans/dragonfly/blob/master/lib/dragonfly/image_magick/processor.rb

Constant Summary collapse

GRAVITIES =
{
  'nw' => 'NorthWest',
  'n'  => 'North',
  'ne' => 'NorthEast',
  'w'  => 'West',
  'c'  => 'Center',
  'e'  => 'East',
  'sw' => 'SouthWest',
  's'  => 'South',
  'se' => 'SouthEast'
}
RESIZE_GEOMETRY =

Geometry string patterns

/^\d*x\d*[><%^!]?$|^\d+@$/
CROPPED_RESIZE_GEOMETRY =

e.g. β€˜20x50#ne’

/^(\d+)x(\d+)[#|-](\w{1,2})?$/
CROP_GEOMETRY =

e.g. β€˜30x30+10+10’

/^(\d+)x(\d+)([+-]\d+)?([+-]\d+)?(\w{1,2})?$/

Instance Method Summary collapse

Constructor Details

#initialize(base_path, convert_command = nil) ⇒ Commander

Returns a new instance of Commander.



26
27
28
29
30
31
# File 'lib/anisoptera/commander.rb', line 26

def initialize(base_path, convert_command = nil)
  @convert_command = convert_command || 'convert'
  @base_path = base_path
  @original = nil
  @geometry = nil
end

Instance Method Details

#check_fileObject



103
104
105
# File 'lib/anisoptera/commander.rb', line 103

def check_file
  ::File.exists?(full_file_path)
end

#command(file_path = nil) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/anisoptera/commander.rb', line 87

def command(file_path = nil)
  raise ArgumentError, "no original file provided. Do commander.file('some_file.jpg')" unless @original
  file_path ||= full_file_path
  cmd = []
  cmd << @geometry if @geometry
  cmd << @greyscale if @greyscale
  cmd << @square if @square # should clear command
  if @encode
    cmd << "#{@encode}:-" # to stdout
  else
    cmd << '-'
  end
  
  "#{@convert_command} #{file_path} " + cmd.join(' ')
end

#encode(format) ⇒ Object



38
39
40
41
# File 'lib/anisoptera/commander.rb', line 38

def encode(format)
  @encode = format
  self
end

#file(path) ⇒ Object



33
34
35
36
# File 'lib/anisoptera/commander.rb', line 33

def file(path)
  @original = path
  self
end

#greyscaleObject



50
51
52
53
# File 'lib/anisoptera/commander.rb', line 50

def greyscale
  @greyscale = '-colorspace Gray'
  self
end

#mime_typeObject



74
75
76
77
# File 'lib/anisoptera/commander.rb', line 74

def mime_type
  ext = @encode ? ".#{@encode}" : File.extname(@original) 
  Rack::Mime.mime_type ext
end

#square(size) ⇒ Object



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

def square(size)
  size = size.to_i
  d = size * 2
  @square = "-thumbnail x#{d} -resize '#{d}x<' -resize 50% -gravity center -crop #{size}x#{size}+0+0"
  self
end

#thumb(geometry) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/anisoptera/commander.rb', line 55

def thumb(geometry)
  @geometry = case geometry
  when RESIZE_GEOMETRY
    resize(geometry)
  when CROPPED_RESIZE_GEOMETRY
    resize_and_crop(:width => $1, :height => $2, :gravity => $3)
  when CROP_GEOMETRY
    crop(
      :width => $1,
      :height => $2,
      :x => $3,
      :y => $4,
      :gravity => $5
    )
  else raise ArgumentError, "Didn't recognise the geometry string #{geometry}."
  end
  self
end

#to_file(path) ⇒ Object

Utility method to test that all commands are working



81
82
83
84
85
# File 'lib/anisoptera/commander.rb', line 81

def to_file(path)
  File.open(path, 'w+') do |f|
    f.write(`#{command}`)
  end
end