RE: Rmagick Howto?

Here’s some code to take an image and create one that has a maximum
dimension of 100 pixels:

img_big = Magick::Image.from_blob(file_field.read).first
img_smaller = img_big.change_geometry("100x100") { |cols, rows, img|
  img.resize(cols, rows)
}
thumbnail_column_data = img_smaller.to_blob { self.quality = 75 }

Remember to always follow up the RMagick calls with:

fDisabled = GC.enable
GC.start
GC.disable if fDisabled

Otherwise Ruby will soon run out of memory.

I am trying to add a thumbnail to the image upload example in the “Agile
Web D. with Rails” book.

I added a blob column “thumbnail” to the table.
Added “require ‘RMagick’” to the bottom of environment.rb

My model looks like:


class Picture < ActiveRecord::Base
belongs_to :picture_category
validates_format_of :content_type, :with => /^image/,
:message => “— you can only upload pictures”

def picture=(picture_field)
self.name = base_part_of(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
self.data = picture_field.read

img_big = Magick::Image.from_blob(self.data)
img_smaller = img_big.change_geometry("100x100") { |cols, rows, img|
  img.resize(cols, rows)
}
self.thumbnail = img_smaller.to_blob { self.quality = 75 }

end
def base_part_of(file_name)
name = File.basename(file_name)
name.gsub(/[^\w._-]/, ‘’)
end
end


The error I am getting is:
undefined method `change_geometry’ for [ JPEG 1024x768 DirectClass 8-bit
201kb]:Array

I am sure there are probably mutiple mistakes. What am I doing wrong
(besides error recovery and validation, one step at a time)?

Try this code

img = Magick::Image.from_blob(self.content_data).first

img.change_geometry!('140x200') { |cols, rows, i|
  i.resize!(cols, rows)
}

self.thumbnail = img.to_blob

GC.start