Best practice is to cache the generated images and allow the web server to serve them.
Use a web server such as Apache or Nginx in front of your Rails application, and make sure that you write the image where the web server can serve it. Therefore, if your Rails route evaluates to /dynamic_images/3.png (which calls dynamic_images_controller action show with id = 3 and format = png), write this image to public/dynamic_images/3.png and use send_file in the controller to send it.
The next time this file is requested ( /dynamic_images/3.png ), the web server will happily serve it (cached), and the Rails application will never suffer.
For advanced needs like re-generating images and flushing controller code, take a look at paperclip gem.
source share