RE: Re: RE: Rmagick Howto?

One of the non-obvious things that RMagick does is that the from_blob
method generates an array of images, even if your source image does not
have animation available. So, for a JPEG file, an array of one image is
created, and you have to index into that array to get the image, hence
the call to the first method.

The cool thing about this is that you can build and manipulate animated
GIFs, as well as static images.


From: Anatol P. [mailto:[email protected]]
Sent: Thursday, December 08, 2005 9:33 AM
To: [email protected]
Subject: Re: [Rails] Re: RE: Rmagick Howto?

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

On 12/8/05, Scott NJ [email protected] wrote:

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)?


Posted via http://www.ruby-forum.com/.

Thanks for that explanation Tom. It really cleared things up. I have
everything working now!
-Scott

Tom wrote:

One of the non-obvious things that RMagick does is that the from_blob
method generates an array of images, even if your source image does not
have animation available. So, for a JPEG file, an array of one image is
created, and you have to index into that array to get the image, hence
the call to the first method.

The cool thing about this is that you can build and manipulate animated
GIFs, as well as static images.