在 Ruby on Rails 中调整基本图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2442248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic image resizing in Ruby on Rails
提问by Sebtm
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:
我正在为我们家的内联网创建一个小照片共享站点,我有一个上传功能,可以将原始大小的照片上传到数据库中。但是,我也想把照片保存成其他四种尺寸:W=1024、W=512、W=256和W=128,但只能保存比原始尺寸小的尺寸(例如如果原始宽度为511,则只生成256 和 128)。应始终生成宽度为 128 的图像(因为它是缩略图)。此外,调整大小应始终与宽度和高度成比例。我该如何实施?我已经有了这个代码来上传照片:
pic.rb <-- model
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 <-- view
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 %>
Thanks
谢谢
采纳答案by denisjacquemin
Simply use paperclipor attachment_fu
只需使用回形针或attachment_fu
回答by Sebtm
You can resize with RMagick gem.
您可以使用 RMagick gem 调整大小。
This is just a example that you can adapt:
这只是一个您可以适应的示例:
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")
}
More Info here: http://www.imagemagick.org/script/api.php#ruby
回答by kernification
Knowing this is an old one, but here is a very nice railscast according to this topic: http://railscasts.com/episodes/253-carrierwave-file-uploadsIt is using rmagic and carrierwave.
知道这是一个旧的,但这里有一个非常好的 railscast 根据这个主题:http://railscasts.com/episodes/253-carrierwave-file-uploads 它使用 rmagic 和carrierwave。
回答by suresh.g
I tried following way. Its working fine. I hope it will help to some one.
我试过以下方式。它的工作正常。我希望它会帮助某人。
1.add following gem in Gemfile:
1.在Gemfile中添加以下gem:
gem "ImageResize", "~> 0.0.5"
2.run bundle
2.运行包
3.use this in controller function:
3.在控制器功能中使用它:
require 'rubygems'
require 'ImageResize'
#input_image_filename, output_image_filename, max_width, max_height
Image.resize('big.jpg', 'small.jpg', 40, 40)
回答by Vaibhav Maheshwari
There is a gem called "Refile". It is great. Go ahead and check this tutorial on how to use it. https://gorails.com/episodes/file-uploads-with-refilehere is how to do it.
有一种叫做“Refile”的宝石。太好了。继续并查看本教程以了解如何使用它。 https://gorails.com/episodes/file-uploads-with-refile这里是如何做到的。
Add this to your gem file
将此添加到您的 gem 文件中
gem 'refile', '~> 0.4.2', require: ["refile/rails", "refile/image_processing"]
Create a table field using rails migration in your table as image_id of string type.Now comes how to insert into that field and display the image.
在表中使用 rails 迁移创建一个表字段作为字符串类型的 image_id。现在介绍如何插入该字段并显示图像。
Use this in your upload form, largely form_for do |f|
在你的上传表单中使用它,主要是 form_for do |f|
<%= f.attachment_field :image %>
if you are using rails 4 make sure you pass rails strong parameters.
如果您使用的是 rails 4,请确保您传递了 rails 强参数。
Put this in your model.rb file where you are storing the image (below class modelname)
把它放在你存储图像的 model.rb 文件中(在类模型名下面)
attachment :image
displaying the image is simple.
显示图像很简单。
<%= image_tag attachment_url(current_user,:image, :fill, 200, 170, format: "jpg") %>

