从rmagick图像创建回形针附件

gut*_*uts 9 ruby ruby-on-rails rmagick paperclip

我有一个问题是找到一种方法来保存使用RMagick在回形针附件中创建的图像.

imageList = Magick::ImageList.new
imageList.new("images/apple.gif", "images/overlay.png")
...
picture = imageList.flatten_images
Run Code Online (Sandbox Code Playgroud)

我在一个有附件的模型中

has_attached_file :picture, :url => ..., :path => ...

我只想将imageList.flatten_images返回的图像保存为我模型的图片.

有谁知道如何轻松地做到这一点?

谢谢

jor*_*inl 12

让我们看看这是否是你需要的

picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)
Run Code Online (Sandbox Code Playgroud)

YourModel根据您使用的型号进行更改......


小智 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
Run Code Online (Sandbox Code Playgroud)