Go media transformations

After you or your users have uploaded media 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 actions. 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 or concatenate videos, blur or pixelate faces, apply a large variety of special effects and filters, and apply settings to optimize your media and to deliver them responsively.

Cloudinary's Go SDK simplifies the generation of transformation URLs for easy embedding of assets in your Go application.

Syntax overview

The Go SDK allows you to build URLs for delivering assets stored in your Cloudinary account, and optionally add transformations by passing them directly as strings. This is enabled by the Go SDK's custom-defined objects which store information about a specified asset.

Using the Go SDK, you can generate a delivery URL:

  • Of the original asset.
  • With transformations applied.
  • With other modifications.

In order to achieve the desired edit or optimization of your asset using the Go SDK, you need to correctly formulate the desired transformation as a string and then pass it as a parameter. For example, if you want to set the height of your asset to 300 pixels and apply a cartoonify effect, you need to pass the string c_scale,w_300/e_cartoonify as the transformation parameter.

For comprehensive coverage of all available URL transformation parameters and detailed instructions as to how to formulate your transformations, see the Transformation URL API reference

Deliver and transform assets

The @cloudinary/cloudinary-go library makes it easy for you to create image URLs including any transformation parameters.

Direct URL building

You can build an asset URL by:

  1. Instantiating an asset object using cld.Image(PublicID) or cld.Video(PublicID).
  2. Customize or transform your asset URL by passing parameters to the object.
  3. Calling the String() method of the Go asset object to return the delivery URL.
Copy to clipboard
import(
    "fmt"
    "github.com/cloudinary/cloudinary-go/v2"
)

func main() {
    // Instantiate an object for the image with public ID "maroon_hat" in folder "docs/sdk/go"
    i, err := cld.Image("docs/sdk/go/maroon_hat")
    if err != nil {
        fmt.Println("error")
    }

    // Add the transformation
    i.Transformation = "f_auto/q_auto/c_fill,g_face,h_200,w_200/a_-15"

    // Generate and print the delivery URL
    myURL, err := i.String()
    fmt.Println(myURL)
    if err != nil {
        fmt.Println("error")
    }
}

The resulting URL, myURL, is:

Ruby (cloudinary 1.x):
Copy to clipboard
cl_image_tag("docs/sdk/go/maroon_hat.jpg", :transformation=>[
  {:quality=>"auto"},
  {:gravity=>"face", :height=>200, :width=>200, :crop=>"fill"},
  {:angle=>-15}
  ])
PHP (cloudinary_php 2.x):
Copy to clipboard
(new ImageTag('docs/sdk/go/maroon_hat.jpg'))
  ->delivery(Delivery::format(
  Format::auto()))
  ->delivery(Delivery::quality(
  Quality::auto()))
  ->resize(Resize::fill()->width(200)
->height(200)
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  )
  ->rotate(Rotate::byAngle(-15))
  ->version(1639034832);
PHP (cloudinary_php 1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/sdk/go/maroon_hat.jpg", array("transformation"=>array(
  array("quality"=>"auto"),
  array("gravity"=>"face", "height"=>200, "width"=>200, "crop"=>"fill"),
  array("angle"=>-15)
  )))
Python (cloudinary 1.x):
Copy to clipboard
CloudinaryImage("docs/sdk/go/maroon_hat.jpg").image(transformation=[
  {'quality': "auto"},
  {'gravity': "face", 'height': 200, 'width': 200, 'crop': "fill"},
  {'angle': -15}
  ])
Node.js (cloudinary 1.x):
Copy to clipboard
cloudinary.image("docs/sdk/go/maroon_hat.jpg", {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 200, width: 200, crop: "fill"},
  {angle: -15}
  ]})
Java (cloudinary 1.x):
Copy to clipboard
cloudinary.url().transformation(new Transformation()
  .quality("auto").chain()
  .gravity("face").height(200).width(200).crop("fill").chain()
  .angle(-15)).imageTag("docs/sdk/go/maroon_hat.jpg");
JS (@cloudinary/url-gen 1.x):
Copy to clipboard
new CloudinaryImage("docs/sdk/go/maroon_hat.jpg")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(200)
      .height(200)
      .gravity(focusOn(face()))
  )
  .rotate(byAngle(-15))
  .version(1639034832);
JS (cloudinary-core 2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/sdk/go/maroon_hat.jpg', {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 200, width: 200, crop: "fill"},
  {angle: -15}
  ]}).toHtml();
