在 Ruby on Rails 中调整基本图像大小 [英] Basic image resizing in Ruby on Rails
问题描述
我正在为我们家的内联网创建一个小照片共享站点,我有一个上传功能,可以将原始大小的照片上传到数据库中.不过,我也想把照片保存成其他四种尺寸:W=1024、W=512、W=256和W=128,但只能保存比原始尺寸小的尺寸(比如如果原始宽度为511,则只生成256 和 128).应始终生成宽度为 128 的图像(因为它是缩略图).此外,调整大小应始终与宽度和高度成比例.我该如何实施?我已经有了上传照片的代码:
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:
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 %>
谢谢
推荐答案
只需使用 paperclip 或 href="http://github.com/technoweenie/attachment_fu" rel="noreferrer">attachment_fu
这篇关于在 Ruby on Rails 中调整基本图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!