title: Basic image resizing in Ruby on Rails
body: <p>I'm creating a little photo sharing site for our home's intranet, and I have an upload feature, which uploads the photo at original size into the database. However, I also want to save the photo in four other sizes: W=1024, W=512, W=256 and W=128, but only the sizes smaller than the original size (e.g. if the original width is 511, only generate 256 and 128). The image with a width of 128 should always be generated (because it's a thumbnail). Also, the resization should always be with a proportional width and height. How can I implement this?
  I already have this code to upload the photo:</p>
  
  <h1>pic.rb &lt;-- model</h1>
  
  <pre><code>def image_file=(input_data)
    self.filename     = input_data.original_filename
    self.content_type = input_data.content_type.chomp
    self.binary_data  = input_data.read
    # here it should generate the smaller sizes
    #+and save them to self.binary_data_1024, etc...
  end
  </code></pre>
  
  <h1>new.rb &lt;-- view</h1>
  
  <pre><code>&lt;h1&gt;New pic&lt;/h1&gt;
  
  &lt;% form_for(@pic, :html =&gt; {:multipart =&gt; true}) do |f| %&gt;
    &lt;%= f.error_messages %&gt;
  
    &lt;p&gt;
      &lt;%= f.label :title %&gt;&lt;br /&gt;
      &lt;%= f.text_field :title %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :description %&gt;&lt;br /&gt;
      &lt;%= f.text_field :description %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :image_file %&gt;&lt;br /&gt;
      &lt;%= f.file_field :image_file %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.submit 'Create' %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  
  &lt;%= link_to 'Back', pics_path %&gt;
  </code></pre>
  
  <p>Thanks</p>