I have been trying to get rmagick working in my rails app. Everything I try fails! I am able to run it okay from a *.rb file from my command line, so I know its installed. Are there any tutorials or howtos? I haven''t found anything good on the web. What I am trying is to upload an image from a form, create a thumbnail, and store them in a db. I am able to upload and store the image, just not the thumbnail. Anyone have any recomendations how to do this? -- Posted via http://www.ruby-forum.com/.
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.
-----Original Message-----
From: Scott NJ [mailto:scottnj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Wednesday, December 07, 2005 9:32 PM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Rmagick Howto?
I have been trying to get rmagick working in my rails app. Everything I
try fails! I am able to run it okay from a *.rb file from my command
line, so I know its installed.
Are there any tutorials or howtos? I haven''t found anything good on the
web.
What I am trying is to upload an image from a form, create a thumbnail,
and store them in a db. I am able to upload and store the image, just
not the thumbnail.
Anyone have any recomendations how to do this?
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
I am trying to add a thumbnail to the image upload example in the "Agile
Web Development 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/.
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 <scottnj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I am trying to add a thumbnail to the image upload example in the
"Agile
> Web Development 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/.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
anatol (http://pomozov.info)
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
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 Pomozov
[mailto:anatol.pomozov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Thursday, December 08, 2005 9:33 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
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 <scottnj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
I am trying to add a thumbnail to the image upload example in the "Agile
Web Development 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/.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
--
anatol (http://pomozov.info)
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
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. >-- Posted via http://www.ruby-forum.com/.