Forum: Ruby on Rails RE: Rmagick Howto?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tom (Guest)
on 2005-12-08 08:03
(Received via mailing list)
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.
Scott N. (Guest)
on 2005-12-08 19:09
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)?
anatol.pomozov (Guest)
on 2005-12-08 19:33
(Received via mailing list)
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
This topic is locked and can not be replied to.