jQuery integration
Overview
Cloudinary is a cloud-based service that provides an end-to-end image management solution including uploads, storage, manipulations, optimizations and delivery.
With Cloudinary you can easily upload images to the cloud, automatically perform smart image manipulations without installing any complex software. All your images are then seamlessly delivered through a fast CDN, optimized and using industry best practices. Cloudinary offers comprehensive APIs and administration capabilities and is easy to integrate with new and existing web and mobile applications.
Quick example
Take a look at the following Cloudinary URL that generates the image below:
Simply accessing the above URL told Cloudinary to transform an uploaded image, create a 150x150px thumbnail using face detection based cropping, round the image's corners, add a sepia effect, convert it to a transparent PNG format, add a watermark layer, rotate the image by 10 degrees and ultimately delivered the resulting image through a fast CDN using smart caching techniques.
jQuery Plugin Features
Cloudinary provides a jQuery plugin (open source) for further simplifying the integration:
- Build URLs for image transformation & manipulation.
- Javascript helper methods for embedding and transforming images.
- Direct image uploading from the browser.
Note: The Javascript library has undergone an extensive redesign.
Up until now, the library was deeply coupled with jQuery and the Blueimp jQuery File Upload plugin. In order to cater for developers who do not wish to use jQuery, the library is now available in three different flavors:
- Core JavaScript Library: The core Cloudinary JavaScript library which does not depend on jQuery.
- jQuery Plugin: If you are using jQuery, you can take advantage of the Cloudinary jQuery plugin which includes all the functionality of the Core JavaScript Library.
- jQuery File Upload: The Cloudinary jQuery File Upload library extends the Cloudinary jQuery plugin that utilizes the Blueimp jQuery File Upload library. This library includes all the functionality of the Core JavaScript Library and the jQuery plugin.
The redesigned jQuery File Upload library (the 3rd option above) is fully backwards compatible with previous versions of the jQuery plugin. The documentation on this page is still relevant and will be updated in the near future to reflect the additional functionality of the library.
jQuery - Getting started guide
1Installation
Cloudinary's Javascript integration library is available as an open-source jQuery plugin.
You can download jquery.cloudinary.js from the js folder of the GitHub project. In your HTML pages, include jquery.cloudinary.js after including the jQuery library.
<script src='jquery.min.js' type='text/javascript'></script> <script src='jquery.cloudinary.js' type='text/javascript'></script>
If you want to perform direct uploading from the browser, download the following files from the js folder of the GitHub project as well. These files belong to the jQuery File Upload plugin.
jquery.ui.widget.js jquery.iframe-transport.js jquery.fileupload.js
Include all required jQuery files in your HTML page:
<script src='jquery.min.js' type='text/javascript'></script> <script src='jquery.ui.widget.js' type='text/javascript'></script> <script src='jquery.iframe-transport.js' type='text/javascript'></script> <script src='jquery.fileupload.js' type='text/javascript'></script> <script src='jquery.cloudinary.js' type='text/javascript'></script>
2Configuration
Your cloud_name account parameter is required to build image URLs. The public api_key is needed to perform direct uploading to Cloudinary (e.g., image uploads). The private api_secret parameter should not be included in your client-side HTML or Javascript code. See API, URLs and access identifiers for more details.
Setting the configuration parameters can be done either programmatically in each call to a Cloudinary method or globally using the $.cloudinary.config method.
You can find your configuration parameters in the dashboard of our Management Console.
Here's an example of setting configuration parameters globally in your Javascript code:
$.cloudinary.config({ cloud_name: 'sample', api_key: '874837483274837'})
3Upload images
You can upload images and any other files directly from the browser to Cloudinary. Uploading is done over HTTPS using a secure protocol based on the api_key and api_secret parameters of your account.
However, Cloudinary requires upload requests to be signed with your private api_secret, which should not be exposed in public web pages. Therefore, a signature needs to be generated in your server-side application for authorizing direct uploading.
You can use Cloudinary's helper methods for generating the signature in various server frameworks: Ruby on Rails, PHP, Django and Node.js.
The following file input tag will be automatically converted by Cloudinary's jQuery plugin to perform direct uploading to the cloud:
<input name="file" type="file" class="cloudinary-fileupload" data-cloudinary-field="image_id" data-form-data=" ... html-escaped JSON data ... " ></input>
The unescaped JSON content of data-form-data includes all upload parameters and the signature for authorizing upload. Here's an example:
{ "timestamp": 1345719094, "callback": "https://www.example.com/cloudinary_cors.html", "signature": "7ac8c757e940d95f95495aa0f1cba89ef1a8aa7a", "api_key": "1234567890" }
Users can upload one or more images or non-image files. Alternatively, you can specify a remote HTTP URL of images. Each image uploaded to Cloudinary is assigned a unique Public ID and is available for immediate delivery and transformation. The upload method returns a hash with content similar to that shown in the following example:
{ url: 'http://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg', secure_url: 'https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg', public_id: 'sample', version: '1312461204', format: 'jpg', width: 864, height: 564, bytes: 120253 }
As you can see in the following form data example, with a single call you can define your own Public ID, apply an incoming image transformation before storing the image in the cloud, generate derived images eagerly and assign tags to uploaded images:
{ "api_key": "1234567890", "callback": "https://www.example.com/cloudinary_cors.html", "eager": "c_thumb,g_face,h_200,w_200|c_fit,f_png,h_150,w_100", "public_id": "sample_id", "signature": "f5caf4b413d9f9a0008ba4dd42ae04eabcdb9f74", "timestamp": 1372419182, "transformation": "c_limit,h_2000,w_1200", "tags": "special,for_homepage" }
Many more upload options are detailed here: jQuery image upload.
Cloudinary supports uploading videos as well. See Upload videos documentation page for more details.
4Display and manipulate images
Adding images to your Rails view is quite straightforward. Just use the $.cloudinary.image helper method. This method generates the full image resource URL based on the given parameters and creates an IMG tag you can add to to your HTML code:
For example, displaying the uploaded image with the sample public ID, while providing an alternate text:
Now, let's say that you wish to display a small thumbnail of this uploaded image. Simply add the transformation instructions to your call. For example, displaying the 'sample' image transformed to fill a 100x150 rectangle:
This is equivalent to:
<img width="100" height="150" src="http://res.cloudinary.com/demo/image/upload/c_fill,h_150,w_100/sample.jpg"/>
Using simple parameters you can perform powerful image manipulations. The jQuery plugin builds Cloudinary URLs that you can embed in your web and mobile views for dynamically transforming your uploaded images in the cloud and delivering the results through a fast CDN with advanced caching.
You can easily convert image formats, resize your images, perform face detection based cropping, apply effects and filters, append textual layers or watermarks and more.
The following command, for example, embeds a JPG thumbnail of a profile photo fetched from Facebook in real-time, crops it to a circle, applies a sepia effect and delivers it optimized through a CDN:
$.cloudinary.facebook_profile_image("billclinton.jpg", { width: 90, height: 98, crop: 'fill', gravity: 'face', radius: 'max', effect: 'sepia' });
For a full list of supported transformations and their usage, refer to Image transformations.
For more details about Cloudinary's image transformation and manipulation in Ruby on Rails, see jQuery image manipulation.
Videos can be delivered and manipulated as well. See Video manipulation and delivery documentation page for more details.
5Sample projects
To find additional useful code samples and learn how to integrate Cloudinary with your web applications, take a look at our sample projects for Ruby on Rails, PHP and Django.
You should also take a look at the following fully working example that performs direct uploading from the browser with uploading progress and preview with advanced transformations and embeds in a dynamic HTML page using Cloudinary's jQuery plugin.
6What's next
Sign up for a free account if you haven't done so already. Follow the steps above and try Cloudinary out. Finished all steps? That's just an example of what Cloudinary can offer. Here's some additional reading material to help you get the best out of Cloudinary:
Learn more about jQuery image upload.
Explore powerful jQuery image manipulation features and see our Image transformations docs.
Stay tuned for updates, tips and tutorials: Blog, Twitter, Facebook.