开发者

How to serve generated images with sinatra in ruby

开发者 https://www.devze.com 2022-12-21 23:18 出处:网络
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

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 th开发者_运维问答e 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号