Module: UploadColumn::Manipulators::RMagick

Defined in:
lib/upload_column/manipulators/rmagick.rb

Instance Method Summary collapse

Instance Method Details

#convert!(format) ⇒ Object

Convert the image to format



26
27
28
29
30
31
# File 'lib/upload_column/manipulators/rmagick.rb', line 26

def convert!(format)
  manipulate! do |img|
    img.format = format.to_s.upcase
    img
  end
end

#crop_resized!(geometry) ⇒ Object

Resize and crop the image so that it will have the exact dimensions passed via geometry, geometry should be a string, formatted like ‘200x100’ where the first number is the height and the second is the width



51
52
53
54
55
56
# File 'lib/upload_column/manipulators/rmagick.rb', line 51

def crop_resized!( geometry )
  manipulate! do |img|
    h, w = geometry.split('x')
    img.crop_resized(h.to_i,w.to_i)
  end
end

#load_manipulator_dependenciesObject

:nodoc:



10
11
12
# File 'lib/upload_column/manipulators/rmagick.rb', line 10

def load_manipulator_dependencies #:nodoc:
  require 'RMagick'
end

#manipulate!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/upload_column/manipulators/rmagick.rb', line 58

def manipulate!
  image = ::Magick::Image.read(self.path)

  if image.size > 1
    list = ::Magick::ImageList.new
    image.each do |frame|
      list << yield( frame )
    end
    list.write(self.path)
  else
    yield( image.first ).write(self.path)
  end
rescue ::Magick::ImageMagickError => e
  # this is a more meaningful error message, which we could catch later
  raise ManipulationError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}")
end

#process!(instruction = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/upload_column/manipulators/rmagick.rb', line 14

def process!(instruction = nil, &block)
  if instruction.is_a?(Proc)
    manipulate!(&instruction)
  elsif instruction.to_s =~ /^c(\d+x\d+)$/
    crop_resized!($1)
  elsif instruction.to_s =~ /^(\d+x\d+)$/
    resize!($1)
  end
  manipulate!(&block) if block
end

#resize!(geometry) ⇒ Object

Resize the image so that it will not exceed the dimensions passed via geometry, geometry should be a string, formatted like ‘200x100’ where the first number is the height and the second is the width



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/upload_column/manipulators/rmagick.rb', line 36

def resize!( geometry )
  manipulate! do |img|
    img.change_geometry( geometry ) do |c, r, i|
      if i.rows > c || i.columns > r
        i.resize(c,r)
      else
        i
      end
    end
  end
end