从rmagick图像创建回形针附件
我有一个问题是找到一种方法来保存使用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返回的图像保存为我模型的图片。
有谁知道如何轻松地做到这一点?
谢谢
让我们看看这是否是你需要的
picture = imageList.flatten_images file = Tempfile.new('my_picture.jpg') picture.write(file.path) YourModel.create(:picture => file, ...)
使用您正在使用的模型更改YourModel
…
你应该在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
在Paperclip的后续版本中(我的是5.0.0
),您需要提供Paperclip自己的Tempfile
实例:
file = Paperclip::Tempfile.new(["processed", ".jpg"]) thumb.write(file.path) result = YourModel.create(image: file)
这会将文件扩展名保留在文件名的末尾,以便Paperclip在上传时识别它。