Raymond Brigleb
2005-Apr-28 03:08 UTC
RMagick image + thumbnail creation question / plea for help
Hello everybody. I''m probably asking more than my ''fair share'' of these kinds of questions on here, but I''ve had such good luck (and everybody''s been so incredibly helpful) that I can''t help myself.... so here comes another one! I have an image controller with the following method: def create @image = Image.new(@params[:image]) # Store a copy of the userid who created this image @image.user_id = @session[''user''].id # Store the filename that we''ll be using (user_id-image_id.jpg) @image.filename = @image.user_id.to_s + "-" + Time.now.to_f.to_s + ".jpg" if @image.save File.open("#{RAILS_ROOT}/public/pictures/# {@image.filename}", "wb") do |f| f.write(@params[''picture_file''].read) end img = Image.new "#{RAILS_ROOT}/public/pictures/# {@image.filename}" thumb = img.scale(0.25) thumb.write "#{RAILS_ROOT}/public/pictures/# {@image.filename}-thumb.jpg" flash[''notice''] = ''Image was successfully created.'' redirect_to :action => ''list'' else render_action ''new'' end end (I am including "RMagick" at the top of the controller....) The purpose of this method is to create an image, and give it a unique filename, from the user''s upload. Everything worked just fine, it was great for me and I can get a unique filename this way, so I was happy. But then I wanted to use RMagick to generate a simple thumbnail (the code here is rough) and it blows up. And when I say "blows up," I mean I get the ever-so-unhelpful "Application error (Rails)" message that I''ve grown so fond of... The problem is somewhere in the three lines starting with img = ... but this is basically code I grabbed off the RMagick site so I''m not sure how far off I could be. I''m just trying to save another version of the file, a thumbnail, generated from the file that I first receive. Should be simple enough... and should be working. (Or so he thought...) Any ideas why this blows up? A thousand more thank-yous, Raymond
Jeremy Hinegardner
2005-Apr-28 05:31 UTC
Re: RMagick image + thumbnail creation question / plea for help
I think it might be because of your Image.new(). In RMagick that is a constructor for creating a new image from nothing and its parameters are the columns and rows. You may want to use Image.read() instead. I am assuming that @params[:image] contains the temporary filename of the uploaded file, or it is the File object that is associated with the uploaded file. http://www.simplesystems.org/RMagick/doc/image1.html#read If it doesn''t happen every time and only happens after a period of time and multiple operations you might be encountering the out of memory error that happens with RMagick. This is one of those rare circumstances where you have to do a little worrying about memory with ruby. http://rubyforge.org/forum/forum.php?thread_id=1374&forum_id=1618 enjoy, -jeremy On Wed, Apr 27, 2005 at 08:08:17PM -0700, Raymond Brigleb wrote:> Hello everybody. I''m probably asking more than my ''fair share'' of > these kinds of questions on here, but I''ve had such good luck (and > everybody''s been so incredibly helpful) that I can''t help myself.... > so here comes another one! > > I have an image controller with the following method: > > def create > @image = Image.new(@params[:image]) > # Store a copy of the userid who created this image > @image.user_id = @session[''user''].id > # Store the filename that we''ll be using (user_id-image_id.jpg) > @image.filename = @image.user_id.to_s + "-" + > Time.now.to_f.to_s + ".jpg" > > if @image.save > File.open("#{RAILS_ROOT}/public/pictures/# > {@image.filename}", "wb") do |f| > f.write(@params[''picture_file''].read) > end > img = Image.new "#{RAILS_ROOT}/public/pictures/# > {@image.filename}" > thumb = img.scale(0.25) > thumb.write "#{RAILS_ROOT}/public/pictures/# > {@image.filename}-thumb.jpg" > > flash[''notice''] = ''Image was successfully created.'' > redirect_to :action => ''list'' > else > render_action ''new'' > end > end > > (I am including "RMagick" at the top of the controller....) The > purpose of this method is to create an image, and give it a unique > filename, from the user''s upload. Everything worked just fine, it was > great for me and I can get a unique filename this way, so I was > happy. But then I wanted to use RMagick to generate a simple > thumbnail (the code here is rough) and it blows up. And when I say > "blows up," I mean I get the ever-so-unhelpful "Application error > (Rails)" message that I''ve grown so fond of... > > The problem is somewhere in the three lines starting with img = ... > but this is basically code I grabbed off the RMagick site so I''m not > sure how far off I could be. I''m just trying to save another version > of the file, a thumbnail, generated from the file that I first > receive. Should be simple enough... and should be working. (Or so he > thought...) > > Any ideas why this blows up? > > A thousand more thank-yous, > Raymond > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- ======================================================================= Jeremy Hinegardner jeremy-eg9WJcVQRd1EMKyauTCisw@public.gmane.org
Brian L.
2005-Apr-29 23:54 UTC
Re: RMagick image + thumbnail creation question / plea for help
I use this in my model: def image=(tmp_file) filename = tmp_file.original_filename.gsub(/[^a-zA-Z0-9.]/, ''_'') data = tmp_file.read img = Image.from_blob(data)[0] tiny = img.change_geometry("80x80") { |w,h,img| img.resize(w,h) } medium = img.change_geometry("200x200") { |w,h,img| img.resize(w,h) } full = img.change_geometry("800x600") { |w,h,img| img.resize(w,h) } write_attribute(''tiny_thumb'', tiny.to_blob) write_attribute(''medium_thumb'', medium.to_blob) write_attribute(''full_size'', full.to_blob) end and it works just fine taking the arguments from @params directly in the ItemImage.new constructor (where ItemImage is my model). It''s on my list for refinement (the filename handling is not the best), but it works. Brian On 4/28/05, Jeremy Hinegardner <jeremy-eg9WJcVQRd1EMKyauTCisw@public.gmane.org> wrote:> I think it might be because of your Image.new(). In RMagick that is a > constructor for creating a new image from nothing and its parameters are > the columns and rows. You may want to use Image.read() instead. > > I am assuming that @params[:image] contains the temporary filename of > the uploaded file, or it is the File object that is associated with the > uploaded file. > > http://www.simplesystems.org/RMagick/doc/image1.html#read > > If it doesn''t happen every time and only happens after a period of time > and multiple operations you might be encountering the out of memory > error that happens with RMagick. This is one of those rare > circumstances where you have to do a little worrying about memory with > ruby. > > http://rubyforge.org/forum/forum.php?thread_id=1374&forum_id=1618 > > enjoy, > > -jeremy > > On Wed, Apr 27, 2005 at 08:08:17PM -0700, Raymond Brigleb wrote: > > Hello everybody. I''m probably asking more than my ''fair share'' of > > these kinds of questions on here, but I''ve had such good luck (and > > everybody''s been so incredibly helpful) that I can''t help myself.... > > so here comes another one! > > > > I have an image controller with the following method: > > > > def create > > @image = Image.new(@params[:image]) > > # Store a copy of the userid who created this image > > @image.user_id = @session[''user''].id > > # Store the filename that we''ll be using (user_id-image_id.jpg) > > @image.filename = @image.user_id.to_s + "-" + > > Time.now.to_f.to_s + ".jpg" > > > > if @image.save > > File.open("#{RAILS_ROOT}/public/pictures/# > > {@image.filename}", "wb") do |f| > > f.write(@params[''picture_file''].read) > > end > > img = Image.new "#{RAILS_ROOT}/public/pictures/# > > {@image.filename}" > > thumb = img.scale(0.25) > > thumb.write "#{RAILS_ROOT}/public/pictures/# > > {@image.filename}-thumb.jpg" > > > > flash[''notice''] = ''Image was successfully created.'' > > redirect_to :action => ''list'' > > else > > render_action ''new'' > > end > > end > > > > (I am including "RMagick" at the top of the controller....) The > > purpose of this method is to create an image, and give it a unique > > filename, from the user''s upload. Everything worked just fine, it was > > great for me and I can get a unique filename this way, so I was > > happy. But then I wanted to use RMagick to generate a simple > > thumbnail (the code here is rough) and it blows up. And when I say > > "blows up," I mean I get the ever-so-unhelpful "Application error > > (Rails)" message that I''ve grown so fond of... > > > > The problem is somewhere in the three lines starting with img = ... > > but this is basically code I grabbed off the RMagick site so I''m not > > sure how far off I could be. I''m just trying to save another version > > of the file, a thumbnail, generated from the file that I first > > receive. Should be simple enough... and should be working. (Or so he > > thought...) > > > > Any ideas why this blows up? > > > > A thousand more thank-yous, > > Raymond > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > -- > =======================================================================> Jeremy Hinegardner jeremy-eg9WJcVQRd1EMKyauTCisw@public.gmane.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)