Programmable Media

JavaScript SDK (Legacy) image transformations

Last updated: Oct-31-2023

Overview

After you or your users have uploaded image assets to Cloudinary, you can deliver them via dynamic URLs. You can include instructions in your dynamic URLs that tell Cloudinary to transform your assets using a set of transformation parameters. All transformations are performed automatically in the cloud and your transformed assets are automatically optimized before they are routed through a fast CDN to the end user for optimal user experience.

For example, you can resize and crop, add overlay images, blur or pixelate faces, apply a large variety of special effects and filters, and apply settings to optimize your images and to deliver them responsively.

Cloudinary's JavaScript libraries simplify the generation of transformation URLs, and include special components and directives for easy embedding of assets in your JavaScript application.

See also: JavaScript video transformations

Deliver and transform images

You can deliver your images using the imageTag method or via direct URL-building directives.

The imageTag method

The most common way to deliver images is using the imageTag method, which generates an instance of the ImageTag class. You can then generate an HTML tag by using the toHtml method, or create a DOM element which you can add to your DOM tree with the toDOM method.

The code above generates the following HTML image tag:

Transforming your image

Images are transformed by adding serialized transformation instructions to the image delivery URL. For example, to scale your image to a width of 400 pixels, add c_scale,w_400.

https://res.cloudinary.com/demo/image/upload/c_scale,w_400/sample.jpg

In JavaScript, transformations can be defined as a hash, for example:

In addition, the Cloudinary JavaScript SDK provide a convenient transformation builder named Transformation, for example:

You can use either form as a transformation parameter in the Cloudinary JavaScript SDK, and you can also convert from one format to the other. For example:

For example, to generate an image tag that delivers a JPEG image padded to a width of 400 pixels:

Additionally, you can add other, non-transformation parameters to the imageTag method such as the asset version, configuration parameters and HTML5 image tag attributes.

For example:

is compiled to:

Tip

In general, when using an SDK, you will probably take advantage of the SDK parameter names for improved readability and maintenance of your code. However, you can also optionally pass a raw_transformation parameter, whose value is a literal URL transformation definition. Note that the string you pass as the raw transformation value will be appended as is (with no processing or validation) to the end of any other transformation parameters passed in the same component of the transformation chain.

For example:

Chaining transformations

Cloudinary transformations can be chained together (each transformation is applied to the result of the previous transformation):

For example, the following code crops the image to 150x150, rounds the corners, applies a sepia effect, adds text to the top center of the resized image, and then rotates the entire result by 20 degrees.

The code above generates the following HTML image tag:

For more information on image transformations, see Applying common image transformations using JavaScript.

Direct URL building

If you need the image URL instead of a complete tag, use the url method.

The code above returns the following string:

https://res.cloudinary.com/demo/image/upload/sample.jpg

You can also include transformation parameters in the request, for example, to return the URL for an image padded to a width of 400 pixels:

The code above returns the following string:

https://res.cloudinary.com/demo/image/upload/c_pad,w_400/sample.jpg

Apply common image transformations using JavaScript

This section provides an overview and examples of the following commonly used image transformation features, along with links to more detailed documentation on these features:

Keep in mind that this section is only intended to introduce you to the basics of using image transformations with JavaScript.

For comprehensive explanations of how to implement a wide variety of transformations, see Image transformations. For a full list of all supported image transformations and their usage, see the Transformation URL API Reference.

Resizing and cropping

There are a variety of different ways to resize and/or crop your images, and to control the area of the image that is preserved during a crop. The following example uses the fill cropping method to generate and deliver an image that completely fills the requested 250x250 size while retaining the original aspect ratio. It uses face detection gravity to ensure that all the faces in the image are retained and centered when the image is cropped:

Original image before face recognition cropping Original image Fill cropping with 'faces' gravity Fill cropping with 'faces' gravity

You can also use automatic gravity to determine what to keep in the crop automatically.

Original image before automatic cropping Original image Fill cropping with 'auto' gravity Fill cropping with 'auto' gravity

For details on all resizing and cropping options, see resizing and cropping images.

Converting to another image format

You can deliver any image uploaded to Cloudinary in essentially any image format. There are three main ways to convert and deliver in another format:

  • Specify the image's public ID with the desired extension.
  • Explicitly set the desired format using the fetch_format parameter.
  • Use the auto fetch_format to instruct Cloudinary to deliver the image in the most optimized format for each browser that requests it.

For example:

  1. Deliver a .jpg file in .gif format:
  2. Let Cloudinary select the optimal format for each browser. For example, in Chrome, this image may deliver in .avif or .webp format (depending on your product environment setup):
    The above code generates a URL with the f_auto parameter:

For more details, see:

Applying image effects and filters

You can select from a large selection of image effects, enhancements, and filters to apply to your images. The available effects include a variety of color balance and level effects, tinting, blurring, pixelating, sharpening, automatic improvement effects, artistic filters, image and text overlays, distortion and shape changing effects, outlines, backgrounds, shadows, and more.

For example, the code below applies a cartoonify effect, rounding corners effect, and background color effect (and then scales the image down to a height of 300 pixels).

An image with several transformation effects

For more details on the available image effects and filters, see Visual image effects and enhancements.

Adding text and image overlays

You can add images and text as overlays on your main image. You can apply the same types of transformations on your overlay images as you can with any image and you can use gravity settings or x and y coordinates to control the location of the overlays. You can also apply a variety of transformations on text, such as color, font, size, rotation, and more.

For example, the code below overlays a couple's photo on a mug image. The overlay photo is cropped using face detection with adjusted color saturation and a vignette effect applied. The word love is added in a pink, fancy font and rotated to fit the design. A balloon graphic is also added. Additionally, the final image is cropped and the corners are rounded.

An image with many transformations and overlays applied

Image optimizations

By default, Cloudinary automatically performs certain optimizations on all transformed images. There are also a number of additional features that enable you to further optimize the images you use in your JavaScript application. These include optimizations to image quality, format, and size, among others.

For example, you can use the auto value for the fetch_format and quality attributes to automatically deliver the image in the format and quality that minimize file size while meeting the required quality level. Below, these two parameters are applied, resulting in a 351 KB AVIF file (in Chrome) instead of a 1.4 MB JPG with no visible change in quality.

50% file size optimization using auto format and auto quality features

For an in-depth review of the many ways you can optimize your images, see Image optimization.

Responsive image settings

Responsive web design is a method of designing websites to provide an optimal viewing experience to users, irrespective of the device, viewport size, orientation, or resolution used to view it. Ensuring that optimal experience means you should avoid sending high resolution images that get resized client side, with significant bandwidth waste for users of small displays. Instead, you should always deliver the right size image for each device and screen size.

For example, you can ensure that each user receives images at the size and device pixel ratio (dpr) that fit their device using the auto value for the dpr and width attributes. The auto value is replaced with actual values on the client side based on the screen properties and viewport width:

To activate this function, invoke the responsive() method after the page has loaded.

For example:

Cloudinary offers several options for simplifying the complexity of delivering responsive images. For a detailed guide on how to implement these options, see Responsive images.

✔️ Feedback sent!

Rate this page: