[英]Saving image created with RMagick using Rails and CarrierWave
在我的Rails應用程序中,我有一個模塊可以拍攝多張圖像,並使用RMagick將它們“拼接”成一個新的單個圖像。 我能夠成功創建最終圖像,但是我無法將這個新圖像保存為模型的附件(使用CarrierWave) 。 拼接方法如下所示:
def generate_collage(params)
final_image = ImageList.new
# ... code that puts together the composite image ...
return final_image.append(true).to_blob { |attrs| attrs.format = 'JPEG' }
end
我的用戶模型安裝了一個上傳器:
class User < ActiveRecord::Base
mount_uploader :image, UserImageUploader
end
在ActiveRecord部分下的CarrierWave文檔中,它們顯示了如何分配新圖像,但它們假定文件已存在於某處。 在我的情況下,它在文件系統上還不存在,我正在輸出一個blob
... 有沒有辦法從該blob轉到為CarrierWave生成圖像上傳 ?
我想我試圖避免將這個圖像暫時保存到"#{Rails.root}/tmp/"
然后從那里讀取...似乎我可以刪除這一步並直接發送到CarrierWave,但是我不知道怎么樣! 可能嗎?
我現在正在做類似的事情。 這應該是可能的,但一個簡單的解決方法是將其保存到臨時文件:
temp_file = Tempfile.new([ 'temp', '.png' ])
image.write(temp_file.path)
user = User.new
user.avatar = temp_file
user.save
temp_file.close
temp_file.unlink
我希望通過遵循以下答案之一中的建議來嘗試改進它以完全消除文件系統依賴性: 如何處理file_as_string(由Prawn生成)以便Carrierwave接受它?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.