Cloudinary Blog

How to quickly build a stock photo site using Cloudinary

by Tal Lev-Ami
Different web applications have very different requirements when images are involved. A dating website will focus its image-related attention on normalizing and delivering his user uploaded snapshots. A pinterest style service is focused on retrieving external images from around the web and fitting them to its cool layout. An eCommerce website is focused on the effective delivery of the many product thumbnails it depicts.
 
While every website has different image related needs, they all share many commonalities. At Cloudinary we strive to coalesce all of our customers' image requirements into a single coherent image pipeline management service.
 
In the past several months we were repeatedly asked about private images and strict transformations. These two features are now available through Cloudinary, and what better way to tell you about them then showing you how these work in a real-world use-case - building a stock photo site.
 

Stock Photo Service - Requirements

So what does building a stock photo service entails?
 
Our photos website’s content manager will want to upload the original hi-resolution images to the cloud for safe storage. These are our most prized assets and will need to be protected from prying eyes (and clicks). We will want to publicly display smaller, low resolution thumbnails of our photos for potential buyers to browse. We'll also want to add watermarks to all of our images to prevent copying, and deliver these many images through a fast CDN for quick browsing. Finally, after a purchase was made, we'll want to allow our buyer (and only him) access to his purchased hi-resolution photo.
 
If you are familiar with Cloudinary, you already know that we make it quite simple to upload images to the cloud, generate thumbnails, add watermarks and deliver the resulting images through a fast CDN. Today we wanted to introduce our latest two features - uploading private, authenticated images and strict transformations.
 
To make this post shorter, most of the examples below are for Ruby-on-Rails. The same can be easily accomplished with any one of our client libraries, or by directly using our URL-based APIs.
 

Uploading Private Images to the Cloud

When uploading images to Cloudinary, both the original images and their transformed versions are publicly available through a CDN. Our default randomly generated public IDs will prevent users from guessing your URLs.
 
This is all very nice for the majority of websites out there, but simply won't do for our stock photo site. Here, we'll want a far more secured approach, and this is where private uploads comes in.
 
Starting today, you can upload private images to Cloudinary. Simply set the type parameter to 'private' (instead of the default 'upload' type). For example, the following Ruby code uploads a private image to Cloudinary:
Cloudinary::Uploader.upload("sheep.jpg", :public_id => "sheep", :type => :private)
 
Same in PHP, Python and Node.js:
\Cloudinary\Uploader::upload("sheep.jpg", "public_id" => "sheep", "type" => "private")
cloudinary.uploader.upload("sheep.jpg", public_id = "sheep", type = "private")
cloudinary.uploader.upload("sheep.jpg", function(result) { }, 
                           {public_id: 'sheep', type: "private"})
If you use CarrierWave in your Ruby project, simply add 'make_private' to your uploader class. In the example below we also request to eagerly generate a 164 by 164 thumbnail on upload, and store it in JPG format with 80% quality:
class PictureUploader < CarrierWave::Uploader::Base  
    include Cloudinary::CarrierWave  
    make_private
    eager

    version :medium do    
      process :resize_to_fill => [164, 164, 'North']
      process :convert => 'jpg'
      cloudinary_transformation :quality => 80
    end
  end
Trying to publicly access the uploaded image is not possible:
 

Strict transformations

One of Cloudinary's more powerful features is the ability to dynamically transform images on-the-fly. However, in some cases, like our stock photo site example, you might prefer to prevent your users from accessing just any type of image transformation. You want to strictly define the transformations you allow.

Starting today, you can switch your account to a strict transformations mode by enabling the new "Strict Transformations" setting in your Cloudinary management console's settings page:

When strict transformations are enabled, users can publicly access only images created through named transformations or dynamic transformations that you personally marked as allowed. In addition, incoming or eager transformations generated using an authenticated request to our API, are also allowed.

To mark a transformation as allowed or disallowed, head to the "Transformations" section of the Management Console. Near each transformation, you’ll find a green / red icon. Click it to allow or disallow the transformation. You can also pre-allow dynamic transformations when Strict Transformations are still disabled - can be useful when you're in development mode.

 

Trying to generate and access an allowed transformation will work fine:

http://res.cloudinary.com/private-demo/image/private/w_300,h_200,c_fill,r_20/sheep.jpg

 

Trying to access any other transformation, either disallowed or non-existing, will simply fail. As you can see below, you can always check the X-Cld-Error response header for finding out the reason of non-delivered images.

http://res.cloudinary.com/private-demo/image/private/c_fill,h_200,w_320/sheep.jpg

http://res.cloudinary.com/private-demo/image/private/w_1.0,r_20/sheep.jpg

