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

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.
Yavor D. (Guest)
on 2007-06-11 02:45
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:

-----

<div class="profile_div_left">
            <h2 class="profile_title">Avatar</h2>
            <hr size="1" style="width: 300px;" />
            <strong style="color: #999999; font-weight: bold; font-size:
11px;">
                Choose a picture for your avatar <br />
                The picture will be shown in the hall of fame.
            </strong>
            <form action="/user/save_picture" method="post"
ENCTYPE="multipart/form-data">
                <input type='file' style='width: 290px;'
name="picture_file" />
                <br />
                <span style="font-size: 11px; font-style: italic;">
                    The picture should be <u>jpg</u> about 100K
                </span>
                <br />
                <input type="submit" name="upload" value="Upload
Picture" style="font-size: 12px;" />
            </form>
        </div>

-----
Cliff R. (Guest)
on 2007-06-11 13:32
(Received via mailing list)
Yavor D. wrote:
> Hi everyone!
> I have a difficult time with Ruby/Rails and RMagick upload and resizing.
>
<snip>

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
Yavor D. (Guest)
on 2007-06-11 15:05
Cliff R. wrote:
> Yavor D. wrote:
>> Hi everyone!
>> I have a difficult time with Ruby/Rails and RMagick upload and resizing.
>>
> <snip>
>
> 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 :(

need some push
Cliff R. (Guest)
on 2007-06-11 19:09
(Received via mailing list)
Yavor D. wrote:
>> Out of sheer curiosity, why don't you want to use a model?  Not sure if
>
> Some strange errors last night :(
>
> need some push
>
>
>
What are these "strange errors"?
Yavor D. (Guest)
on 2007-06-11 22:52
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 :(
>>
>> 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.
andreim (Guest)
on 2007-06-12 17:55
(Received via mailing list)
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. <removed_email_address@domain.invalid>
Yavor D. (Guest)
on 2007-06-13 01:49
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. <removed_email_address@domain.invalid>

Thanks :)
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!!!
Daniel G. (Guest)
on 2007-06-13 09:42
(Received via mailing list)
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...

(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) }
This topic is locked and can not be replied to.