Basic image resizing in Ruby on Rails

Posted by Koning Baard XIV on Stack Overflow See other posts from Stack Overflow or by Koning Baard XIV
Published on 2010-03-14T13:20:12Z Indexed on 2010/03/14 13:25 UTC
Read the original article Hit count: 318

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). How can I implement this? I already have this code to upload the photo:

pic.rb <-- model

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

new.rb <-- view

<h1>New pic</h1>

<% form_for(@pic, :html => {:multipart => true}) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </p>
  <p>
    <%= f.label :image_file %><br />
    <%= f.file_field :image_file %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', pics_path %>

Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about upload