jQuery (cloudinary-jquery 2.x):
Copy to clipboard
$.cloudinary.image("docs/sdk/go/maroon_hat.jpg", {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 200, width: 200, crop: "fill"},
  {angle: -15}
  ]})
React (@cloudinary/react 1.x):
Copy to clipboard
//React SDK transformations are created using @cloudinary/url-gen.
new CloudinaryImage("docs/sdk/go/maroon_hat.jpg")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(200)
      .height(200)
      .gravity(focusOn(face()))
  )
  .rotate(byAngle(-15))
  .version(1639034832);
React (cloudinary-react 1.x):
Copy to clipboard
<Image publicId="docs/sdk/go/maroon_hat.jpg" >
  <Transformation quality="auto" />
  <Transformation gravity="face" height="200" width="200" crop="fill" />
  <Transformation angle="-15" />
</Image>
Vue.js (cloudinary-vue 1.x):
Copy to clipboard
<cld-image public-id="docs/sdk/go/maroon_hat.jpg" >
  <cld-transformation quality="auto" />
  <cld-transformation gravity="face" height="200" width="200" crop="fill" />
  <cld-transformation angle="-15" />
</cld-image>
Angular (@cloudinary/ng 1.x):
Copy to clipboard
//Angular SDK transformations are created using @cloudinary/url-gen.
new CloudinaryImage("docs/sdk/go/maroon_hat.jpg")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(200)
      .height(200)
      .gravity(focusOn(face()))
  )
  .rotate(byAngle(-15))
  .version(1639034832);
Angular (@cloudinary/angular-5.x 1.x (legacy)):
Copy to clipboard
<cl-image public-id="docs/sdk/go/maroon_hat.jpg" >
  <cl-transformation quality="auto">
  </cl-transformation>
  <cl-transformation gravity="face" height="200" width="200" crop="fill">
  </cl-transformation>
  <cl-transformation angle="-15">
  </cl-transformation>
</cl-image>
.NET (CloudinaryDotNet 1.x):
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Quality("auto").Chain()
  .Gravity("face").Height(200).Width(200).Crop("fill").Chain()
  .Angle(-15)).BuildImageTag("docs/sdk/go/maroon_hat.jpg")
iOS (cloudinary 3.x):
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation()
  .setQuality("auto").chain()
  .setGravity("face").setHeight(200).setWidth(200).setCrop("fill").chain()
  .setAngle(-15)).generate("docs/sdk/go/maroon_hat.jpg")!, cloudinary: cloudinary)
Android (cloudinary-android 1.x):
Copy to clipboard
MediaManager.get().url().transformation(new Transformation()
  .quality("auto").chain()
  .gravity("face").height(200).width(200).crop("fill").chain()
  .angle(-15)).generate("docs/sdk/go/maroon_hat.jpg");
Kotlin (kotlin-url-gen 1.x):
Copy to clipboard
cloudinary.image {
  publicId("docs/sdk/go/maroon_hat.jpg")
   delivery(Delivery.format(
  Format.auto()))
   delivery(Delivery.quality(
  Quality.auto()))
   resize(Resize.fill() { width(200)
 height(200)
   gravity(
  Gravity.focusOn(
  FocusOn.face()))
   })
   rotate(Rotate.byAngle(-15))
   version(1639034832) 
}.generate()
Sample image

1. Determine the asset type

You can instantiate an object that will capture and provide access to all the information needed to build your asset's delivery URL.

Different object are used to instantiate assets of different types:

Object Asset type
File used for raw assets
Image used for images
Video used for videos
Media used when the asset type is unknown. In this case, the AssetType can be set dynamically.

Use the appropriate object for your asset type, and identify the asset you are instantiating by passing the object a public ID:

Copy to clipboard
img, err := cld.Image(PublicID)

Note
The folder path is included in the public ID when instantiating an object. The images and videos on this page are located in the docs/sdk/go folder.

2. Customize and transform your asset delivery URL

The parameters described in the Object parameters table are part of the object you instantiated. These parameters, automatically populated with the values of the asset you identified, contain the information needed to generate the asset's delivery URL:

