Basic image resizing in Ruby on Rails
- by Koning Baard XIV
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