虽然paperclip gem通常用于轻松上传文件,但我在服务器端也有类似的要求。我在服务器上生成了一个图像,并希望按照回形针选项中的配置将其保存为各种样式。
我已经生成了迁移,我的模型如下所示:
attr_accessible :genimg
has_attached_file :genimg
styles: { mini: '48x48>', small: '100x100>',
product: '240x240>', large: '600x600>' },
default_style: :product
url: '/images/:basename.:extension',
path: ':rails_root/app/assets/images/:basename.:extension'
convert_options: { all: '-strip -auto-orient' }
有没有办法可以使用回形针以各种格式保存我的本地图像。我怎么能这样做?如果那是不可能的,那么另一种可能的尝试方法是什么?
答案 0 :(得分:0)
class YourController < ActionController::Base
def your_action
@image = Image.find 1 # (might be other way how you get the asset)
file_path = '/path/to/your/file'
file = File.open(file_path, 'r')
@image.asset = file
@image.save
end
end
在控制台中你可能会这样做:
Image.new(:data => File.new(path_to_your_file, "r"))