Object parameters table
Parameter Description
AssetType The type of asset (raw, image, or video), determined by the method you used to create the object (File, Image, or Video). For the Media object, the default AssetType is image, but you can change this parameter dynamically.
DeliveryType Provides an indication about the way the asset will be delivered, although in most cases, the delivery type is determined at the time that the asset is stored in your account. The default delivery type is upload.
Transformation Contains the transformation string, which, when appended to the delivery URL, programmatically modifies the original image or video asset, resulting in a newly generated (derived) media file based on the original.
Version Represents the timestamp of the upload and enables you to access the latest version of an asset, or backed-up versions of it.
PublicID The unique identifier of the asset.
Suffix A user-friendly, descriptive ending that you can add as a suffix to the public ID in the delivery URL.
Config The configuration parameters that you set globally for your SDK.

You can change parameters stored in the object to modify the delivery URL. For example, you can add a suffix, change the asset type, or change the asset version in the delivery URL.

By assigning a string containing transformation components to the Transformation parameter, you can change, optimize, edit, and enhance the asset generated by the delivery URL.

Dynamically assign an asset type

To instantiate an object and then dynamically assign it an AssetType:

Copy to clipboard
m, err := cld.Media("docs/sdk/go/play_time")

m.AssetType="video"

// URL: https://res.cloudinary.com/demo/video/upload/v1/docs/sdk/go/play_time
Change the version of an asset

To instantiate an image object and then change the version of the asset:

Copy to clipboard
i, err := cldImage("docs/sdk/go/")

i.Version = "v1639034812"

// URL: https://res.cloudinary.com/demo/image/upload/v1639034812/docs/sdk/go/apple

Assign a transformation

To instantiate an image object and then assign the Transformation parameter transformation components:

Copy to clipboard
i, err := cld.Image("docs/sdk/go/maroon_hat")

i.Transformation = "f_auto/q_auto/c_fill,g_face,h_250,w_250/e_sepia:80"

// URL: https://res.cloudinary.com/demo/image/upload/f_auto/q_auto/c_fill,g_face,h_250,w_250/e_sepia:80/v1/docs/sdk/go/maroon_hat

In the above example: * f_auto and q_auto optimize the format and quality of the image automatically * c_fill crops the image while keeping its aspect ratio * g_face keeps the image focused on the face after cropping * w_250 and h_250 set the width and height of the transformed image to 250px * e.sepia applies a special effect called "sepia" to the image.

3. Return the delivery URL

If you added a transformation or modified the parameters of the object, the URL you generate will reflect those changes.

The following code generates the URL from the example above:

Copy to clipboard
myURL, err := i.String()

The resulting URL and image:

Ruby (cloudinary 1.x):
Copy to clipboard
cl_image_tag("docs/sdk/go/maroon_hat", :transformation=>[
  {:quality=>"auto"},
  {:gravity=>"face", :height=>250, :width=>250, :crop=>"fill"},
  {:effect=>"sepia:80"}
  ])
PHP (cloudinary_php 2.x):
Copy to clipboard
(new ImageTag('docs/sdk/go/maroon_hat'))
  ->delivery(Delivery::format(
  Format::auto()))
  ->delivery(Delivery::quality(
  Quality::auto()))
  ->resize(Resize::fill()->width(250)
->height(250)
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  )
  ->effect(Effect::sepia()->level(80))
  ->version(1639034832);
PHP (cloudinary_php 1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/sdk/go/maroon_hat", array("transformation"=>array(
  array("quality"=>"auto"),
  array("gravity"=>"face", "height"=>250, "width"=>250, "crop"=>"fill"),
  array("effect"=>"sepia:80")
  )))
Python (cloudinary 1.x):
Copy to clipboard
CloudinaryImage("docs/sdk/go/maroon_hat").image(transformation=[
  {'quality': "auto"},
  {'gravity': "face", 'height': 250, 'width': 250, 'crop': "fill"},
  {'effect': "sepia:80"}
  ])
Node.js (cloudinary 1.x):
Copy to clipboard
cloudinary.image("docs/sdk/go/maroon_hat", {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 250, width: 250, crop: "fill"},
  {effect: "sepia:80"}
  ]})
Java (cloudinary 1.x):
Copy to clipboard
cloudinary.url().transformation(new Transformation()
  .quality("auto").chain()
  .gravity("face").height(250).width(250).crop("fill").chain()
  .effect("sepia:80")).imageTag("docs/sdk/go/maroon_hat");
JS (@cloudinary/url-gen 1.x):
Copy to clipboard
new CloudinaryImage("docs/sdk/go/maroon_hat")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(250)
      .height(250)
      .gravity(focusOn(face()))
  )
  .effect(sepia().level(80))
  .version(1639034832);
