Hello World - Scaling an image

Code Example

import {Cloudinary} from "@cloudinary/url-gen/instance/Cloudinary";
import {scale} from "@cloudinary/url-gen/actions/resize";

// Create your instance
const cld = new Cloudinary({
    cloud: {
        cloudName: 'demo'
    },
    url: {
        secure: true // force https, set to false to force http
    }
});


// Create a new image
const myImage = cld.image('sample');

myImage.resize(
    scale()
        .width(100)
        .height(100)
);

const myURL = myImage.toURL();
// End result: https://res.cloudinary.com/demo/image/upload/c_scale,w_100,h_100/sample

Explanation

The code above performs the following operations:

  • Imports Cloudinary modules and classes
  • Creates a new Cloudinary instance, with a set of configurations
  • Instructs your instance to work with a Cloudinary Image (CloudinaryImage)
  • Creates a new CloudinaryImage instance with a publicID of 'sample' (cld.image('sample'))
  • Imports the scale() Action from '@cloudinary/url-gen/actions/resize'
  • Uses the Action methods width and height to set the scale size
  • Applies your changes and creates a URL string