14 upload ruby-on-rails image-processing
我正在为我们家的内联网创建一个小照片共享网站,我有一个上传功能,它将原始大小的照片上传到数据库中.但是,我还想将照片保存为其他四种尺寸:W = 1024,W = 512,W = 256和W = 128,但只有小于原始尺寸的尺寸(例如,如果原始宽度为511,则仅生成256和128).应始终生成宽度为128的图像(因为它是缩略图).此外,重新调整应始终具有成比例的宽度和高度.我该如何实现呢?我已经有了上传照片的代码:
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
Run Code Online (Sandbox Code Playgroud)
<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 %>
Run Code Online (Sandbox Code Playgroud)
谢谢
Seb*_*btm 26
您可以使用RMagick gem调整大小.
这只是一个你可以适应的例子:
require 'RMagick'
f = File.new( File.join(save_path, self.filename), "wb" )
f.write form_file.read #remeber to set :html=>{:multipart => true} in form
f.close
image = Magick::Image.read(self.filename).first
image.change_geometry!("640x480") { |cols, rows, img|
newimg = img.resize(cols, rows)
newimg.write("newfilename.jpg")
}
Run Code Online (Sandbox Code Playgroud)
更多信息:http://www.imagemagick.org/script/api.php#ruby
| 归档时间: |
|
| 查看次数: |
29920 次 |
| 最近记录: |