JS (cloudinary-core 2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/sdk/go/maroon_hat', {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 250, width: 250, crop: "fill"},
  {effect: "sepia:80"}
  ]}).toHtml();
jQuery (cloudinary-jquery 2.x):
Copy to clipboard
$.cloudinary.image("docs/sdk/go/maroon_hat", {transformation: [
  {quality: "auto"},
  {gravity: "face", height: 250, width: 250, crop: "fill"},
  {effect: "sepia:80"}
  ]})
React (@cloudinary/react 1.x):
Copy to clipboard
//React SDK transformations are created using @cloudinary/url-gen.
new CloudinaryImage("docs/sdk/go/maroon_hat")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(250)
      .height(250)
      .gravity(focusOn(face()))
  )
  .effect(sepia().level(80))
  .version(1639034832);
React (cloudinary-react 1.x):
Copy to clipboard
<Image publicId="docs/sdk/go/maroon_hat" >
  <Transformation quality="auto" />
  <Transformation gravity="face" height="250" width="250" crop="fill" />
  <Transformation effect="sepia:80" />
</Image>
Vue.js (cloudinary-vue 1.x):
Copy to clipboard
<cld-image public-id="docs/sdk/go/maroon_hat" >
  <cld-transformation quality="auto" />
  <cld-transformation gravity="face" height="250" width="250" crop="fill" />
  <cld-transformation effect="sepia:80" />
</cld-image>
Angular (@cloudinary/ng 1.x):
Copy to clipboard
//Angular SDK transformations are created using @cloudinary/url-gen.
new CloudinaryImage("docs/sdk/go/maroon_hat")
  .delivery(format(auto()))
  .delivery(quality(auto()))
  .resize(
    fill()
      .width(250)
      .height(250)
      .gravity(focusOn(face()))
  )
  .effect(sepia().level(80))
  .version(1639034832);
Angular (@cloudinary/angular-5.x 1.x (legacy)):
Copy to clipboard
<cl-image public-id="docs/sdk/go/maroon_hat" >
  <cl-transformation quality="auto">
  </cl-transformation>
  <cl-transformation gravity="face" height="250" width="250" crop="fill">
  </cl-transformation>
  <cl-transformation effect="sepia:80">
  </cl-transformation>
</cl-image>
.NET (CloudinaryDotNet 1.x):
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Quality("auto").Chain()
  .Gravity("face").Height(250).Width(250).Crop("fill").Chain()
  .Effect("sepia:80")).BuildImageTag("docs/sdk/go/maroon_hat")
iOS (cloudinary 3.x):
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation()
  .setQuality("auto").chain()
  .setGravity("face").setHeight(250).setWidth(250).setCrop("fill").chain()
  .setEffect("sepia:80")).generate("docs/sdk/go/maroon_hat")!, cloudinary: cloudinary)
Android (cloudinary-android 1.x):
Copy to clipboard
MediaManager.get().url().transformation(new Transformation()
  .quality("auto").chain()
  .gravity("face").height(250).width(250).crop("fill").chain()
  .effect("sepia:80")).generate("docs/sdk/go/maroon_hat");
Kotlin (kotlin-url-gen 1.x):
Copy to clipboard
cloudinary.image {
  publicId("docs/sdk/go/maroon_hat")
   delivery(Delivery.format(
  Format.auto()))
   delivery(Delivery.quality(
  Quality.auto()))
   resize(Resize.fill() { width(250)
 height(250)
   gravity(
  Gravity.focusOn(
  FocusOn.face()))
   })
   effect(Effect.sepia() { level(80) })
   version(1639034832) 
}.generate()
Generated asset

Note
Your URL may contain some letters and numbers at the end of it. This indicates the asset analytics.

Combining transformations

Cloudinary supports powerful transformations. You can even combine multiple transformations together as part of a single transformation request, e.g. crop an image and add a border. In certain cases you may want to perform additional transformations on the result of the previous transformation request.

To support multiple transformations in a transformation URL, you can include multiple transformation components, each separated by a '/'. Each transformation component is applied to the result of the previous one. In Go, applying multiple transformations is achieved by simply adding the next transformation to your transformation string, and then assigning that value to the Transformation parameter of your asset object. The following example first crops the original image to a specific set of custom coordinates and then transforms the result so it fills a 130x100 rectangle:

Original image before face recognition cropping Original image Fill cropping with 'faces' gravity Fill cropping with
'faces' gravity
Copy to clipboard
package main

import (
    "fmt"
    "github.com/cloudinary/cloudinary-go/v2"
)

func main() {

    // Add your Cloudinary credentials.
    cld, _ := cloudinary.NewFromParams(<cloud_name>, <api_key>, <api_secret>)

    // Instantiate an object for the image with public ID "black_coat_portrait" in folder "docs/sdk/go"
    img, err := cld.Image("docs/sdk/go/black_coat_portrait")
    if err != nil {
        fmt.Println("error")
    }

    // Add the transformation
    img.Transformation = "f_auto/q_auto/c_crop,h_350,w_300,x_1400,y_1200/c_fill,h_100,w_130"

    // Generate and print the delivery URL
    myURL, err := img.String()
    if err != nil {
        fmt.Println("error")
    }
    fmt.Println(myURL)

    //Output: https://res.cloudinary.com/demo/image/upload/f_auto/q_auto/c_crop,h_350,w_300,x_1400,y_1200/c_fill,h_100,w_130/v1/docs/sdk/go/black_coat_portrait
}

Apply common image transformations

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 Go.

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 shows an image cropped (c_fill) to a 350X350 square (w_350,h_350) while keeping focus on the faces (g_faces) in the image:

Copy to clipboard
// Instantiate an object for the image with public ID "family_video" in folder "docs/sdk/go"
img_fam, err := cld.Image("docs/sdk/go/family_video")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_fam.Transformation = "c_fill,g_faces,h_300,w_300"

// Generate and print the delivery URL
myURL, err := img_fam.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/image/upload/c_fill,g_faces,h_300,w_300/v1/docs/sdk/go/family_video
Original image before face recognition cropping Original image Fill cropping with 'faces' gravity Fill cropping with
'faces' gravity

For all the ways in which an image can be cropped, see the parameters under the c (crop) section of the Transformation URL Reference.

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

Converting to another image format

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

  • Specify the image's public ID with the desired extension.
  • Explicitly set the desired format using the Transformations 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:

Deliver a .jpg file in .png format:
Copy to clipboard
// Instantiate an object for the image with public ID "cloud_castle" in folder "docs/sdk/go"
img_fam, err := cld.Image("docs/sdk/go/cloud_castle")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_fam.Transformation = "f_png"

// Generate and print the delivery URL
myURL, err := img_fam.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/image/upload/f_png/v1/docs/sdk/go/cloud_castle
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 account settings):

Copy to clipboard
// Instantiate an object for the image with public ID "cloud_castle" in folder "docs/sdk/go"
img_fam, err := cld.Image("docs/sdk/go/cloud_castle")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_fam.Transformation = "f_auto"

// Generate and print the delivery URL
myURL, err := img_fam.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/image/upload/f_auto/v1/docs/sdk/go/cloud_castle

The above code generates a URL with the f_auto parameter:

For information about the relevant parameter, see the f (format) section of the Transformation URL Reference.

For more details, see:

Applying image effects and filters

You can select from a large variety of image effects, enhancements, and filters to apply to your images. The available effects include various 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 (e_cartoonify), rounding corners effect (r_max), and background color effect (b_lightblue), and then crops and pads (c_pad) the image down to a height of 300 pixels.

Copy to clipboard
// Instantiate an object for the image with public ID "actor" in folder "docs/sdk/go"
img_actor, err := cld.Image("docs/sdk/go/actor")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_actor.Transformation = "e_cartoonify/r_max/co_lightblue,e_outline:100/b_lightblue,c_pad,h_300"

// Generate and print the delivery URL
myURL, err := img_actor.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/image/upload/e_cartoonify/r_max/co_lightblue,e_outline:100/b_lightblue,c_pad,h_300/v1/docs/sdk/go/actor

An image with several transformation effects

For all the effects that can be applied to images, see the parameters under the e (effect) section of the Transformation URL Reference.

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 (l_nice_couple). The overlay photo is cropped using face detection (g_faces) with adjusted color saturation (e_saturation:50) and a vignette effect (e_vignette) applied. The word love is added in a pink, fancy font (l_text:Cookie_40_bold:Love,co_rgb:f08) and rotated to fit the design (a_20). A balloon graphic is also added (l_balloon). Additionally, the final image is cropped (c_crop,w_300,h_250,x_30) and the corners are rounded (r_60).

