【问题标题】:How to use rmagick in Rails?如何在 Rails 中使用 rmagick?
【发布时间】:2016-02-16 12:54:30
【问题描述】:
我是 Ruby on Rails 的新手。我想使用 rmagick 在我的 rails 项目中进行图像处理。但我很困惑如何做到这一点。我已经在控制台上练习了“rmagick”。但现在我想使用 rmagick 在我的 rails 项目中操作上传的图像。帮我解决这个问题??
【问题讨论】:
标签:
ruby
ruby-on-rails-4
rmagick
【解决方案1】:
除非您打算做一些非常奇特的事情,否则您可能最好使用 Carrierwave 或 PaperClip gem。
基本上这两个在后台使用 imagemagic,但已经自动化了常见的操作,如图像上传、调整大小和头像用户图片。
与 rmagic gem 不同的是,它们附带了很好的入门文档。
给你。
- 回形针https://github.com/thoughtbot/paperclip
- 载波https://github.com/carrierwaveuploader/carrierwave
也就是说如果你想在这里使用 Rmagic 是一个图像调整控制器方法的例子。
def resize_images
require 'rubygems'
require 'RMagick'
include Magick
require "open-uri"
file_url = open('URL to image')
save_path = "/"
f = File.new( File.join(save_path, file_url), "wb")
f.write file_url.read
f.close
image = Magick::Image.read(file_url).first
image.change_geometry!("1500x150") { |cols, rows, img|
newimg = img.resize(cols, rows)
newimg.write("newfilename.jpg")
}
end