Class: Dragonfly::ImageMagick::Processor

Inherits:
Object
  • Object
show all
Includes:
Configurable, Utils
Defined in:
lib/dragonfly/image_magick/processor.rb

Constant Summary

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. '300x200!' 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})?$/
THUMB_GEOMETRY =
Regexp.union RESIZE_GEOMETRY, CROPPED_RESIZE_GEOMETRY, CROP_GEOMETRY

Instance Attribute Summary

Attributes included from Loggable

#log_object

Instance Method Summary (collapse)

Methods included from Configurable

included

Methods included from Loggable

#log, #log=, #use_same_log_as

Methods included from Shell

#escape_args, #quote, #raise_shell_command_failed, #run

Instance Method Details

- (Object) auto_orient(temp_object)



30
31
32
# File 'lib/dragonfly/image_magick/processor.rb', line 30

def auto_orient(temp_object)
  convert(temp_object, "-auto-orient")
end

- (Object) convert(temp_object, args = '', format = nil)



102
103
104
# File 'lib/dragonfly/image_magick/processor.rb', line 102

def convert(temp_object, args='', format=nil)
  format ? [super, {:format => format.to_sym}] : super
end

- (Object) crop(temp_object, opts = {})



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dragonfly/image_magick/processor.rb', line 34

def crop(temp_object, opts={})
  width   = opts[:width]
  height  = opts[:height]
  gravity = GRAVITIES[opts[:gravity]]
  x       = "#{opts[:x] || 0}"
  x = '+' + x unless x[/^[+-]/]
  y       = "#{opts[:y] || 0}"
  y = '+' + y unless y[/^[+-]/]
  repage  = opts[:repage] == false ? '' : '+repage'
  resize  = opts[:resize]
    
  convert(temp_object, "#{"-resize #{resize} " if resize}#{"-gravity #{gravity} " if gravity}-crop #{width}x#{height}#{x}#{y} #{repage}")
end

- (Object) flip(temp_object)



48
49
50
# File 'lib/dragonfly/image_magick/processor.rb', line 48

def flip(temp_object)
  convert(temp_object, "-flip")
end

- (Object) flop(temp_object)



52
53
54
# File 'lib/dragonfly/image_magick/processor.rb', line 52

def flop(temp_object)
  convert(temp_object, "-flop")
end

- (Object) greyscale(temp_object) Also known as: grayscale



56
57
58
# File 'lib/dragonfly/image_magick/processor.rb', line 56

def greyscale(temp_object)
  convert(temp_object, "-colorspace Gray")
end

- (Object) resize(temp_object, geometry)



26
27
28
# File 'lib/dragonfly/image_magick/processor.rb', line 26

def resize(temp_object, geometry)
  convert(temp_object, "-resize #{geometry}")
end

- (Object) resize_and_crop(temp_object, opts = {})



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dragonfly/image_magick/processor.rb', line 61

def resize_and_crop(temp_object, opts={})
  if !opts[:width] && !opts[:height]
    return temp_object
  elsif !opts[:width] || !opts[:height]
    attrs          = identify(temp_object)
    opts[:width]   ||= attrs[:width]
    opts[:height]  ||= attrs[:height]
  end

  opts[:gravity] ||= 'c'

  opts[:resize]  = "#{opts[:width]}x#{opts[:height]}^^"
  crop(temp_object, opts)
end

- (Object) rotate(temp_object, amount, opts = {})



76
77
78
# File 'lib/dragonfly/image_magick/processor.rb', line 76

def rotate(temp_object, amount, opts={})
  convert(temp_object, "-rotate #{amount}#{opts[:qualifier]}")
end

- (Object) strip(temp_object)



80
81
82
# File 'lib/dragonfly/image_magick/processor.rb', line 80

def strip(temp_object)
  convert(temp_object, "-strip")
end

- (Object) thumb(temp_object, geometry)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dragonfly/image_magick/processor.rb', line 84

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