Copy to clipboard
//  Instantiate an object for the image with public ID "coffee_cup" in folder "docs/sdk/go"
img_coffee, err := cld.Image("docs/sdk/go/coffee_cup")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_coffee.Transformation = "c_fill,g_south,h_250,w_400/l_nice_couple/c_crop,fl_region_relative,g_faces,h_1.3,w_1.3/e_saturation:50/e_vignette/c_scale,w_100/r_max/fl_layer_apply,g_center,x_-20,y_20/l_balloon/c_scale,h_55/a_5/e_hue:-20/fl_layer_apply,x_30,y_5/co_rgb:f08,l_text:Cookie_40_bold:Love/a_20/fl_layer_apply,x_-45,y_44/c_crop,h_250,w_300,x_30/r_60"

// Generate and print the delivery URL
myURL, err := img_coffee.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/image/upload/c_fill,g_south,h_250,w_400/l_nice_couple/c_crop,fl_region_relative,g_faces,h_1.3,w_1.3/e_saturation:50/e_vignette/c_scale,w_100/r_max/fl_layer_apply,g_center,x_-20,y_20/l_balloon/c_scale,h_55/a_5/e_hue:-20/fl_layer_apply,x_30,y_5/co_rgb:f08,l_text:Cookie_40_bold:Love/a_20/fl_layer_apply,x_-45,y_44/c_crop,h_250,w_300,x_30/r_60/v1/docs/sdk/go/coffee_cup

An image with many transformations and overlays applied

For all ways in which you can apply overlays to images, see the parameters under the l (layer) section of the Transformation URL Reference.

For more details on adding image overlays, see Visual image effects and enhancements.

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 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 50% file size reduction (1.4MB vs. 784KB) with no visible change in quality.

Copy to clipboard
// Instantiate an object for the image with public ID "pond_reflect" in folder "docs/sdk/go"
img_lake, err := cld.Image("docs/sdk/go/pond_reflect")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
img_lake.Transformation = "f_auto/q_auto"

// Generate and print the delivery URL
myURL, err := img_lake.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)
// Output: https://res.cloudinary.com/demo/image/upload/f_auto/q_auto/v1/docs/sdk/go/pond_reflect

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

For all ways in which you can apply overlays to images, see the parameters under the q (quality) and f (format) sections of the Transformation URL Reference.

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

Apply common video transformations

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 video transformations with Go.

For a full list of all supported video transformations and their usage, see the Transformation URL API Reference.

For comprehensive explanations of how to implement a wide variety of transformations, see Video transformations.

Resizing and cropping videos

There are a variety of different ways to resize and/or crop your videos, and to control the area of the video that is preserved during a crop. The following examples:

  • crop the video to a specified dimension (c_crop,h_800,w_750) in a specified area (x_200,y_450)
  • resize the video to fit a specified height and width while padding the extra space (c_pad,h_320,w_480) with the original asset blurred playing in the background (b_blurred:400:15) to preserve the original aspect ratio.
Copy to clipboard
// Instantiate an object for the video with public ID "play_time" in folder "docs/sdk/go"
v_play_time, err := cld.Video("docs/sdk/go/play_time")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
v_play_time.Transformation = "b_blurred:400:15,c_pad,h_320,w_480"


// Generate and print the delivery URL
myURL, err := v_play_time.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/video/upload/b_blurred:400:15,c_pad,h_320,w_480/v1/docs/sdk/go/play_time
Original video Keep centered
c_crop,x_200,y_450
Pad with blurred video
c_pad,b_blurred:380:15

For all the ways in which a video can be cropped, see the parameters under the c (crop) section of the Transformation URL Reference that are marked as supported for video.

For details on all resizing and cropping options, see Resizing and cropping videos.

Concatenating videos

You can concatenate videos with a variety of options, including concatenating only a portions of the videos, concatenating a video at the beginning or end of another, creating custom transitions between concatenated videos, and concatenating an image to a video.

The following example shows two videos shortened to 5 seconds each (du_5), the main video starting with an offset of 1 second (so_1), with both videos uniformly resized (c_fill,h_200,w_300) and concatenated (fl_splice):

Copy to clipboard
// Instantiate an object for the video with public ID "horse_race" in folder "docs/sdk/go"
v_races, err := cld.Video("docs/sdk/go/horse_race")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
v_races.Transformation = "c_fill,h_300,w_450/du_5.0,so_1/fl_splice,l_video:swimming_race/c_fill,h_300,w_450/du_5.0/fl_layer_apply"

