Class: Dragonfly::ImageMagick::Processor

Inherits:
Object
  • Object
show all
Includes:
Configurable, Utils
Defined in:
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*[><%^!]?$|^\[email protected]$/
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 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

#auto_orient(temp_object) ⇒ Object


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

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

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


108
109
110
# File 'lib/dragonfly/image_magick/processor.rb', line 108

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

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


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

def crop(temp_object, opts={})
  opts = Dragonfly::Utils.symbolize_keys(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

#flip(temp_object) ⇒ Object


50
51
52
# File 'lib/dragonfly/image_magick/processor.rb', line 50

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

#flop(temp_object) ⇒ Object


54
55
56
# File 'lib/dragonfly/image_magick/processor.rb', line 54

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

#greyscale(temp_object) ⇒ Object Also known as: grayscale


58
59
60
# File 'lib/dragonfly/image_magick/processor.rb', line 58

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

#resize(temp_object, geometry) ⇒ Object


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

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

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


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

def resize_and_crop(temp_object, opts={})
  opts = Dragonfly::Utils.symbolize_keys(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

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


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

def rotate(temp_object, amount, opts={})
  opts = Dragonfly::Utils.symbolize_keys(opts)

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

#strip(temp_object) ⇒ Object


86
87
88
# File 'lib/dragonfly/image_magick/processor.rb', line 86

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

#thumb(temp_object, geometry) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dragonfly/image_magick/processor.rb', line 90

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