将Rmagick与上传的文件一起使用

时间:2014-03-30 23:52:09

标签: ruby-on-rails imagemagick

从磁盘上存在的文件中读取映像时,

image.formatimage.filename可以正常工作。但是,在阅读内存中存在的上传文件时(我认为我正确地使用了该术语),我根本无法使用rmagick:

ap data['file'] #=> #<ActionDispatch::Http::UploadedFile:0x000000071f5d90 @tempfile=#<Tempfile:/tmp/RackMultipart20140331-42056-w8cqh6>, @original_filename="Screenshot from 2014-03-05 17:27:04.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile_image_attributes][file]\"; filename=\"Screenshot from 2014-03-05 17:27:04.png\"\r\nContent-Type: image/png\r\n">

image = Magick::Image::from_blob(data['file'].read) # this doesn't give an error

image.format # private method `format' called for [ PNG 988x766 988x766+0+0 DirectClass 8-bit 194kb]:Array ( works fine when image has been read from disk )
image.filename # undefined method, works fine when image has been read from disk

private method `format' called for [ PNG 988x766 988x766+0+0 DirectClass 8-bit 194kb]:Array

给出任何线索?

2 个答案:

答案 0 :(得分:3)

from_blob的文档看,返回值看起来像是一个数组。所以你可能不得不调用 image [0] .format image [0] .filename 而不是 image.format 图片.filename

答案 1 :(得分:2)

image = Magick::Image::from_blob(data['file'].read).first