Class: Dragonfly::Minimagick::Processor

Inherits:
Object
  • Object
show all
Includes:
Configurable, Utils
Defined in:
lib/dragonfly-minimagick/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

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dragonfly-minimagick/processor.rb', line 30

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

  unless opts.has_key?(:width) and opts.has_key?(:height) or opts.has_key?(:x) and opts.has_key?(:y)
    return minimagick_image(temp_object) do |image|
      image
    end
  end
  minimagick_image(temp_object) do |image|
    unless opts.has_key?(:width) and opts.has_key?(:height)
      width  = image[:width] - x
      height = image[:height] - y
    end
    # Minimagick throws an error if the cropping area is bigger than the image,
    # when the gravity is something other than nw
    width  = image[:width]  - x if x + width  > image[:width]
    height = image[:height] - y if y + height > image[:height]
# p "\n\n#{opts[:width]}x#{opts[:height]}+#{opts[:x]}+#{opts[:y]} \n#{width}x#{height}+#{x}+#{y}"
    image.gravity gravity unless gravity.nil? or gravity.empty?
    image.crop "#{width}x#{height}+#{x}+#{y}" #(gravity, x, y, width, height)
    image
  end
end

#flip(temp_object) ⇒ Object



58
59
60
61
62
63
# File 'lib/dragonfly-minimagick/processor.rb', line 58

def flip(temp_object)
  minimagick_image(temp_object) do |image|
    image.flip
    image
  end
end

#flop(temp_object) ⇒ Object



65
66
67
68
69
70
# File 'lib/dragonfly-minimagick/processor.rb', line 65

def flop(temp_object)
  minimagick_image(temp_object) do |image|
    image.flop
    image
  end
end

#greyscale(temp_object, opts = {}) ⇒ Object Also known as: grayscale



72
73
74
75
76
77
78
# File 'lib/dragonfly-minimagick/processor.rb', line 72

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

#resize(temp_object, geometry) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/dragonfly-minimagick/processor.rb', line 81

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dragonfly-minimagick/processor.rb', line 91

def resize_and_crop(temp_object, opts={})
  unless opts.has_key?(:width) or opts.has_key?(:height)
    return minimagick_image(temp_object) do |image|
      image
    end
  end

  minimagick_image(temp_object) do |image|
    width   = opts[:width] ? opts[:width].to_i : image[:width]
    height  = opts[:height] ? opts[:height].to_i : image[:height]
    gravity = GRAVITIES[opts[:gravity]] || 'center'
    image.combine_options do |i|
      i.resize "#{width}x#{height}^"
      i.gravity gravity
      i.extent "#{width}x#{height}"
    end
    # image.crop_resized(width, height, gravity)
    image
  end
end

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



112
113
114
115
116
117
118
119
120
121
# File 'lib/dragonfly-minimagick/processor.rb', line 112

def rotate(temp_object, amount, opts={})
  args = [amount.to_f]
  args << opts[:qualifier] if opts[:qualifier]
  minimagick_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.join(''))
    image
  end
end

#thumb(temp_object, geometry) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dragonfly-minimagick/processor.rb', line 123

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



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dragonfly-minimagick/processor.rb', line 141

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

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