如何在 ruby​​ 中使用 sinatra 提供生成的图像

发布于 2024-08-23 14:23:33 字数 215 浏览 4 评论 0原文

我编写了一个简单的 Sinatra 应用程序,它使用 rmagick 根据一些用户输入生成图像。图像以唯一的文件名保存在 ./public 目录中。 Sinatra 生成的 HTML 中使用唯一的文件名,以便每个用户都能获得正确的图像。脚本每天删除一次超过一小时的文件。这显然是一个可怕的黑客攻击,但我没有网络经验!

有没有办法在 sinatra 中提供 rmagick 图像而无需先将其保存到磁盘?

I wrote a simple Sinatra app that generate an image using rmagick from some user inputs. The image is saved in the ./public directory with a unique file name. The unique file name is used in the HTML generated by Sinatra so that each user gets the correct image. Once a day a script deletes files older than one hour. This is clearly a terrible hack but I have no web experience!

Is there any way to serve the rmagick image in sinatra without first saving it to disk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

林空鹿饮溪 2024-08-30 14:23:33

使用 Image#to_blob 方法将将内存中的图像转换为字符串:

get '/' do
  content_type 'image/png'
  img = Magick::Image.read('logo:')[0]
  img.format = 'png'
  img.to_blob
end

Use the Image#to_blob method to turn the in-memory image into a string:

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