Create paperclip attachment from rmagick image

人走茶凉 提交于 2019-11-30 18:56:12
jordinl

Let's see if that's what you need

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

Change YourModel with the model you are using...

You should force the extension on TempFile.new; in this case I pull the original image from S3 or some such, this is happening in the model of course:

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

In the later versions of Paperclip (mine is 5.0.0), you'll need to provide Paperclip's own Tempfile instance:

file = Paperclip::Tempfile.new(["processed", ".jpg"])
thumb.write(file.path)
result = YourModel.create(image: file)

This preserves the file extension at the end of the filename, so that it is recognized by Paperclip when it's uploaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!