Ruby/Rails - RMagick, Upload and Resize without a model

Hi everyone!
I have a difficult time with Ruby/Rails and RMagick upload and resizing.

In user_controller of my Rails application I have the following code:

def save_picture
user = User.find(session[:user_id])

folder = (user.id/1000).to_i
path = "#{RAILS_ROOT}/public/pictures/#{folder}"

FileUtils.mkdir_p(path) if !File.exists?(path)

File.open("#{path}/#{user.id}.jpg", "wb") do |f|
  f.write(@params['picture_file'].read)
end

redirect_to :action => 'profile'

end

This interacts with the action of the same controller called profile.
It is used to upload a picture.

I have written this but still I want to resize the picture in two sizes
(thumb and 200x200) with RMagick without a model or anything like that.
Now the upload you see works like uploading the picture to a category
which is named like the round of this: user_id/1000
This was made for performance reasons but as I said I want to make with
RMagick resizing and checking the image if it is a png or jpg and then
create them in the directory (I don’t wont to write the actual file on
the filesystem).

Please help!
Thanks in advance!


By the way!
I have RMagick installed and run Mongrel on Windows XP (this is the
development platform… the deploy is made on a Linux)!

and just in case you are interested this is part of the profile view:


Avatar


Choose a picture for your avatar
The picture will be shown in the hall of fame.

The picture should be jpg about 100K

Yavor D. wrote:

Hi everyone!
I have a difficult time with Ruby/Rails and RMagick upload and resizing.

Out of sheer curiosity, why don’t you want to use a model? Not sure if
you’re aware, but your model doesn’t need to be an ActiveRecord class -
it can just be a regular class. It just keeps things cleaner IMO.

Cliff

Cliff R. wrote:

Yavor D. wrote:

Hi everyone!
I have a difficult time with Ruby/Rails and RMagick upload and resizing.

Out of sheer curiosity, why don’t you want to use a model? Not sure if
you’re aware, but your model doesn’t need to be an ActiveRecord class -
it can just be a regular class. It just keeps things cleaner IMO.

Cliff

Hmmm yep!
Not aware of this… but anyway how can I do the things I want to do
whithout getting pictures into the database I really have trouble with
it?

Some strange errors last night :frowning:

need some push

Yavor D. wrote:

Out of sheer curiosity, why don’t you want to use a model? Not sure if

Some strange errors last night :frowning:

need some push

What are these “strange errors”?

Cliff R. wrote:

Yavor D. wrote:

Out of sheer curiosity, why don’t you want to use a model? Not sure if

Some strange errors last night :frowning:

need some push

What are these “strange errors”?

I tried to get/load the image from @params[‘picture_file’] but when I
did
I got errors from the profile view which was a line that has nothing to
do
with this action … like just printing a variable and then the mongrel
freezes and I needed to restart it… so I think it is a bug or
something.

So please tell me how to get/load the image so I can manipulate it.

I’m fairly new to rails, but I think I can help you.
I think you wan to use RMagick’s from_blob flunction. It takes an
image as binary data and gives you an Magick Image object. Like so:

image = Magick::Image.from_blob(@params[‘picture_file’].read).first

Then you can manipulate it. For instance resample:

thumb = image.sample(200, 200)

Then write to file:

thumb.write(…

hope this helps

–andrei

On Jun 10, 6:45 pm, Yavor D. [email protected]

andreim wrote:

I’m fairly new to rails, but I think I can help you.
I think you wan to use RMagick’s from_blob flunction. It takes an
image as binary data and gives you an Magick Image object. Like so:

image = Magick::Image.from_blob(@params[‘picture_file’].read).first

Then you can manipulate it. For instance resample:

thumb = image.sample(200, 200)

Then write to file:

thumb.write(…

hope this helps

–andrei

On Jun 10, 6:45 pm, Yavor D. [email protected]

Thanks :slight_smile:
It actually worked!

By the way there was a problem when including Magick which I followed by
a tutorial and a book… strange but any way now it works.

The only think that now bothers me is the resizing itself.
It produces a very low class resized image. Isn’t there a way
to resize the image better like keeping the proportions ?!?
I tried a google search but nothing found.

Can someone assist a bit.
A link to a material on the net would be enough good I think.

Thanks in advance!

and very very thanks to andreim!!!

You can find the rMagick Docs here:
http://www.imagemagick.org/RMagick/doc/

There are a number of ways to resize an image, but I have been using
change_geometry to maintain aspect ratio.
I don’t remember the exact syntax, but the docs should be enough to
get you on your way.

http://www.imagemagick.org/RMagick/doc/image1.html#change_geometry

(I’m not sure if this works, but it should be close)
image = Magick::Image.from_blob(@params[‘picture_file’].read).first
image.change_geometry(“400x300”){|cols, rows, i| i.resize!
(cols,rows) }