Yavor Dimitrov
2007-Jun-10  22:45 UTC
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:
-----
<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>
-----
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Cliff Rowley
2007-Jun-11  09:31 UTC
Re: Ruby/Rails - RMagick, Upload and Resize without a model
Yavor Dimitrov 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yavor Dimitrov
2007-Jun-11  11:05 UTC
Re: Ruby/Rails - RMagick, Upload and Resize without a model
Cliff Rowley wrote:> Yavor Dimitrov 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. > > CliffHmmm 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cliff Rowley
2007-Jun-11  15:08 UTC
Re: Ruby/Rails - RMagick, Upload and Resize without a model
Yavor Dimitrov wrote:> Cliff Rowley wrote: > >> Yavor Dimitrov 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 > > >What are these "strange errors"? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yavor Dimitrov
2007-Jun-11  18:52 UTC
Re: Ruby/Rails - RMagick, Upload and Resize without a model
Cliff Rowley wrote:> Yavor Dimitrov 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Dimitrov <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi everyone! > I have a difficult time with Ruby/Railsand RMagick upload and resizing. > > In user_controller of myRailsapplication 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 actualfileon > 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> > <formaction="/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> > > ----- > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yavor Dimitrov
2007-Jun-12  21:49 UTC
Re: Ruby/Rails - RMagick, Upload and Resize without a model
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 Dimitrov <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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!!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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) }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---