Cloudinary Blog

Ruby on Rails image uploads with CarrierWave and Cloudinary

by Nadav Soferman

When we set to develop Cloudinary’s Rails integration Gem, it was obvious to us that we’ll base it on CarrierWave. Here’s why.

Photos are a major part of your website. Your eCommerce solution will have multiple snapshots uploaded for each product. Your users might want to upload their photo to be used as their personal profile photo. What’s entailed when developing such a photo management pipeline, end-to-end?


  • You’ll need an HTML file upload form.
  • The server will need to manage the reception and processing of the uploaded image files.
  • Uploaded images should be stored on a safe storage with access for multiple application servers.
  • Model entities should keep references to uploaded images.
  • Uploaded images will need to be resized and cropped into different dimensions matching the graphics design of your web site.
  • The server will need to find and deliver the resized images to visitors of your site when displaying a page with the relevant model entity (e.g., display a thumbnail of the profile picture in a user profile page, etc.).
  • Allow overriding uploaded images with new ones when needed.
Cloudinary allows you to overcome this complexity in its entirety, but how does it work?

Over the years, we’ve had the pleasure of using some of RoR’s many excellent file upload solutions: CarrierWave, Paperclip, Dragonfly, attachment_fu and others. All-in-all, CarrierWave often proved a better fit for our needs:

  • Simple Model entity integration. Adding a single string ‘image’ attribute for referencing the uploaded image.
  • "Magic" model methods for uploading and remotely fetching images.
  • HTML file upload integration using a standard file tag and another hidden tag for maintaining the already uploaded "cached" version.
  • Straight-forward interface for creating derived image versions with different dimensions and formats. Image processing tools are nicely hidden behind the scenes.
  • Model methods for getting the public URLs of the images and their resized versions for HTML embedding.
  • Many others - see CarrierWave documentation page.
What we liked most is the fact the CarrierWave is very modular. You can easily switch your storage engine between a local file system, Cloud-based AWS S3, and more. You can switch the image processing module between RMagick, MiniMagick and other tools. You can also use local file system in your dev env and switch to S3 storage in the production system.
 
When we developed Cloudinary and decided to provide a Ruby GEM for simple Rails integration, it was obvious that we’ll want to build on CarrierWave. Our users can still enjoy all benefits of CarrierWave mentioned above, but also enjoy the additional benefits that Cloudinary provides:
  • The storage engine is Cloudinary. All images uploaded through CarrierWave model methods are directly uploaded and stored in the cloud. 
  • All resized versions and image transformations are done in the cloud by Cloudinary: 
    • No need to install any image processing tools or Ruby GEMs. 
    • You can create the resized versions eagerly while uploading or lazily when users accesses the actual images. Save processing time and storage.
    • Change your desired image versions at any time and Cloudinary will just create them on the fly, no need to batch update all your images when the graphics design of your site changes.
  • All public image URLs returned by CarrierWave are Cloudinary URLs. This means they are automatically delivered through a global CDN with smart caching. Seamlessly enhancing the performance of your web application.
Some code samples:

class PictureUploader < CarrierWave::Uploader::Base

  include Cloudinary::CarrierWave
  
  version :standard do
    process :resize_to_fill => [100, 150, :north]
  end
  
  version :thumbnail do
    process :resize_to_fit => [50, 50]
  end     
    
end
class Post < ActiveRecord::Base
  ...
  mount_uploader :picture, PictureUploader
  ...
end
= form_for(:post) do |post_form|
  = post_form.hidden_field(:picture_cache)
  = post_form.file_field(:picture)
= image_tag(post.picture_url, :alt => post.short_name)

= image_tag(post.picture_url(:thumbnail), :width => 50, :height => 50)

We believe that for Ruby on Rails developers, the combination of Cloudinary with its CarrierWave-based gem, delivers a complete image management solution, with excellent model binding.

More details about about our CarrierWave plugin are available in our documentation: http://cloudinary.com/documentation/rails_integration#carrierwave_upload

What do you think about our solution? any suggestions or improvement ideas?

UPDATE: We've published another post about additional advanced image transformations in the cloud with CarrierWave & Cloudinary.

Recent Blog Posts

Taking the labor out of baby books

by Nicole Amsler
How Baby's Firsts save development time with Cloudinary
Cloudinary helps Baby’s Firsts App deliver images quickly and preserve consistency from app to print. Baby’s Firsts is a free iPhone app that helps parents collect photos, instantly share photos via Facebook, Twitter and Flickr, and produce photo albums of their baby’s first year. Using more than 300 creative, developmentally timed reminders to capture key moments and milestones, the easy-to-navigate app enables parents to store photos in the cloud and create customized photo pages that are then transformed into heirloom-quality, printed baby books. 
Read more

Lossy compression for optimizing animated GIFs

by Meir Feinberg
How to optimize animated GIFs with lossy compression

Animated GIFs keep getting more and more popular, but they are generally very big files with slow loading times and high bandwidth costs, while the format itself is quite old and not optimized for modern video clips. As developers, you need to allow users to upload their animated GIF files, but you also need to deliver them optimized, which can be a complex, time consuming process.

Read more
Building and scaling a cloud service for developers

Last month I was invited to speak at Daho.am, Munich's developers conference. This conference was organized by Stylight, a very successful fashion technology startup.  Stylight signed up for a free Cloudinary account about 3 years ago and similarly to Cloudinary back then, Stylight were quite a young startup. Since then both companies have grown impressively together and Stylight are now a premium customer of Cloudinary, managing hundreds of millions of images.

Read more
10 Startups managing images in the cloud, Part 6
Cloudinary’s customers can range in size, be found in a variety of verticals and boast multiple use cases. Large and small, our customers are our backbone and we like to support them by highlighting their stories on our blog.
 
Check out the ten featured startups below who are Cloudinary’s customers and utilizing the service as their image management system of choice.
Read more
Automatic visual image enhancement for web apps

Various factors can have an effect on the visual quality of photos captured by a wide variety of digital cameras. Technical limitations of cameras, coupled with changing conditions in which users take photos, results in a wide range of visual quality. Camera-related limitations arise from a combination of poor optics, noisy sensors, and the modest capabilities of mobile camera phones that are used to take photos in conditions that range from bright daylight to indoor scenes with incandescent light or even dark night scenes.

Read more