Caching generated images with Sinatra, MiniMagick & nginx
-
11-04-2022 - |
Question
I'm building a placeholder image site - similar enough to http://placekitten.com/
I've got a quick & dirty version working with a directory of big images, and a Sinatra route that randomises the image selection & resizes it to the right dimensions.
get '/p/:width' do
width = params[:width].to_i
path = Dir.glob('raw/top100/*.jpg').shuffle[0]
image = ImageResizer.new(path)
image.width= width
content_type("image/#{image.image_format}")
image.to_s
end
- this ImageResizer class https://gist.github.com/EvilScott/2431343#file-image_resizer-rb
This works but is a bit crappy - what's the best way to make it a little bit scalable before I link to it? I've heard about nginx caching but never done it before (& can't see how to fit it in with the images I'm generating) - or should I just pre-generate a bunch of different sizes when I add images to the server and cache those?
Thanks
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow