Class: Dragonfly::Processing::ImageMagickProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/processing/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})?$/
THUMB_GEOMETRY =
Regexp.union RESIZE_GEOMETRY, CROPPED_RESIZE_GEOMETRY, CROP_GEOMETRY

Instance Method Summary collapse

Instance Method Details

#convert(temp_object, args = '', format = nil) ⇒ Object



93
94
95
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 93

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

#crop(temp_object, opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 29

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[/^[+-]/]

  convert(temp_object, "-crop #{width}x#{height}#{x}#{y}#{" -gravity #{gravity}" if gravity}")
end

#flip(temp_object) ⇒ Object



41
42
43
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 41

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

#flop(temp_object) ⇒ Object



45
46
47
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 45

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

#greyscale(temp_object) ⇒ Object



49
50
51
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 49

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

#resize(temp_object, geometry) ⇒ Object



25
26
27
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 25

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

#resize_and_crop(temp_object, opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 54

def resize_and_crop(temp_object, opts={})
  attrs          = identify(temp_object)
  current_width  = attrs[:width].to_i
  current_height = attrs[:height].to_i
  
  width   = opts[:width]  ? opts[:width].to_i  : current_width
  height  = opts[:height] ? opts[:height].to_i : current_height
  gravity = opts[:gravity] || 'c'

  if width != current_width || height != current_height
    scale = [width.to_f / current_width, height.to_f / current_height].max
    temp_object = TempObject.new(resize(temp_object, "#{(scale * current_width).ceil}x#{(scale * current_height).ceil}"))
  end

  crop(temp_object, :width => width, :height => height, :gravity => gravity)
end

#rotate(temp_object, amount, opts = {}) ⇒ Object



71
72
73
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 71

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

#thumb(temp_object, geometry) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dragonfly/processing/image_magick_processor.rb', line 75

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