我有一个问题,就是找到一种方法来将使用RMagick创建的图像保存在回形针附件中。
imageList = Magick::ImageList.new
imageList.new("images/apple.gif", "images/overlay.png")
...
picture = imageList.flatten_images
我在一个有附件的模型
has_attached_file :picture,
:url => ...,
:path => ...
我只想将imageList.flatten_images返回的图像保存为我模型的图片。
有人知道如何轻松地做到这一点吗?
感谢
答案 0 :(得分:12)
让我们看看这是否是您需要的
picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)
使用您正在使用的模型更改YourModel
...
答案 1 :(得分:5)
你应该在TempFile.new上强制扩展;在这种情况下,我从S3中拉出原始图像或其他类似的东西,这当然发生在模型中:
orig_img = Magick::ImageList.new(self.photo.url(:original))
#process image here
# Force extension with array form:
file = Tempfile.new(['processed','.jpg'])
orig_img.write(file.path)
self.photo = file
self.save
答案 2 :(得分:0)
在Paperclip的后续版本中(我的是5.0.0
),您需要提供Paperclip自己的Tempfile
实例:
file = Paperclip::Tempfile.new(["processed", ".jpg"])
thumb.write(file.path)
result = YourModel.create(image: file)
这会保留文件名末尾的文件扩展名,以便Paperclip在上传时识别它。