// Generate and print the delivery URL
myURL, err := v_races.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/video/upload/c_fill,h_300,w_450/du_5.0,so_1/fl_splice,l_video:swimming_race/c_fill,h_300,w_450/du_5.0/fl_layer_apply/v1/docs/sdk/go/horse_race

For a detailed description of how to concatenate videos, see the the splice parameter of the fl_flag section of the Transformation URL Reference.

For details on all the ways in which you can concatenate videos, see Concatenating media.

Trimming videos

When trimming your videos, you can determine when to start trimming, when to stop, and / or the duration of the trimmed video.

The following example shows trimming a video to the section that starts at 3.5 seconds (so_3.5) with a duration of 5 seconds (du_5):

Copy to clipboard
// Instantiate an object for the video with public ID "dog_mirror" in folder "docs/sdk/go"
v_dog, err := cld.Video("docs/sdk/go/dog_mirror")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
v_dog.Transformation = "c_scale,w_450/du_5.0,so_3.5"


// Generate and print the delivery URL
myURL, err := v_dog.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/video/upload/c_scale,w_450/du_5.0,so_3.5/v1/docs/sdk/go/dog_mirror

For all the ways in which a video can be trimmed, see the so (start_offset), du (duration), eo (end_offset) parameters the Transformation URL Reference that are marked as supported for video.

For more details on trimming videos, see Trimming videos.

Adding video overlays

You can add video, text, or image overlays onto your videos.

The following example shows one video scaled down (c_scale,w_100) and placed in the north-east corner (g_north_east) as an overlay (l_video:exercise2), starting 3 seconds after the main video starts playing (so_3):

Copy to clipboard
// Instantiate an object for the video with public ID "exercise1" in folder "docs/sdk/go"
v_exercise, err := cld.Video("docs/sdk/go/exercise1")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
v_exercise.Transformation = "c_scale,w_300/l_video:exercise2/c_fit,w_80/bo_2px_solid_blue/fl_layer_apply,g_north_east,so_2.0"

// Generate and print the delivery URL
myURL, err := v_exercise.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/video/upload/c_scale,w_300/l_video:exercise2/c_fit,w_80/bo_2px_solid_blue/fl_layer_apply,g_north_east,so_2.0/v1/docs/sdk/go/exercise1

For all the ways in which you can add video overlays, see the parameters under the l (layer) section of the Transformation URL Reference that are marked as supported for video.

For more details on video overlays, see Placing layers on videos.

Adding video effects

You can select from a large variety of video effects, enhancements, and filters to apply to your video. The available effects include speed, direction, and looping control, adding a progress indicator overlay, different transparency settings, AI generated preview, a variety of color balance and level effects, blurring, fading, and automatic improvement effects.

The following example shows a video that fades in and out (e_fade:2000/e_fade:-4000), loops twice (e_loop:2), and has a vignette filter applied (e_vignette:50):

Copy to clipboard
// Instantiate an object for the video with public ID "dog_garden" in folder "docs/sdk/go"
v_dogarden, err := cld.Video("docs/sdk/go/dog_garden")
if err != nil {
    fmt.Println("error")
}

// Add the transformation
v_dogarden.Transformation = "c_scale,w_400/du_10.0/e_vignette:50/e_fade:2000/e_fade:-4000/e_loop:2"

// Generate and print the delivery URL
myURL, err := v_dogarden.String()
if err != nil {
    fmt.Println("error")
}
fmt.Println(myURL)

// Output: https://res.cloudinary.com/demo/video/upload/c_scale,w_400/du_10.0/e_vignette:50/e_fade:2000/e_fade:-4000/e_loop:2/v1/docs/sdk/go/dog_garden

For all video effects, see the parameters under the e (effect) section of the Transformation URL Reference that are marked as supported for video.

For an in-depth review of all the available video effects, see Video Effects.

Try it out!

Here's the code in a fully functioning Go app. It's structured a little differently from above - each transformation example has its own Go file showing the imports and syntax required. Find those files in the go/src/lib folder of the project.

main.go imports all the functions to display the transformed images on a simple web page.

Experiment with images:

This code is also available in GitHub.

Experiment with videos:

This code is also available in GitHub

Related topics

✔️ Feedback sent!

Rate this page: