If you heard of Cloudinary before, you probably already know how useful Cloudinary is with managing all your dynamically uploaded images, transforming these to their required dimensions, performing image optimization to ensure files are have the optimal quality and parameters, and delivering them through a fast CDN.
But what about all the static images you have in your web application? background images, buttons, icons – they too should be delivered through a Rails CDN, offloading their delivery from your servers and improvixng your website’s performance.
You can always do it yourself – setup your cloud environment, upload all these static images to your cloud storage, access them through a Rails CDN and make sure to update these images when they change.
Or – you can let Cloudinary do it. Automatically.
In this post we wanted to introduce a new Cloudinary feature. This feature simplifies and streamlines the process of uploading your static images to the cloud and delivering them through a Rails CDN.
If you haven’t done so already – upgrade to our latest Ruby GEM and you will enjoy this new feature with zero code change.
How is this done? First, upload all your Ruby-on-Rails applications’ static images to Cloudinary, using a single Rake command:
This Rake task finds all the images in all common public folders and in Rails Asset Pipeline’s image asset folders. Afterwards, it uploads all new and modified images to Cloudinary. Uploaded images and their versions are maintained using a local .cloudinary.static file. Make sure you commit this file to your source control.
Now that you’ve uploaded all your static images to Cloudinary, all you’ve left to do in order to deliver these through a CDN is to edit your cloudinary.yml file and set the following configuration parameters to ‘true’:
That’s it. No other code changes required. From now on, every image_tag call in your views would automatically check if the image was uploaded to the cloud (using .cloudinary.static) and if so, would generate a remote Cloudinary CDN URL.
UPDATE [November 2021]: Ruby Sass was deprecated in 2019. The consensus is to use SassC instead. Note thatas opposed to Sass, optional named arguments are not supported in SassC, use a hash map instead.
For example: Sass: cloudinary-url(“sample”, $quality: “auto”, $fetch_format: “auto”);
One of Cloudinary’s major strengths is in its powerful image transformations. In most cases, you’ll want your static images displayed as-is. But occasionally, applying transformations on your static images can be very useful. For example, displaying a set of icons in multiple dimensions. Another example is when you want to support Responsive Layout and Images. In this case, adjusting the size of all static images according to your visitors’ device resolution might greatly improve your visitors’ experience (e.g., resize all images to 50% their original size).
With Cloudinary you can apply various transformations on your static images, with ease.
In the following example, we take a 100×100 static logo.png image and resize it on-the-fly to a 50×50 image with rounded corners of 10 pixels radius. The following image_tag:
Changing the look & feel and dimensions of images in your site based on the user’s device can be done using CSS instead of changing your code. This can be be made even simpler if you are using Sass in your Rails project. Simply use the ‘cloudinary-url‘ template method. It will convert image references to remote Cloudinary CDN URLs and can also receive all supported transformation parameters.
For example, the following Sass line would generate the same 50×50 scaled logo with rounded corners, via Sass:
To summarize – if you are using Cloudinary for managing and transforming your uploaded images, you should definitely follow the simple instructions above to immediately experience the performance boost gained by delivering all your static assets through Cloudinary. Don’t have a Cloudinary account yet? Click here to setup a free account in seconds.
Few possess the expertise of Jon Sneyers, recognized as one of the foremost experts at the intersection of image compression and standards development. As a computer scientist, Jon has been a guiding force behind the creation and enhancement of image compression standards, driving the industry toward more efficient, versatile, and…