Status Code: 401 Unauthorized
X-Cld-Error: Transformation w_1.0,r_20 is not allowed

Back to our stock photo site - we can't just allow anyone to dynamically create hi-res transformations of our originals, can we? We will be better off allowing only low resolution transformations that add a watermark to all images:

.../image/private/w_200,h_150,c_fill/l_watermark,w_200,h_150/sheep.jpg

 

Private download URLs

Privately uploading images together with strict transformations allow safe, cloud-based storage for images that are inaccessible to your users, side-by-side with publicly available scaled down versions of these same images.

This is a great start for our stock photo site, but one crucial feature still remains. Assume that a buyer purchases a photo, how can we deliver the original image to him (and only to him) while our originals are inaccessible to anyone but us?

For that, you (and only you) can generate a unique, signed URL, based on your account's API Key and Secret. Anyone you share this URL with will have temporary access to download the original, high-resolution image. This URL will automatically expire after one hour.

For example, generating such a URL in Rails is done by simply calling the cl_private_download_url view helper method (or Cloudinary::Utils.private_download_url from your model or controller):

<%= link_to("Download", cl_private_download_url("sheep", :jpg)) %>

This will generate a link similar to the following (this one has already expired...):

https://api.cloudinary.com/v1_1/private-demo/image/download?api_key=824698761754661&format=jpg&public_id=sheep&signature=d994c2b972c30d84d33fde684aa377fc17878be6&timestamp=1346076992

This method delivers the original images through a secure authenticated API request and not through the faster CDN. Therefore, this method is most appropriate when the original images are not accessed very frequently.

Note - We've recently added a more advanced solution that allows delivering private (authenticated) images to your users through a CDN by generating a signed URL on your side using your own private key. This alternative solution utilizes Amazon CloudFront signed URLs. It is already available for our Advanced, and higher plans. Please contact us if this interests you.

 

Summary

Uploading private and authenticated content were features frequently requested by many of Cloudinary's customers. Supporting these now, opens a wide range of new image management streamlining use-cases that Cloudinary can cover, such as our imaginary new stock photo site.

Strict transformations and private uploading are available to all of our plans, free and paid. As mentioned above, authenticated image delivery through a CDN is available for the Advanced plan or higher (contact us for details).

The stock photo example is a very cool use case. There are plenty of other use cases that these features are relevant for. Do you have such an interesting use-case you can share? Tell us about it and we will be happy to share it with our community.

Recent Blog Posts

Dynamic ZIP files generation with a single line of code

As a developer, you want to allow your users to download multiple files in a single click. An easy way to download multiple files and share them is to generate a ZIP file. When images are involved, you may also want to normalize the original images before including them in the ZIP file, by scaling them down to the same maximum resolution or converting them to the same format.

Read more
How mindbodygreen reduce costs & save development time

mindbodygreen is a digital media brand dedicated to health and wellness. The company’s website bring together leaders in the wellness world, best-selling authors, fitness experts, trusted healers, pioneering doctors, top chefs, celebrities and top-tier journalists to provide readers fresh, expert content and tools to help them live healthy, happy lives. The company was founded in 2009 and is headquartered in Brooklyn, N.Y. 
Read more

Why JPEG is like a photocopier

by Jon Sneyers
Why JPEG is like a photocopier (generation loss)

If you make a copy of a copy of a copy, the quality will deteriorate with every ‘generation’. This problem is called ‘generation loss’. It is not difficult to understand why this happens with actual copier machines. Scanning and printing are not perfect, being based on noisy sensors and physical paper and ink, and the resulting noise will tend to accumulate.

Read more
Apply conditions to dynamically manipulate images.

It's great to have the capability to manipulate images on the fly by using dynamic URLs to customize the images to fit the graphic design of your site or mobile application. However, what if you want to manipulate an image depending on a specific image characteristic (like its width or aspect ratio) or its contents (does it contain a face?). What you need is a way to apply a transformation to an image only if a specific condition is met. Take for example a situation where you have allocated space on your page for a user uploaded image with a width and height of 200 pixels. Furthermore, if the image contains a face you would like to zoom in and focus on the face itself, otherwise you would like to fit the entire image into the available space:

Read more
Moviepilot focus on core business and reduce costs
 
Moviepilot Inc. is a media company that hosts a website and social publishing platform designed to bring together movie and TV fans, and help studios trigger, fuel and amplify positive conversations about entertainment topics. The site invites fans to create content, such as writing articles and producing videos, which are then curated on the site and distributed to more than 30 million fans worldwide. The site boasts more than 20 million unique visitors per month, with 87 percent of visitors accessing the site on mobile devices. The company is headquartered in Venice Beach, Calif., and has an office Berlin, Germany, where it was originally founded.
Read more