【发布时间】:2011-01-27 09:17:53
【问题描述】:
我正在为我们家的 Intranet 创建一个小型照片共享网站,并且我有一个上传功能,可以将原始大小的照片上传到数据库中。但是,我还想将照片保存为其他四种尺寸:W=1024、W=512、W=256 和 W=128,但只有小于原始尺寸的尺寸(例如,如果原始宽度为 511,则只生成256 和 128)。应始终生成宽度为 128 的图像(因为它是缩略图)。此外,调整大小应始终具有成比例的宽度和高度。我该如何实施? 我已经有了上传照片的代码:
pic.rb
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
<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 %>
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
<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 %>
谢谢
【问题讨论】:
标签: ruby-on-rails image-processing upload