Code Climate Test Coverage Build Status


Generate images on the fly with caching by giving simple parameters to URL's.


First add RailsImager to your gemfile and bundle it:

gem 'rails_imager'

The mount RailsImager in your "routes.rb":

YourApp::Application.routes.draw do
  mount RailsImager::Engine => "/rails_imager"

Add this to the ApplicationHelper in order to gain access to the rails_imager_p-helper method for generating URL's easy.

module ApplicationHelper
  include RailsImager::ImagesHelper

In case you use RailsImager in an engine, you might want to alter the path through an initializer file (config/initializers/rails_imager.rb):

RailsImager.config.path = "/my_engine/rails_imager"


If you use Capistrano to deploy, you probably symlink the "public/system" directory, in order to share uploaded images between releases. This screws with RailsImager's security only to serve images from the "public"-folder.

You can solve this by adding the following initializer in "config/initializers/rails_imager.rb":

# Add public/system to path, because it is symlinked through Capistrano on deployed servers.
RailsImager.config.allowed_paths << File.realpath("#{Rails.public_path.to_s}/system")


Now you can use RailsImager to convert any image located in the public-folder like so:

rails_imager_p helper method

Usage with a static image in the public folder:

<%= image_tag rails_imager_p("/picture.jpg", :smartsize => 200, :rounded_corners => 10, :border => 1, :border_color => "black") %>

Usage with a Paperclip attachment on a model:

<%= image_tag rails_imager_p(user.picture, :smartsize => 200) %>

Rounded corners

Makes the corners of an image round.


Smart image sizing

Resize the longest side of an image to have a given size:


Normal sizing

Resize the given size but keep the aspect of the image:


Max sizing

Sets the maximum size of an image but keeps the aspect:



Give the image a border of a certain size and/or with a certain color.



RailsImager performs various forms of caching, to avoid having to do too much work in regards to the actual resizing.

It caches the results as temp-files, so it never has to render the same image with the same parameters again.

It sends out the appropriate headers to the client, so the client doesn't read the same image again and again.


This project uses MIT-LICENSE.