RMagick 直接提交表格 图片来自 Sinatra

发布于 2024-09-16 01:31:10 字数 896 浏览 3 评论 0原文

我正在尝试使用 SinatraRMagick 做一些非常简单的事情。

  1. 通过简单的形式拍摄图像 文件上传
  2. 使用RMagick调整大小
  3. 然后存储到数据库中 持久性(无关)

但是在经历了 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.

  1. Take a image, through a simple form
    file upload
  2. Use RMagick to resize it
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅哥哥的热头脑 2024-09-23 01:31:10

您需要从临时文件中获取路径并将其传递给 Magick::Image 的 read

这是一个例子:

post "/upload-photo" do
  image = Magick::Image.read(params[:image][:tempfile].path)[0]
  image.crop_resized! 100, 100, Magick::CenterGravity
  store_image_data image.to_blob

  redirect "/done"
end

You need to get the path from the tempfile and pass that to Magick::Image’s read.

Here’s an example:

post "/upload-photo" do
  image = Magick::Image.read(params[:image][:tempfile].path)[0]
  image.crop_resized! 100, 100, Magick::CenterGravity
  store_image_data image.to_blob

  redirect "/done"
end
橘虞初梦 2024-09-23 01:31:10

或者您可以直接从 ActionDispatch::Http::UploadedFile 读取,如下所示:

image = Magick::Image.from_blob(params[:image].read)

Or you can read straight from the ActionDispatch::Http::UploadedFile like so:

image = Magick::Image.from_blob(params[:image].read)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文