从Magick :: Image创建一个ruby File对象

| 我正在尝试使用rmagick和rest-client将加水印的图像发布到url。生成合成图像时,我将其保存(.write),并使用File.new读回,然后发布该File对象。不过,理想情况下,我想绕过写入操作,因为我再也不需要这张照片了。有什么方法可以将Magick :: Image对象转换为File对象,以便我可以将其发布到rest-client吗?
require \'rmagick\'
require \'rest-client\'

photo = Magick::Image.read(\'myphoto.jpg\').first
water_mark = Magick::Image.read(\'watermark.png\').first

result = photo.composite(water_mark, 0, 0, Magick::OverCompositeOp)
result.write(\'result.jpg\')

file = File.new(\'result.jpg\', \'rb\')
RestClient.post(\"http://example.com\", :source => file)
    
已邀请:
        我终于使用StringIO和Koala gem(Facebook API的红宝石包装器)弄清楚了。代码如下:
access_token = \"asdfasdfasdfasdf\"
graph = Koala::Facebook::API.new(access_token)
photo = Magick::Image.read(\"my_photo.jpg\").first
watermark = Magick::Image.read(\"watermark.png\").first
watermarked = photo.composite(watermark, 5, 5, Magick::OverCompositeOp)
photo_graph_id = StringIO.open(watermarked.to_blob) do |strio|
  response = graph.put_picture(strio, \"image/jpeg\", { \"message\" => \"hi\" })
  response[\'id\']
end
关键是在Magick :: Image上调用to_blob,然后从该字符串创建StringIO。当前版本的Koala gem在StringIO上存在故障,但我已将其修复在fork中并提交了pull请求: https://github.com/arsduo/koala/pull/122     

要回复问题请先登录注册