【问题标题】:Saving image created with RMagick using Rails and CarrierWave使用 Rails 和 CarrierWave 保存 RMagick 创建的图像
【发布时间】:2017-01-09 19:36:37
【问题描述】:

在我的 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

the ActiveRecord section 下的 CarrierWave 文档中,他们展示了如何分配新图像,但他们假设文件已经存在于某处。在我的情况下,它在文件系统上尚不存在,我正在输出 blob...有没有办法从该 blob 生成 CarrierWave 的图像上传

我想我想避免将此图像暂时保存到 "#{Rails.root}/tmp/" 然后从那里读取它...似乎我可以删掉这一步并以某种方式直接发送到 CarrierWave,但我不知道如何!可能吗?

【问题讨论】:

    标签: ruby-on-rails ruby image-processing carrierwave image-uploading


    【解决方案1】:

    我现在正在做类似的事情。这应该是可能的,但一个简单的解决方法是将其保存到临时文件中:

    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
    

    我希望尝试改进它以完全删除文件系统依赖项,遵循以下答案之一中的建议:How to handle a file_as_string (generated by Prawn) so that it is accepted by Carrierwave?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2018-07-11
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多