RMagick 直接提交表格 图片来自 Sinatra
我正在尝试使用 Sinatra 和 RMagick 做一些非常简单的事情。
- 通过简单的形式拍摄图像 文件上传
- 使用RMagick调整大小
- 然后存储到数据库中 持久性(无关)
但是在经历了 RDocs 和无休止的头撞测试之后 我似乎无法干净地将表单图像获取到 RMagick 对象。
这是目前对我来说可怕的事情:
def image_resize(img_data)
filecount = rand
writer = File.new("/tmp/#{filecount}.jpg", "w")
writer.puts(img_data)
writer.close
resized_image = Magick::ImageList.new("/tmp/#{filecount}.jpg").first
resized_image.crop_resized!(100,100, Magick::NorthGravity)
resized.format = 'jpeg'
resized_image.to_blob
end
#call the method with my form image data
image_resize(params[:image][:tempfile].read)
那么我该如何做明显正确的事情,只需将我的表单图像数据直接粘贴到 RMagick 对象中,而无需写入和读取磁盘。
我尝试过各种阅读 Magick::Image 和 ImageLists 的方法,但只收到大量错误。
感谢您的任何指导
-1.2。
I am trying to do something quite simple using Sinatra and RMagick.
- Take a image, through a simple form
file upload - Use RMagick to resize it
- Then store it in a database for
persistence (irrelevant)
But after going through the RDocs and endless head banging testing
I can't seem to get the form image to a RMagick object cleanly.
This is the horrible thing that is currently working for me:
def image_resize(img_data)
filecount = rand
writer = File.new("/tmp/#{filecount}.jpg", "w")
writer.puts(img_data)
writer.close
resized_image = Magick::ImageList.new("/tmp/#{filecount}.jpg").first
resized_image.crop_resized!(100,100, Magick::NorthGravity)
resized.format = 'jpeg'
resized_image.to_blob
end
#call the method with my form image data
image_resize(params[:image][:tempfile].read)
So how do I do the obvious right thing and just stick my form image data straight into a RMagick object without having to write and read the disk.
I have tried various ways of reading in Magick::Image and ImageLists but have only got an abundance of errors barfed at me.
Thanks for any kind of direction
-1.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要从临时文件中获取路径并将其传递给 Magick::Image 的
read
。这是一个例子:
You need to get the path from the tempfile and pass that to Magick::Image’s
read
.Here’s an example:
或者您可以直接从 ActionDispatch::Http::UploadedFile 读取,如下所示:
Or you can read straight from the ActionDispatch::Http::UploadedFile like so: