Cloudinary Blog

Centralized control for image upload - image size, format, thumbnail generation, tagging and more

Central control for image upload and post-upload actions

There are many options to consider when allowing a user to upload an image to your website or mobile app. You might wish to limit the size and format of the uploaded images. You may want to apply specific manipulations to the images, such as cropping, resizing and adapting the image to your site's look & feel. Beyond that, it's common to create a variety of thumbnails from a newly uploaded image.

In many cases, image upload options are hard-coded in the application code. This makes it difficult to adapt to new design requirements and new types of content that are introduced over time. Also, if you're developing a mobile app, when making a change to an upload parameter, you might have to re-submit your app to the app store and wait for users to upgrade - until then, the new image settings will not take effect.

In this post, we'll show how to centrally control image upload options using Cloudinary. Cloudinary is a cloud-based image management solution that gives you a ready-made implementation of the image upload process - and also stores your images in the cloud and helps you deliver them to users in an optimal manner. Cloudinary's upload mechanism includes special options like eagerly generating face-detection-based thumbnails, flagging images for moderation, callback notification, automatic categorization based on image content, image format conversion and normalization, and more.

Our new feature, called upload presets, allows you to update your uploaded image settings centrally via a web interface or API, while auto-applying to all your web sites and mobile applications.

Common uses of upload presets - centrally defined upload options

In Cloudinary, upload presets include one of more upload parameters. Any of Cloudinary's upload parameters can be defined and included in a preset. A preset can be modified at any time using Cloudinary's Management Console (UI) or Admin API, and changes are applied immediately.

Some common examples of upload presets:

  • Defining that uploaded images should be limited to a certain size and converted to a given image format. This is usually done as an “incoming transformation” which performs manipulation on the image while it is uploaded, and before it is stored on the cloud.

  • Assigning tags for listing and managing an image or file in Cloudinary's Media Library.

  • Creating a folder and organizing uploaded images into specific folders.

  • Defining and updating an 'upload-completed' callback notification URL, enabling your application to get notification from Cloudinary when an upload is complete.

  • Defining image manipulations that occur automatically after upload, and generate one or more processed images - thumbnails, face-detection-based cropped versions, artistic effects like sepia or saturation, and more - to automatically fit images to your site’s look and feel (these are called 'eager image transformations').

  • Restricting image upload to certain allowed image formats.

  • Marking images for manual moderation (see our blog post explaining Cloudinary's built-in UI and API allowing editors to moderate, accept and reject user uploaded images).

  • Enabling automatic backup of uploaded files.

For a full list of supported upload parameters, see this documentation page.

Using upload presets in Cloudinary

Each upload preset has a unique name. To apply an upload preset when uploading an image from your code, simply specify this name as the value of the upload_preset parameter, when calling the upload API.

You can use this parameter in two types of upload: (1) Upload initiated by a server-side call of Cloudinary's authenticated upload API, and (2) Upload directly from the user's browser to Cloudinary - via signed file input fields in your HTML pages.

The following Ruby, PHP and Node.js code samples show a server-side upload request that specifies an upload preset named ta2ozk9e:

Ruby:
Cloudinary::Uploader.upload("sample.jpg", :upload_preset => 'ta2ozk9e')
PHP:
\Cloudinary\Uploader::upload('sample.jpg', array("upload_preset" => "ta2ozk9e"));
Python:
cloudinary.uploader.upload("sample.jpg", upload_preset = 'ta2ozk9e')
Node.js:
cloudinary.uploader.upload('sample.jpg', function(result) { console.log(result) }, 
                           { upload_preset: "ta2ozk9e" });
Java:
cloudinary.uploader().upload(new File("sample.jpg"), Cloudinary.asMap("upload_preset", "ta2ozk9e"));

A dedicated upload preset can be defined for each type of image users upload to your application. So you could have one set of options for profile photo uploading, and a different set for uploading to shared albums.

In addition, if you are using authenticated signed uploads, you can specify custom upload parameters in the upload API call, and these custom parameters will override the default settings of the upload preset.

Creating upload presets using the Management Console

Cloudinary's Management Console can be used to list, create and edit upload presets:

List upload presets UI

A preset name is randomly generated by default, to ensure uniqueness. In the Upload Settings page, you can edit this default name, and define which upload parameters should be applied in this preset. The interface allows you to create presets, list them, edit existing presets and delete unused presets.

In addition, you can apply any of Cloudinary's image manipulations as part of the upload preset. The Edit Transformation dialog, shown below, allows you to interactively define an image manipulation which should be performed immediately after an image is uploaded.

Edit upload preset UI

For example, you can easily define that after an image is uploaded, Cloudinary should generate a thumbnail, modify image dimensions, rotate the image or create rounded corners. All of these are applied automatically, and the user's uploaded image is already modified before it is saved to cloud storage.

API for managing presets

You may also want to manage your presets programmatically. Cloudinary provides an authenticated Admin API for managing upload presets in your account.

For example, the following Ruby, PHP and Node.js code snippets define an upload preset that limits images to 2000x2000 before storing in the cloud. In addition, images are tagged as 'mobile_upload'.

Ruby:
Cloudinary::Api.create_upload_preset(:name => "mobile_profile_photo", 
      :width => 2000, :height => 2000, :crop => :limit, :tags => "mobile_upload")
PHP:
\Cloudinary\Api::create_upload_preset(array("name" => "mobile_profile_photo", 
      "width" => 2000, "height" => 2000, "crop" => "limit", "tags" => "mobile_upload"));
Python:
cloudinary.api.create_upload_preset(name = "mobile_profile_photo", 
      width = 2000, height = 2000, crop = "limit", tags => "mobile_upload")
Node.js:
cloudinary.api.create_upload_preset(function(result) { console.log(result) }, 
      { name: "mobile_profile_photo", 
         width: 2000, height: 2000, crop: 'limit', tags: "mobile_upload" });
Java:
cloudinary.api().createUploadPreset(Cloudinary.asMap(
   "name", "mobile_profile_photo", 
   "transformation", new Transformation().width(2000).height(2000).crop("limit").generate(), 
   "tags", "mobile_upload"));

To modify an upload preset, call the update_upload_preset API method, supplying the preset name:

Ruby:
Cloudinary::Api.update_upload_preset("mobile_profile_photo", 
      :width => 3000, :height => 3000, :crop => :limit, :tags => "mobile_upload",
      :folder => "users")
PHP:
\Cloudinary\Api::update_upload_preset("mobile_profile_photo", 
      array("width" => 3000, "height" => 3000, "crop" => "limit", "tags" => "mobile_upload", "folder" => "users"));
Python:
cloudinary.api.update_upload_preset("mobile_profile_photo", 
     width = 3000, height = 3000, crop = "limit", tags => "mobile_upload",
     folder = "users")
Node.js:
cloudinary.api.update_upload_preset("mobile_profile_photo", function(result) { console.log(result) }, 
      { width: 3000, height: 3000, crop: 'limit', tags: "mobile_upload",
        folder: 'users' });
Java:
cloudinary.api().updateUploadPreset("mobile_profile_photo", Cloudinary.asMap(
   "transformation", new Transformation().width(3000).height(3000).crop("limit").generate(), 
   "tags", "mobile_upload", 
   "folder", "users"));

Use the following code to list all upload presets defined in your account, with their details:

Ruby:
Cloudinary::Api.upload_presets
PHP:
\Cloudinary\Api::upload_presets();
Python:
cloudinary.api.upload_presets()
Node.js:
cloudinary.api.upload_presets(function(result) { console.log(result) });
Java:
cloudinary.api().uploadPresets(Cloudinary.emptyMap());

Default presets

Even though images uploaded in different situations require different settings, there are many settings that are common across all uploaded images. Therefore, you might want to apply a certain upload preset to all uploaded images by default. To do that, you can mark a certain preset as default.

Defining a default preset means that all upload requests that don't specify a preset name, are applied with the default settings. It also means that you can now modify the behavior of your existing production sites or mobile apps, even if you didn't explicitly use a preset in your code.

For example, you can modify your live iPhone application, without requiring an update of the app, to add the following upload options: create face-detection-based thumbnails eagerly to improve user experience, add the tag 'iPhone uploads', allow only images of file type JPG, and mark for manual moderation.

The default upload preset is also applied to images automatically uploaded via remote fetch URLs - see our blog post on automatic migration of images.

Summary

Cloudinary provides a rich set of upload options and image manipulation capabilities. With the introduction of upload presets, our platform allows a healthy separation between generic upload code and upload settings. You should be able to control and apply settings centrally from a management console or API, without updating production code or releasing new versions of your mobile app.

The upload presets described in this post are used for signed (authenticated) upload requests to Cloudinary, based on a public API Key and a private API Secret. In addition, we also support unsigned client side uploading based on upload presets. Stay tuned for more details regarding our new unsigned upload feature.

Creation, management and usage of upload presets if available for all Cloudinary's plans, including the free tier. Try it yourself by opening a free account. We will also greatly appreciate your feedback on this feature - give us a shout out in the comments below or via Facebook or Twitter.

Recent Blog Posts

 New Image File Format: FUIF:Lossy, Lossless, and Free

I've been working to create a new image format, which I'm calling FUIF, or Free Universal Image Format. That’s a rather pretentious name, I know. But I couldn’t call it the Free Lossy Image Format (FLIF) because that acronym is not available any more (see below) and FUIF can do lossless, too, so it wouldn’t be accurate either.

Read more
Optimizing Video Streaming and Delivery: Q&A with Doug Sillars

Doug Sillars, a digital nomad and a freelance mobile-performance expert, answers questions about video streaming and delivery, website optimization, and more.

Doug Sillars, a freelance mobile-performance expert and developer advocate, is a Google Developer Expert and the author of O’Reilly’s High Performance Android Apps. Given his extensive travels across the globe—from the UK to Siberia—with his wife, kids, and 11-year-old dog, Max, he has been referred to as a “digital nomad.” So far in 2018, Doug has spoken at more than 75 meetups and conferences!

Read more
Building a Music Discovery Service

In May 2018, Cloudinary sponsored Capitol Music Group’s first hackathon held by its new Capitol360 Innovation Center, which aims at connecting musicians and software technologists to facilitate and stimulate the creation of music. See this interview for details. As a starter project for the hackathon, we built a sample app called Music Discovery Service.

Read more
Once a Hackathon Participant, Now a Judge

Over the past several years, I've had a chance to participate in hackathons, as part of teams that developed a social payment app and helped users decide meals to cook. But it wasn't until last month that I got to experience a hackathon from the other side, as a judge.

Read more
10 Website Videos Mistakes and How to Solve Them

It should come as no surprise that video use on the internet is exploding. You can see the dramatic growth of video on the average site in this SpeedCurve blog post.

With the growth in video comes greater bandwidth use, which is not only costly for your IT budget, but for your visitors as well. Beyond the expense, there is the user experience to consider. The heavier the page, the longer it will take to load, and the greater likelihood visitors will abandon your site. Page load speed is also an important factor in SEO ranking, so clearly video is something we need to take seriously and get right. Video is challenging, presenting terms still unfamiliar to developers - like codecs, bitrate and adaptive bitrate streaming. As a result, mistakes are being made in video implementation.

Read more
Android Data Saver: Optimizing Mobile Data Usage with Cloudinary

Over the life of a mobile device, the cost of a cellular data plan often exceeds that of the device itself. To optimize data usage and purge useless data on their mobile devices, users can enable Data Saver from Android 7.0 (API level 24). To do so, users toggle Data Saver in quick settings under the Notification shade or under Settings > Data usage. With Data Saver enabled, apps that aren't whitelisted cannot use cellular data in the background. They are also directed to consume less data while active.

Read more