Cloudinary Blog

Cloudinary's jQuery plugin for embedding and transforming images

by Itai Lahan
As web developers, we closely monitor the shifts in today's modern web applications architecture stack. We find the client vs. server-side HTML rendering debate particularly interesting.
 
In the past several years, we’ve witnessed the enormous rise in popularity of client-side JS/CoffeeScript MVC & MVVM solutions. From popular libraries such as Backbone.js that strive to add basic structure to client-side apps, all the way to feature-rich libraries that manage your entire client-side stack, with data-binding, client-server model sync, dependency tracking, templates and more. The involvement of high-profile companies and individuals in this market is also fascinating, between KnockoutJS contributions from Microsoft, the Google-backed Angular and Yehuda Katz's Ember, the heat is definitely on. 
 
Client-side rendering adoption will most likely continue to grow as major companies adopt the concept and treat the browser as just another device to access their server-based APIs. Twitter is one of these companies. Twitter's engineers has moved to client-side rendering a year and a half ago as part of their last major redesign work. They even went a step further and open sourced their entire Javscript toolbelt as part of the Bootstrap project.
 
Since we at Cloudinary have been longtime fans of server-side rendering, the first web application development framework we've integrated Cloudinary with was Ruby-on-Rails. 
Due to popular demand, we really wanted to help simplify image management for Javascript developers - client-side rendering and node.js developers alike. Fortunately, Cloudinary’s techniques are already based on simple URL and HTTP interfaces that can be easily integrated with any web-dev framework.
 
Without further ado, we want to introduce Cloudinary’s jQuery plugin. It is open-source and available at GitHub.
 
We have big plans for this jQuery plugin. We’ll tell you more about it in the follow-up posts. In the meantime, we wanted to show you how to easily integrate Cloudinary for embedding images and transforming images using Javascript.
 
For example, a JPG file was uploaded to Cloudinary and was assigned the 'sample' public ID.
 
First, configure your cloud name:
$.cloudinary.config("cloud_name", "your_cloud_name_goes_here");
 
The following Javascript command returns the URL of a 150x100 transformed version of the uploaded image:
$.cloudinary.url("sample.jpg", 
                 { width: 150, height: 100,
                   crop: 'fill', gravity: 'south'});
 
It returns the following Cloudinary URL that goes through AWS CloudFront CDN. Note that 'demo' should be replaced with your Cloudinary's cloud name.
 

 
Cloudinary's jQuery plugin includes the 'image' function. It returns an IMG HTML element that you can add to your page.
 
The following example generates a 150x100 PNG version of the uploaded image, this time with rounded corners. It also generated an IMG tag and adds it to the HTML DOM elements with the 'image_holder' class:
$('.image_holder').append(
     $.cloudinary.image("sample.png", 
                        { width: 150, height: 100,
                          crop: 'fill', gravity: 'south', radius: 20}
                        ).addClass('my_class_name'));

This would generate the following HTML elements:
<div class="image_holder">
   ...
  <img width="150" height="100" class="my_class_name"   
           src="http://res.cloudinary.com/demo/image/upload/
                       w_150,h_100,c_fill,g_south,r_20/sample.jpg"/>
</div>

You can use the same method to create an image tag with a Facebook profile picture. Simply specify the user's Facebook name, fan page name or numeric ID and the required format.
For example, the following Javascript commands returns Cristiano Ronaldo's Facebook profile picture. Using Cloudinary's image transformations, the picture is cropped to a face detection based thumbnail of 80x100 pixels.
$.cloudinary.facebook_profile_image("Cristiano.jpg",
                    { width: 80, height: 100,
                      crop: 'thumb', gravity: 'face'});

 
See our documentation of Facebook & Twitter profile picture for more details.
 
Note: '$' in all examples can be replaced with ‘jQuery’.
 
Another useful thing you can do with the jQuery plugin is lazy loading of images and resizing on demand and on the fly. 
 
For example, add the following image tag to your HTML:
<img src="blank.png" class="dynamic_image”"
     data-src="couple.jpg" 
     data-width="115" data-height="135
     data-crop="thumb" data-gravity="faces" data-radius="20"/>
 
It points to a placeholder 'blank.png' image, but the actual image is 'couple.jpg'. 'couple' is a public ID of an image previously uploaded to Cloudinary. All attributes starting with 'data-' are parameters to Cloudinary's image transformation engine.
 
Simply running 'cloudinary' on a given jQuery selector would process all images matching the given selector. The following example processes all elements with the dynamic_image class when the document is ready:
$(document).ready(function() {
     $(".dynamic_image").cloudinary();
});
 
This would automatically generate transformed versions on the fly. In this example, a 115x135 rounded-cornered thumbnail based on face detection. The transformed image is delivered through a fast CDN. The IMG tag would be converted to the following and the requested image would be displayed:
<img src="http://res.cloudinary.com/demo/image/upload/
                    w_115,h_135,c_thumb,g_faces,r_20/couple.jpg"
     width="115" height="135" class="dynamic_image"/>

 
What do you think? We would love to hear your feedback on our jQuery plugin and your ideas for enhancements. We are already working on some cool additions, we would update you soon...
 

Recent Blog Posts

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
Automatically create images for Responsive design

Responsive web design is a method of designing websites to provide an optimal viewing experience to users, irrespective of the device, window size, orientation, or resolution used to view the website. A site designed responsively adapts its layout to the viewing environment, resizing and moving elements dynamically and based on the properties of the browser or device the site is being displayed on.

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

How Answers.com manage millions of images

by Orly Bogler
How Answers.com utilizes Cloudinary to manage millions of images

When was the last time you've asked Google about your favorite band, movie star, or personal hobby? I can only assume that one of the first results that came up was from Answers.com. Nearly everyone knows this website, which is on the Quantcast Top 10 most visited sites in the world.

Read more
Control the zoom level with automatic image cropping

Many websites now offer their users the ability to upload images and profile pictures, making it a challenge for web designers to maintain a certain graphic design and style when subsequently displaying these images. The profile pictures may need to be smartly cropped to focus on the faces, with some sites that prefer close-ups of faces and others that prefer including more background when displaying images of people.

Read more
Introducing cloud based service for video management

They say that a picture is worth a thousand words. For modern websites, a video surely takes the visual impact to a whole new level.

Nowadays, people enjoy the amazing capability of shooting videos with smartphone cameras that easily fit in their back pockets and are accessible nearly everywhere. Modern web applications have an opportunity to dramatically increase their visual impact by showcasing these videos online. Between news reports, user shared video snippets, explainer videos and ad campaigns, we see more and more videos appearing daily in our visited websites.

Read more