Class: Dragonfly::Processing::RMagickProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/processing/r_magick_processor.rb

Constant Summary collapse

GRAVITIES =
{
  'nw' => Magick::NorthWestGravity,
  'n'  => Magick::NorthGravity,
  'ne' => Magick::NorthEastGravity,
  'w'  => Magick::WestGravity,
  'c'  => Magick::CenterGravity,
  'e'  => Magick::EastGravity,
  'sw' => Magick::SouthWestGravity,
  's'  => Magick::SouthGravity,
  'se' => Magick::SouthEastGravity
}
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

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 30

def crop(temp_object, opts={})
  x       = opts[:x].to_i
  y       = opts[:y].to_i
  gravity = GRAVITIES[opts[:gravity]] || Magick::ForgetGravity
  width   = opts[:width].to_i
  height  = opts[:height].to_i

  rmagick_image(temp_object) do |image|
    # RMagick throws an error if the cropping area is bigger than the image,
    # when the gravity is something other than nw
    width  = image.columns - x if x + width  > image.columns
    height = image.rows    - y if y + height > image.rows
    image.crop(gravity, x, y, width, height)
  end
end

#flip(temp_object) ⇒ Object



46
47
48
49
50
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 46

def flip(temp_object)
  rmagick_image(temp_object) do |image|
    image.flip!
  end
end

#flop(temp_object) ⇒ Object



52
53
54
55
56
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 52

def flop(temp_object)
  rmagick_image(temp_object) do |image|
    image.flop!
  end
end

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



58
59
60
61
62
63
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 58

def greyscale(temp_object, opts={})
  depth = opts[:depth] || 256
  rmagick_image(temp_object) do |image|
    image.quantize(depth, Magick::GRAYColorspace)
  end
end

#resize(temp_object, geometry) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 66

def resize(temp_object, geometry)
  rmagick_image(temp_object) do |image|
    image.change_geometry!(geometry) do |cols, rows, img|
     img.resize!(cols, rows)
    end
  end
end

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 74

def resize_and_crop(temp_object, opts={})
  rmagick_image(temp_object) do |image|

    width   = opts[:width] ? opts[:width].to_i : image.columns
    height  = opts[:height] ? opts[:height].to_i : image.rows
    gravity = GRAVITIES[opts[:gravity]] || Magick::CenterGravity

    image.crop_resized(width, height, gravity)
  end
end

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



85
86
87
88
89
90
91
92
93
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 85

def rotate(temp_object, amount, opts={})
  args = [amount.to_f]
  args << opts[:qualifier] if opts[:qualifier]
  rmagick_image(temp_object) do |image|
    image.background_color = opts[:background_colour] if opts[:background_colour]
    image.background_color = opts[:background_color] if opts[:background_color]
    image.rotate(*args) || temp_object
  end
end

#thumb(temp_object, geometry) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 95

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

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/dragonfly/processing/r_magick_processor.rb', line 113

def vignette(temp_object, opts={})
  x      = opts[:x].to_f      || temp_object.width  * 0.1
  y      = opts[:y].to_f      || temp_object.height * 0.1
  radius = opts[:radius].to_f ||  0.0
  sigma  = opts[:sigma].to_f  || 10.0

  rmagick_image(temp_object) do |image|
    image.vignette(x, y, radius, sigma)
  end
end