Image resizing and cropping

Responsive design and art direction generally requires displaying images and videos at a variety of sizes, often much smaller than the original.

If you deliver full size assets and rely on browser-side resizing (using CSS or HTML width and height attributes), users are forced to download unnecessarily large images and videos. Therefore, assets should always be delivered from the server at their final size.

When you use any of the Cloudinary resizing transformations, the sizing (scaling/cropping) is performed on the server side, and the image or video is always delivered to the browser at the requested size.

Here are some examples of different cropping techniques used on the same image. Click each image to see the URL parameters applied in each case:

Model wearing a denim jacket Original image Portrait crop centered on the model Focus on the
model in a
portrait crop
Thumbnail crop centered on the model's face Detect the
face for a
thumbnail crop
Banner image keeping the model's face in the crop Automatically determine what to keep in a banner crop

Tip
To help you learn about the different cropping and resizing modes, try out the interactive demo.

Resize dimensions

You can set the target dimensions of your resized image by specifying width, height, and/or the target aspect ratio as qualifiers of your resize transformation.

  • Using an integer value for w (width) or h (height) sets the new dimension to that number in pixels. For example, w_150 sets the width to exactly 150 pixels.
  • Using a decimal value for width or height sets the new dimension relative to the original dimension. For example, w_0.5 sets the width to half the original width.
  • Using ih or iw as values sets the dimension to the initial height or initial width of the original image respectively. For example, w_iw sets the width to the same value as the original width of the image. This may be useful when applying chained transformations or setting the dimensions of an overlay.
  • Aspect ratios are specified using the ar (aspect ratio) parameter, as follows:
    • a:b where a signifies the relative width and b the relative height (e.g., ar_4:3 or ar_16:9).
    • a decimal value representing the ratio of the width divided by the height (e.g., ar_1.33 or ar_2.5). 1.0 is a perfect square.
  • In most cases, you will specify both width and height or width/height along with an aspect ratio to define the exact required dimensions. However, in rare cases, you may choose to specify only one of these 3 resize qualifiers, and Cloudinary will automatically determine the missing dimension as follows:

    • If you provide only width or only height, then the other dimension is automatically calculated to deliver the original aspect ratio. For example, if your original asset is 400*600, then specifying c_crop,w_200 is the same as specifying c_crop,w_200,h_300. Supported for all resize and crop modes.
    • If you provide only the aspect ratio: If ar > 1, the original width is maintained and the height is cropped to deliver the requested ratio. If ar < 1, the original height is maintained, and the width is cropped accordingly. Supported for modes that support cropping out part of the image (crop, the fill modes, the pad modes, and thumb). Not supported for imagga_crop.

Note
If you want to resize only one dimension, and keep the other dimension at its original size (rather than the automatic determination described above), you can specify only width or only height, and add the fl_ignore_aspect_ratio flag qualifier.

Resize and crop modes

When changing the dimensions of an uploaded image by setting the image's height, width, and/or aspect ratio, you need to decide how to resize or crop the image to fit into the requested size. Use the c (crop/resize) parameter for selecting the crop/resize mode. Cloudinary supports the following image resize/crop modes:

Crop/resize mode Behavior
Cropping modes If the requested dimensions have a different aspect ratio than the original, these modes crop out part of the image.
fill Resizes the image to fill the specified dimensions without distortion. The image may be cropped as a result.
lfill Same as fill, but only scales down the image.
fill_pad Same as fill, but avoids excessive cropping by adding padding when needed. Supported only with automatic cropping.
crop Extracts a region of the specified dimensions from the original image without first resizing it.
thumb Creates a thumbnail of the image with the specified dimensions, based on a specified gravity. Scaling may occur.
Resize modes These modes adjust the size of the delivered image without cropping out any elements of the original image.
scale Resizes the image to the specified dimensions without necessarily retaining the original aspect ratio.
fit Resizes the image to fit inside the bounding box specified by the dimensions, maintaining the aspect ratio.
limit Same as fit, but only scales down the image.
mfit Same as fit, but only scales up the image.
pad Resizes the image to fit inside the bounding box specified by the dimensions, maintaining the aspect ratio, and applies padding if the resized image does not fill the whole area.
lpad Same as pad, but only scales down the image.
mpad Same as pad, but only scales up the image.
Add-on resize and crop modes These modes adjust the size and/or crop the image using an add-on.
imagga_scale Performs smart scaling, using the Imagga Crop and Scale add-on.
imagga_crop Performs smart cropping, using the Imagga Crop and Scale add-on.

Note
When creating dynamic delivery URLs, if you specify only the width and/or height parameters, but no cropping mode (no c_<mode>), the image is scaled to the new dimensions by default. However, there is no default cropping mode when using the Cloudinary SDK helper methods (see Embedding images in web pages), so a cropping mode must be explicitly set.

Some of the cropping modes keep only a certain part of the original image in the resulting image. By default, the center of the image is kept in the crop, but this is not always ideal. To keep the parts of the image that are important to you, you can use the gravity parameter. For example, you can specify to keep faces, text, or certain objects in the crop, or gravitate towards an automatically-determined area of interest. You can also guide the crop towards areas of your image defined by compass points, for example, north to keep the top part of the image, or south_east to keep the bottom-right part. Additionally, if you know the coordinates of the area you want to keep, you can explicitly specify these.

Resize and crop mode examples

The following examples show the same image resized to a width and height of 200 pixels, using different methods of resizing and cropping.

The original image is 640 x 426 pixels:

Shop front

Resizing the image to 200 x 200 pixels, using crop, scale, fill and pad results in the following images:

Square crop c_crop,h_200,w_200 Square scale c_scale,h_200,w_200
Square crop c_fill,h_200,w_200 Square pad b_black,c_pad,h_200,w_200

You could deliver the c_crop transformation shown above as follows:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>200, :width=>200, :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::crop()->width(200)
->height(200));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>200, "width"=>200, "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=200, width=200, crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 200, width: 200, crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(200).width(200).crop("crop")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(crop().width(200).height(200));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 200, width: 200, crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 200, width: 200, crop: "crop"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="200" width="200" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="200" width="200" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="200" width="200" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(200).Width(200).Crop("crop")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(200).setWidth(200).setCrop("crop")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(200).width(200).crop("crop")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.crop() { width(200)
 height(200) }) 
}.generate()

The following sections explain how each of the crop modes behave.

Tip
You can experiment with the various crop modes (in combination with different dimensions and gravity settings) using the interactive demo.

fill

The fill cropping mode creates an image with the exact specified dimensions, without distorting the image. This option first scales up or down as much as needed to at least fill both of the specified dimensions. If the requested aspect ratio is different than the original, cropping will occur on the dimension that exceeds the requested size after scaling. You can specify which part of the original image you want to keep if cropping occurs using the gravity parameter (set to center by default).

Example 1: Fill a specific area with an image

Fill a 250-pixel square with the models image:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>250, :width=>250, :crop=>"fill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::fill()->width(250)
->height(250));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>250, "width"=>250, "crop"=>"fill"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=250, width=250, crop="fill")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "fill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(250).width(250).crop("fill")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(fill().width(250).height(250));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 250, width: 250, crop: "fill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "fill"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="250" width="250" crop="fill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="250" width="250" crop="fill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="250" width="250" crop="fill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(250).Width(250).Crop("fill")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(250).setWidth(250).setCrop("fill")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(250).width(250).crop("fill")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.
Image filled to a width and height of 250 pixels

Example 2: Fill a specific area with a specific part of an image

Fill a 250-pixel square with the right-most part (gravity east) of the models image:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"1.0", :gravity=>"east", :width=>250, :crop=>"fill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::fill()->width(250)
->aspectRatio(1.0)
  ->gravity(
  Gravity::compass(
  Compass::east()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"1.0", "gravity"=>"east", "width"=>250, "crop"=>"fill"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="1.0", gravity="east", width=250, crop="fill")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", gravity: "east", width: 250, crop: "fill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").gravity("east").width(250).crop("fill")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  fill()
    .width(250)
    .aspectRatio("1.0")
    .gravity(compass("east"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "1.0", gravity: "east", width: 250, crop: "fill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", gravity: "east", width: 250, crop: "fill"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="1.0" gravity="east" width="250" crop="fill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="1.0" gravity="east" width="250" crop="fill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="1.0" gravity="east" width="250" crop="fill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Gravity("east").Width(250).Crop("fill")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setGravity("east").setWidth(250).setCrop("fill")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").gravity("east").width(250).crop("fill")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.
Image filled to a width and height of 250 pixels with east gravity

lfill (limit fill)

The lfill cropping mode behaves the same as the fill mode, but only if the original image is larger than the specified resolution limits, in which case the image is scaled down to fill the specified dimensions without distorting the image, and then the dimension that exceeds the request is cropped. If the original dimensions are smaller than the requested size, it is not resized at all. This prevents upscaling. You can specify which part of the original image you want to keep if cropping occurs using the gravity parameter (set to center by default).

Example 1: Scale down and fill an area defined by width and height

Fill a 150 x 200 pixel area with the models image and limiting the size to no larger than the original image:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>200, :width=>150, :crop=>"lfill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::limitFill()->width(150)
->height(200));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>200, "width"=>150, "crop"=>"lfill"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=200, width=150, crop="lfill")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 200, width: 150, crop: "lfill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(200).width(150).crop("lfill")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  limitFill().width(150).height(200)
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 200, width: 150, crop: "lfill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 200, width: 150, crop: "lfill"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="200" width="150" crop="lfill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="200" width="150" crop="lfill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="200" width="150" crop="lfill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(200).Width(150).Crop("lfill")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(200).setWidth(150).setCrop("lfill")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(200).width(150).crop("lfill")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.limitFill() { width(150)
 height(200) }) 
}.generate()
Image lfilled to a width of 150 and a height of 200 pixels

Example 2: Scale down and fill an area defined by aspect ratio and height

Scale down the models image to fill a 200-pixel square defined by aspect ratio and height:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"1.0", :height=>200, :crop=>"lfill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::limitFill()->height(200)
->aspectRatio(1.0));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"1.0", "height"=>200, "crop"=>"lfill"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="1.0", height=200, crop="lfill")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 200, crop: "lfill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").height(200).crop("lfill")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  limitFill().height(200).aspectRatio("1.0")
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "1.0", height: 200, crop: "lfill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 200, crop: "lfill"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="1.0" height="200" crop="lfill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="1.0" height="200" crop="lfill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="1.0" height="200" crop="lfill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Height(200).Crop("lfill")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setHeight(200).setCrop("lfill")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").height(200).crop("lfill")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.limitFill() { height(200)
 aspectRatio(1.0F) }) 
}.generate()
Image lfilled to a width of 150 and a height of 200 pixels

fill_pad

The fill_pad cropping mode tries to prevent a "bad crop" by first attempting to use the fill mode, but adding padding if it is determined that more of the original image needs to be included in the final image. Especially useful if the aspect ratio of the delivered image is considerably different from the original's aspect ratio. Only supported in conjunction with Automatic cropping (g_auto), and not supported for animated images.

Example: Compare standard fill mode with fill_pad mode

Compare the lady image delivered as an 80 x 400 image using the fill mode on the left, with the fill_pad mode on the right:

fill fill fill_pad fill_pad

Ruby:
Copy to clipboard
cl_image_tag("lady.jpg", :background=>"auto", :gravity=>"auto", :height=>400, :width=>80, :crop=>"fill_pad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('lady.jpg'))
  ->resize(Resize::fillPad()->width(80)
->height(400)
  ->gravity(
  Gravity::autoGravity())
  ->background(
  Background::auto())
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("lady.jpg", array("background"=>"auto", "gravity"=>"auto", "height"=>400, "width"=>80, "crop"=>"fill_pad"))
Python:
Copy to clipboard
CloudinaryImage("lady.jpg").image(background="auto", gravity="auto", height=400, width=80, crop="fill_pad")
Node.js:
Copy to clipboard
cloudinary.image("lady.jpg", {background: "auto", gravity: "auto", height: 400, width: 80, crop: "fill_pad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().background("auto").gravity("auto").height(400).width(80).crop("fill_pad")).imageTag("lady.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("lady.jpg").resize(
  fillPad()
    .width(80)
    .height(400)
    .gravity(autoGravity())
    .background(auto())
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('lady.jpg', {background: "auto", gravity: "auto", height: 400, width: 80, crop: "fill_pad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("lady.jpg", {background: "auto", gravity: "auto", height: 400, width: 80, crop: "fill_pad"})
React:
Copy to clipboard
<Image publicId="lady.jpg" >
  <Transformation background="auto" gravity="auto" height="400" width="80" crop="fill_pad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="lady.jpg" >
  <cld-transformation background="auto" gravity="auto" height="400" width="80" crop="fill_pad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="lady.jpg" >
  <cl-transformation background="auto" gravity="auto" height="400" width="80" crop="fill_pad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Background("auto").Gravity("auto").Height(400).Width(80).Crop("fill_pad")).BuildImageTag("lady.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setBackground("auto").setGravity("auto").setHeight(400).setWidth(80).setCrop("fill_pad")).generate("lady.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().background("auto").gravity("auto").height(400).width(80).crop("fill_pad")).generate("lady.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.

crop

The crop cropping mode extracts a region of the specified dimensions or a detected object from the original image. No scaling is applied, so if you specify dimensions, applying the crop mode to the same image of different resolutions can provide very different results. You can specify the gravity parameter to select which area or object to extract, or use fixed coordinates cropping.

Example 1: Crop an image to specified dimensions (width and height)

Crop the models image to a width of 200 pixels and a height of 150 pixels, with northwest gravity:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :gravity=>"north_west", :height=>150, :width=>200, :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::crop()->width(200)
->height(150)
  ->gravity(
  Gravity::compass(
  Compass::northWest()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("gravity"=>"north_west", "height"=>150, "width"=>200, "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(gravity="north_west", height=150, width=200, crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {gravity: "north_west", height: 150, width: 200, crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("north_west").height(150).width(200).crop("crop")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  crop()
    .width(200)
    .height(150)
    .gravity(compass("north_west"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {gravity: "north_west", height: 150, width: 200, crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {gravity: "north_west", height: 150, width: 200, crop: "crop"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation gravity="north_west" height="150" width="200" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation gravity="north_west" height="150" width="200" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation gravity="north_west" height="150" width="200" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("north_west").Height(150).Width(200).Crop("crop")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("north_west").setHeight(150).setWidth(200).setCrop("crop")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("north_west").height(150).width(200).crop("crop")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.crop() { width(200)
 height(150)
   gravity(
  Gravity.compass(
  Compass.northWest()))
   }) 
}.generate()
Image cropped to 100x150 with west gravity

Example 2: Crop an image to specified dimensions (aspect ratio and width)

Crop the models image to a width of 450 pixels and an aspect ratio of 2.5:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"2.5", :width=>450, :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::crop()->width(450)
->aspectRatio(2.5));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"2.5", "width"=>450, "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="2.5", width=450, crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "2.5", width: 450, crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("2.5").width(450).crop("crop")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  crop().width(450).aspectRatio(2.5)
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "2.5", width: 450, crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "2.5", width: 450, crop: "crop"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="2.5" width="450" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="2.5" width="450" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="2.5" width="450" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("2.5").Width(450).Crop("crop")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("2.5").setWidth(450).setCrop("crop")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("2.5").width(450).crop("crop")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.crop() { width(450)
 aspectRatio(2.5F) }) 
}.generate()
Image cropped to 100x150 with west gravity

Example 3: Crop an image to keep only the face

Crop the woman image without specifying dimensions, to keep only the face.

Ruby:
Copy to clipboard
cl_image_tag("woman.jpg", :gravity=>"face", :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('woman.jpg'))
  ->resize(Resize::crop()
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("woman.jpg", array("gravity"=>"face", "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("woman.jpg").image(gravity="face", crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("woman.jpg", {gravity: "face", crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("face").crop("crop")).imageTag("woman.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("woman.jpg").resize(crop().gravity(focusOn(face())));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('woman.jpg', {gravity: "face", crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("woman.jpg", {gravity: "face", crop: "crop"})
React:
Copy to clipboard
<Image publicId="woman.jpg" >
  <Transformation gravity="face" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="woman.jpg" >
  <cld-transformation gravity="face" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="woman.jpg" >
  <cl-transformation gravity="face" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("face").Crop("crop")).BuildImageTag("woman.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("face").setCrop("crop")).generate("woman.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("face").crop("crop")).generate("woman.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("woman.jpg")
   resize(Resize.crop() {
   gravity(
  Gravity.focusOn(
  FocusOn.face()))
   }) 
}.generate()
Image cropped to a face

Example 4: Crop an image to keep only the largest image in a composition

Use the cld-decompose_tile special gravity position to crop an image composed of many images, such as this, keeping only the largest "tile":

Image of several bird tiles

Ruby:
Copy to clipboard
cl_image_tag("docs/tiled-birds.jpg", :gravity=>"cld-decompose_tile", :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/tiled-birds.jpg'))
  ->resize(Resize::crop()
  ->gravity(
  Gravity::focusOn(
  FocusOn::cldDecomposeTile()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/tiled-birds.jpg", array("gravity"=>"cld-decompose_tile", "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("docs/tiled-birds.jpg").image(gravity="cld-decompose_tile", crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("docs/tiled-birds.jpg", {gravity: "cld-decompose_tile", crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("cld-decompose_tile").crop("crop")).imageTag("docs/tiled-birds.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/tiled-birds.jpg").resize(
  crop().gravity(focusOn(cldDecomposeTile()))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/tiled-birds.jpg', {gravity: "cld-decompose_tile", crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/tiled-birds.jpg", {gravity: "cld-decompose_tile", crop: "crop"})
React:
Copy to clipboard
<Image publicId="docs/tiled-birds.jpg" >
  <Transformation gravity="cld-decompose_tile" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/tiled-birds.jpg" >
  <cld-transformation gravity="cld-decompose_tile" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/tiled-birds.jpg" >
  <cl-transformation gravity="cld-decompose_tile" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("cld-decompose_tile").Crop("crop")).BuildImageTag("docs/tiled-birds.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("cld-decompose_tile").setCrop("crop")).generate("docs/tiled-birds.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("cld-decompose_tile").crop("crop")).generate("docs/tiled-birds.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/tiled-birds.jpg")
   resize(Resize.crop() {
   gravity(
  Gravity.focusOn(
  FocusOn.cldDecomposeTile()))
   }) 
}.generate()
Crop to the largest tile

Fixed coordinates cropping

You can specify a region of the original image to crop by giving the x and y coordinates of the top left corner of the region together with the width and height of the region. You can also use percentage based numbers instead of the exact coordinates for x, y, w and h (e.g., 0.5 for 50%) . Use this method when you know beforehand what the correct absolute cropping coordinates are, as in when your users manually select the region to crop out of the original image.

For example, the following image shows many white sheep and one brown sheep.

Ruby:
Copy to clipboard
cl_image_tag("brown_sheep.jpg")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('brown_sheep.jpg'));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("brown_sheep.jpg")
Python:
Copy to clipboard
CloudinaryImage("brown_sheep.jpg").image()
Node.js:
Copy to clipboard
cloudinary.image("brown_sheep.jpg")
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().imageTag("brown_sheep.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("brown_sheep.jpg");
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('brown_sheep.jpg').toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("brown_sheep.jpg")
React:
Copy to clipboard
<Image publicId="brown_sheep.jpg" >

</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="brown_sheep.jpg" >

</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="brown_sheep.jpg" >

</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.BuildImageTag("brown_sheep.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().generate("brown_sheep.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().generate("brown_sheep.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("brown_sheep.jpg") 
}.generate()
Original image of brown_sheep

To resize the picture so that only the brown sheep is visible, the image is cropped to a 300x200 region starting at the coordinate x = 355 and y = 410:

Ruby:
Copy to clipboard
cl_image_tag("brown_sheep.jpg", :height=>200, :width=>300, :x=>355, :y=>410, :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('brown_sheep.jpg'))
  ->resize(Resize::crop()->width(300)
->height(200)
->x(355)
->y(410));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("brown_sheep.jpg", array("height"=>200, "width"=>300, "x"=>355, "y"=>410, "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("brown_sheep.jpg").image(height=200, width=300, x=355, y=410, crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("brown_sheep.jpg", {height: 200, width: 300, x: 355, y: 410, crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(200).width(300).x(355).y(410).crop("crop")).imageTag("brown_sheep.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("brown_sheep.jpg").resize(
  crop().width(300).height(200).x(355).y(410)
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('brown_sheep.jpg', {height: 200, width: 300, x: 355, y: 410, crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("brown_sheep.jpg", {height: 200, width: 300, x: 355, y: 410, crop: "crop"})
React:
Copy to clipboard
<Image publicId="brown_sheep.jpg" >
  <Transformation height="200" width="300" x="355" y="410" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="brown_sheep.jpg" >
  <cld-transformation height="200" width="300" x="355" y="410" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="brown_sheep.jpg" >
  <cl-transformation height="200" width="300" x="355" y="410" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(200).Width(300).X(355).Y(410).Crop("crop")).BuildImageTag("brown_sheep.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(200).setWidth(300).setX(355).setY(410).setCrop("crop")).generate("brown_sheep.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(200).width(300).x(355).y(410).crop("crop")).generate("brown_sheep.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("brown_sheep.jpg")
   resize(Resize.crop() { width(300)
 height(200)
 x(355)
 y(410) }) 
}.generate()
300x200 image generated with fixed-coordinates cropping

The image can be further resized with chained transformations. For example, the 300x200 cropped version above, also scaled down to half that size:

Ruby:
Copy to clipboard
cl_image_tag("brown_sheep.jpg", :transformation=>[
  {:height=>200, :width=>300, :x=>355, :y=>410, :crop=>"crop"},
  {:width=>150, :crop=>"scale"}
  ])
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('brown_sheep.jpg'))
  ->resize(Resize::crop()->width(300)
->height(200)
->x(355)
->y(410))
  ->resize(Resize::scale()->width(150));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("brown_sheep.jpg", array("transformation"=>array(
  array("height"=>200, "width"=>300, "x"=>355, "y"=>410, "crop"=>"crop"),
  array("width"=>150, "crop"=>"scale")
  )))
Python:
Copy to clipboard
CloudinaryImage("brown_sheep.jpg").image(transformation=[
  {'height': 200, 'width': 300, 'x': 355, 'y': 410, 'crop': "crop"},
  {'width': 150, 'crop': "scale"}
  ])
Node.js:
Copy to clipboard
cloudinary.image("brown_sheep.jpg", {transformation: [
  {height: 200, width: 300, x: 355, y: 410, crop: "crop"},
  {width: 150, crop: "scale"}
  ]})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation()
  .height(200).width(300).x(355).y(410).crop("crop").chain()
  .width(150).crop("scale")).imageTag("brown_sheep.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("brown_sheep.jpg")
  .resize(crop().width(300).height(200).x(355).y(410))
  .resize(scale().width(150));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('brown_sheep.jpg', {transformation: [
  {height: 200, width: 300, x: 355, y: 410, crop: "crop"},
  {width: 150, crop: "scale"}
  ]}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("brown_sheep.jpg", {transformation: [
  {height: 200, width: 300, x: 355, y: 410, crop: "crop"},
  {width: 150, crop: "scale"}
  ]})
React:
Copy to clipboard
<Image publicId="brown_sheep.jpg" >
  <Transformation height="200" width="300" x="355" y="410" crop="crop" />
  <Transformation width="150" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="brown_sheep.jpg" >
  <cld-transformation height="200" width="300" x="355" y="410" crop="crop" />
  <cld-transformation width="150" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="brown_sheep.jpg" >
  <cl-transformation height="200" width="300" x="355" y="410" crop="crop">
  </cl-transformation>
  <cl-transformation width="150" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Height(200).Width(300).X(355).Y(410).Crop("crop").Chain()
  .Width(150).Crop("scale")).BuildImageTag("brown_sheep.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation()
  .setHeight(200).setWidth(300).setX(355).setY(410).setCrop("crop").chain()
  .setWidth(150).setCrop("scale")).generate("brown_sheep.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation()
  .height(200).width(300).x(355).y(410).crop("crop").chain()
  .width(150).crop("scale")).generate("brown_sheep.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("brown_sheep.jpg")
   resize(Resize.crop() { width(300)
 height(200)
 x(355)
 y(410) })
   resize(Resize.scale() { width(150) }) 
}.generate()
fixed-coordinates cropped image also scaled down

thumb

The thumb cropping mode is specifically used for creating image thumbnails from either face or custom coordinates, and must always be accompanied by the gravity parameter set to one of the face detection or custom values. This cropping mode generates a thumbnail of an image with the exact specified dimensions and with the original proportions retained, but the resulting image might be scaled to fit in the specified dimensions. You can specify the zoom parameter to determine how much to scale the resulting image within the specified dimensions.

Example 1: Crop to a thumbnail of a face

Create a 150 x 150 thumbnail with face detection, of the woman image. Below you can see the original image as well as the face detection based thumbnail:

Original photo for face detection

Ruby:
Copy to clipboard
cl_image_tag("woman.jpg", :gravity=>"face", :height=>150, :width=>150, :crop=>"thumb")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('woman.jpg'))
  ->resize(Resize::thumbnail()->width(150)
->height(150)
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("woman.jpg", array("gravity"=>"face", "height"=>150, "width"=>150, "crop"=>"thumb"))
Python:
Copy to clipboard
CloudinaryImage("woman.jpg").image(gravity="face", height=150, width=150, crop="thumb")
Node.js:
Copy to clipboard
cloudinary.image("woman.jpg", {gravity: "face", height: 150, width: 150, crop: "thumb"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("face").height(150).width(150).crop("thumb")).imageTag("woman.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("woman.jpg").resize(
  thumbnail()
    .width(150)
    .height(150)
    .gravity(focusOn(face()))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('woman.jpg', {gravity: "face", height: 150, width: 150, crop: "thumb"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("woman.jpg", {gravity: "face", height: 150, width: 150, crop: "thumb"})
React:
Copy to clipboard
<Image publicId="woman.jpg" >
  <Transformation gravity="face" height="150" width="150" crop="thumb" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="woman.jpg" >
  <cld-transformation gravity="face" height="150" width="150" crop="thumb" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="woman.jpg" >
  <cl-transformation gravity="face" height="150" width="150" crop="thumb">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("face").Height(150).Width(150).Crop("thumb")).BuildImageTag("woman.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("face").setHeight(150).setWidth(150).setCrop("thumb")).generate("woman.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("face").height(150).width(150).crop("thumb")).generate("woman.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("woman.jpg")
   resize(Resize.thumbnail() { width(150)
 height(150)
   gravity(
  Gravity.focusOn(
  FocusOn.face()))
   }) 
}.generate()
150x150 thumbnail with face detection

Example 2: Crop to a thumbnail of a face using aspect ratio and zoom

Create a 150-pixel high thumbnail with aspect ratio 5:6 and face detection of the woman image, zoomed out by 75%.

Ruby:
Copy to clipboard
cl_image_tag("woman.jpg", :aspect_ratio=>"5:6", :gravity=>"face", :height=>150, :zoom=>0.75, :crop=>"thumb")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('woman.jpg'))
  ->resize(Resize::thumbnail()->height(150)
->aspectRatio("5:6")
->zoom(0.75)
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("woman.jpg", array("aspect_ratio"=>"5:6", "gravity"=>"face", "height"=>150, "zoom"=>"0.75", "crop"=>"thumb"))
Python:
Copy to clipboard
CloudinaryImage("woman.jpg").image(aspect_ratio="5:6", gravity="face", height=150, zoom="0.75", crop="thumb")
Node.js:
Copy to clipboard
cloudinary.image("woman.jpg", {aspect_ratio: "5:6", gravity: "face", height: 150, zoom: "0.75", crop: "thumb"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("5:6").gravity("face").height(150).zoom(0.75).crop("thumb")).imageTag("woman.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("woman.jpg").resize(
  thumbnail()
    .height(150)
    .aspectRatio("5:6")
    .zoom(0.75)
    .gravity(focusOn(face()))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('woman.jpg', {aspectRatio: "5:6", gravity: "face", height: 150, zoom: "0.75", crop: "thumb"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("woman.jpg", {aspect_ratio: "5:6", gravity: "face", height: 150, zoom: "0.75", crop: "thumb"})
React:
Copy to clipboard
<Image publicId="woman.jpg" >
  <Transformation aspectRatio="5:6" gravity="face" height="150" zoom="0.75" crop="thumb" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="woman.jpg" >
  <cld-transformation aspect-ratio="5:6" gravity="face" height="150" zoom="0.75" crop="thumb" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="woman.jpg" >
  <cl-transformation aspect-ratio="5:6" gravity="face" height="150" zoom="0.75" crop="thumb">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("5:6").Gravity("face").Height(150).Zoom(0.75).Crop("thumb")).BuildImageTag("woman.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("5:6").setGravity("face").setHeight(150).setZoom(0.75).setCrop("thumb")).generate("woman.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("5:6").gravity("face").height(150).zoom(0.75).crop("thumb")).generate("woman.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("woman.jpg")
   resize(Resize.thumbnail() { height(150)
 aspectRatio("5:6")
 zoom(0.75F)
   gravity(
  Gravity.focusOn(
  FocusOn.face()))
   }) 
}.generate()
150-pixel high thumbnail with face detection zoomed out

scale

The scale resize mode changes the size of the image exactly to the specified dimensions without necessarily retaining the original aspect ratio: all original image parts are visible but might be stretched or shrunk. If only the width or height is specified, then the image is scaled to the new dimension while retaining the original aspect ratio, unless you also include the ignore_aspect_ratio flag.

Example 1: Scale while maintaining aspect ratio

Scale the models image to a width of 150 pixels (maintains the aspect ratio by default):

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :width=>150, :crop=>"scale")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::scale()->width(150));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("width"=>150, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(width=150, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {width: 150, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(150).crop("scale")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(scale().width(150));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {width: 150, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {width: 150, crop: "scale"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation width="150" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation width="150" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation width="150" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(150).Crop("scale")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(150).setCrop("scale")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(150).crop("scale")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.scale() { width(150) }) 
}.generate()
Image scaled to a width of 150 pixels

Example 2: Scale without maintaining aspect ratio

Scale the models image to a width and height of 150 pixels without maintaining the aspect ratio:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>150, :width=>150, :crop=>"scale")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::scale()->width(150)
->height(150));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>150, "width"=>150, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=150, width=150, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 150, width: 150, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(150).width(150).crop("scale")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(scale().width(150).height(150));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 150, width: 150, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 150, width: 150, crop: "scale"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="150" width="150" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="150" width="150" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="150" width="150" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(150).Width(150).Crop("scale")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(150).setWidth(150).setCrop("scale")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(150).width(150).crop("scale")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.scale() { width(150)
 height(150) }) 
}.generate()
Image scaled to a width and height of 150 pixels

Example 3: Scale by a percentage

Scale the models image to a width of 25% (maintains the aspect ratio by default):

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :width=>0.25, :crop=>"scale")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::scale()->width(0.25));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("width"=>"0.25", "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(width="0.25", crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {width: "0.25", crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(0.25).crop("scale")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(scale().width(0.25));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {width: "0.25", crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {width: "0.25", crop: "scale"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation width="0.25" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation width="0.25" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation width="0.25" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(0.25).Crop("scale")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(0.25).setCrop("scale")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(0.25).crop("scale")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.scale() { width(0.25F) }) 
}.generate()
Image scaled to a width of 25%

Example 4: Scale by setting the aspect ratio and width

Scale the models image to a width of 100, changing the aspect ratio to 1:2:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"1:2", :width=>100, :crop=>"scale")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::scale()->width(100)
->aspectRatio("1:2"));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"1:2", "width"=>100, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="1:2", width=100, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "1:2", width: 100, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1:2").width(100).crop("scale")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  scale().width(100).aspectRatio("1:2")
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "1:2", width: 100, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "1:2", width: 100, crop: "scale"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="1:2" width="100" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="1:2" width="100" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="1:2" width="100" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1:2").Width(100).Crop("scale")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1:2").setWidth(100).setCrop("scale")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1:2").width(100).crop("scale")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.scale() { width(100)
 aspectRatio("1:2") }) 
}.generate()
Image scaled using aspect ratio

Example 5: Scale in one dimension ignoring the aspect ratio

Scale the models image to a height of 150, ignoring the aspect ratio:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :flags=>"ignore_aspect_ratio", :height=>150, :crop=>"scale")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::scale()->height(150)
  ->aspectRatio(
  AspectRatio::ignoreInitialAspectRatio())
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("flags"=>"ignore_aspect_ratio", "height"=>150, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(flags="ignore_aspect_ratio", height=150, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {flags: "ignore_aspect_ratio", height: 150, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().flags("ignore_aspect_ratio").height(150).crop("scale")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  scale()
    .height(150)
    .aspectRatio(ignoreInitialAspectRatio())
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {flags: "ignore_aspect_ratio", height: 150, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {flags: "ignore_aspect_ratio", height: 150, crop: "scale"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation flags="ignore_aspect_ratio" height="150" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation flags="ignore_aspect_ratio" height="150" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation flags="ignore_aspect_ratio" height="150" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Flags("ignore_aspect_ratio").Height(150).Crop("scale")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setFlags("ignore_aspect_ratio").setHeight(150).setCrop("scale")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().flags("ignore_aspect_ratio").height(150).crop("scale")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.scale() { height(150)
   aspectRatio(
  AspectRatio.ignoreInitialAspectRatio())
   }) 
}.generate()
Image scaled ignoring aspect ratio

fit

The fit resize mode resizes the image so that it takes up as much space as possible within a bounding box defined by the specified dimensions. The original aspect ratio is retained and all of the original image is visible.

Example 1: Fit the image inside a square bounding box

Resize the models image to fit within a width and height of 250 pixels while retaining the aspect ratio:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>250, :width=>250, :crop=>"fit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::fit()->width(250)
->height(250));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>250, "width"=>250, "crop"=>"fit"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=250, width=250, crop="fit")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "fit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(250).width(250).crop("fit")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(fit().width(250).height(250));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 250, width: 250, crop: "fit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "fit"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="250" width="250" crop="fit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="250" width="250" crop="fit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="250" width="250" crop="fit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(250).Width(250).Crop("fit")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(250).setWidth(250).setCrop("fit")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(250).width(250).crop("fit")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.fit() { width(250)
 height(250) }) 
}.generate()
Image fit to a width and height of 250 pixels

Example 2: Use aspect ratio to define a bounding box

Resize the models image to fit within a 150-pixel square defined by aspect ratio and height:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"1.0", :height=>150, :crop=>"fit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::fit()->height(150)
->aspectRatio(1.0));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"1.0", "height"=>150, "crop"=>"fit"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="1.0", height=150, crop="fit")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 150, crop: "fit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("fit")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  fit().height(150).aspectRatio("1.0")
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "1.0", height: 150, crop: "fit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 150, crop: "fit"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="1.0" height="150" crop="fit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="1.0" height="150" crop="fit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="1.0" height="150" crop="fit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Height(150).Crop("fit")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setHeight(150).setCrop("fit")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("fit")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.fit() { height(150)
 aspectRatio(1.0F) }) 
}.generate()
Image fit to a 150-pixel square

limit

The limit resize mode behaves the same as the fit mode but only if the original image is larger than the specified limit (width and height), in which case the image is scaled down so that it takes up as much space as possible within a bounding box defined by the specified dimensions. The original aspect ratio is retained and all of the original image is visible. This mode doesn't scale up the image if your requested dimensions are larger than the original image's.

Example 1: Limit the size of the image to a square bounding box

Limit the models image to a width and height of 250 pixels while retaining the aspect ratio:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>250, :width=>250, :crop=>"limit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::limitFit()->width(250)
->height(250));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>250, "width"=>250, "crop"=>"limit"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=250, width=250, crop="limit")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "limit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(250).width(250).crop("limit")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  limitFit().width(250).height(250)
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 250, width: 250, crop: "limit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "limit"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="250" width="250" crop="limit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="250" width="250" crop="limit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="250" width="250" crop="limit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(250).Width(250).Crop("limit")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(250).setWidth(250).setCrop("limit")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(250).width(250).crop("limit")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.limitFit() { width(250)
 height(250) }) 
}.generate()
Image limited to a width and height of 250 pixels

Example 2: Use aspect ratio to define a limiting bounding box

Limit the models image to a 150-pixel square defined by aspect ratio and height:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"1.0", :height=>150, :crop=>"limit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::limitFit()->height(150)
->aspectRatio(1.0));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"1.0", "height"=>150, "crop"=>"limit"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="1.0", height=150, crop="limit")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 150, crop: "limit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("limit")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  limitFit().height(150).aspectRatio("1.0")
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "1.0", height: 150, crop: "limit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "1.0", height: 150, crop: "limit"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="1.0" height="150" crop="limit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="1.0" height="150" crop="limit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="1.0" height="150" crop="limit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Height(150).Crop("limit")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setHeight(150).setCrop("limit")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("limit")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.limitFit() { height(150)
 aspectRatio(1.0F) }) 
}.generate()
Image limited to a 150-pixel square

mfit (minimum fit)

The mfit resize mode behaves the same as the fit mode but only if the original image is smaller than the specified minimum (width and height), in which case the image is scaled up so that it takes up as much space as possible within a bounding box defined by the specified dimensions. The original aspect ratio is retained and all of the original image is visible. This mode doesn't scale down the image if your requested dimensions are smaller than the original image's.

Example 1: Ensure an image fits a minimum bounding box

Fit the models image to a minimum width and height of 250 pixels while retaining the aspect ratio. This results in the original larger image being delivered:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :height=>250, :width=>250, :crop=>"mfit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::minimumFit()->width(250)
->height(250));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("height"=>250, "width"=>250, "crop"=>"mfit"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(height=250, width=250, crop="mfit")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "mfit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(250).width(250).crop("mfit")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  minimumFit().width(250).height(250)
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {height: 250, width: 250, crop: "mfit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {height: 250, width: 250, crop: "mfit"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation height="250" width="250" crop="mfit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation height="250" width="250" crop="mfit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation height="250" width="250" crop="mfit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(250).Width(250).Crop("mfit")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(250).setWidth(250).setCrop("mfit")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(250).width(250).crop("mfit")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.minimumFit() { width(250)
 height(250) }) 
}.generate()
Image mfit to a width and height of 250 pixels

Example 2: Use aspect ratio to define a minimum size

Scale up the 100-pixel wide sample_100 image to fit a 150-pixel square defined by aspect ratio and height.

Ruby:
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", :aspect_ratio=>"1.0", :height=>150, :crop=>"mfit")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/sample_100.jpg'))
  ->resize(Resize::minimumFit()->height(150)
->aspectRatio(1.0));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", array("aspect_ratio"=>"1.0", "height"=>150, "crop"=>"mfit"))
Python:
Copy to clipboard
CloudinaryImage("docs/sample_100.jpg").image(aspect_ratio="1.0", height=150, crop="mfit")
Node.js:
Copy to clipboard
cloudinary.image("docs/sample_100.jpg", {aspect_ratio: "1.0", height: 150, crop: "mfit"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("mfit")).imageTag("docs/sample_100.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/sample_100.jpg").resize(
  minimumFit().height(150).aspectRatio("1.0")
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/sample_100.jpg', {aspectRatio: "1.0", height: 150, crop: "mfit"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/sample_100.jpg", {aspect_ratio: "1.0", height: 150, crop: "mfit"})
React:
Copy to clipboard
<Image publicId="docs/sample_100.jpg" >
  <Transformation aspectRatio="1.0" height="150" crop="mfit" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/sample_100.jpg" >
  <cld-transformation aspect-ratio="1.0" height="150" crop="mfit" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/sample_100.jpg" >
  <cl-transformation aspect-ratio="1.0" height="150" crop="mfit">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Height(150).Crop("mfit")).BuildImageTag("docs/sample_100.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setHeight(150).setCrop("mfit")).generate("docs/sample_100.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").height(150).crop("mfit")).generate("docs/sample_100.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/sample_100.jpg")
   resize(Resize.minimumFit() { height(150)
 aspectRatio(1.0F) }) 
}.generate()
Image mfit to a 150-pixel square

pad

The pad resize mode resizes the image to fill the specified dimensions while retaining the original aspect ratio and with all of the original image visible. If the proportions of the original image do not match the specified dimensions, padding is added to the image to reach the required size. You can also specify where the original image is placed by using the gravity parameter (set to center by default), and specify the color of the background in the case that padding is added.

Example 1: Resize and pad an image to fill the square defined by width and height

Resize and pad the models image with a black background to a width and height of 250 pixels:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :background=>"black", :height=>250, :width=>250, :crop=>"pad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::pad()->width(250)
->height(250)
  ->background(
  Background::color(Color::BLACK))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("background"=>"black", "height"=>250, "width"=>250, "crop"=>"pad"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(background="black", height=250, width=250, crop="pad")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {background: "black", height: 250, width: 250, crop: "pad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().background("black").height(250).width(250).crop("pad")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  pad()
    .width(250)
    .height(250)
    .background(color("black"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {background: "black", height: 250, width: 250, crop: "pad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {background: "black", height: 250, width: 250, crop: "pad"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation background="black" height="250" width="250" crop="pad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation background="black" height="250" width="250" crop="pad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation background="black" height="250" width="250" crop="pad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Background("black").Height(250).Width(250).Crop("pad")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setBackground("black").setHeight(250).setWidth(250).setCrop("pad")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().background("black").height(250).width(250).crop("pad")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.pad() { width(250)
 height(250)
   background(
  Background.color(Color.BLACK))
   }) 
}.generate()
Image padded to a width and height of 250 pixels

Example 2: Resize and pad an image to fill the rectangle defined by aspect ratio

Resize and pad the models image with a black background to a rectangle of height of 150 pixels, and aspect ratio 2:1:

Ruby:
Copy to clipboard
cl_image_tag("docs/models.jpg", :aspect_ratio=>"2:1", :background=>"black", :height=>150, :crop=>"pad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/models.jpg'))
  ->resize(Resize::pad()->height(150)
->aspectRatio("2:1")
  ->background(
  Background::color(Color::BLACK))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/models.jpg", array("aspect_ratio"=>"2:1", "background"=>"black", "height"=>150, "crop"=>"pad"))
Python:
Copy to clipboard
CloudinaryImage("docs/models.jpg").image(aspect_ratio="2:1", background="black", height=150, crop="pad")
Node.js:
Copy to clipboard
cloudinary.image("docs/models.jpg", {aspect_ratio: "2:1", background: "black", height: 150, crop: "pad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("2:1").background("black").height(150).crop("pad")).imageTag("docs/models.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/models.jpg").resize(
  pad()
    .height(150)
    .aspectRatio("2:1")
    .background(color("black"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/models.jpg', {aspectRatio: "2:1", background: "black", height: 150, crop: "pad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/models.jpg", {aspect_ratio: "2:1", background: "black", height: 150, crop: "pad"})
React:
Copy to clipboard
<Image publicId="docs/models.jpg" >
  <Transformation aspectRatio="2:1" background="black" height="150" crop="pad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/models.jpg" >
  <cld-transformation aspect-ratio="2:1" background="black" height="150" crop="pad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/models.jpg" >
  <cl-transformation aspect-ratio="2:1" background="black" height="150" crop="pad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("2:1").Background("black").Height(150).Crop("pad")).BuildImageTag("docs/models.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("2:1").setBackground("black").setHeight(150).setCrop("pad")).generate("docs/models.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("2:1").background("black").height(150).crop("pad")).generate("docs/models.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/models.jpg")
   resize(Resize.pad() { height(150)
 aspectRatio("2:1")
   background(
  Background.color(Color.BLACK))
   }) 
}.generate()
Image padded to a width and height of 250 pixels

lpad (limit pad)

The lpad resize mode behaves the same as the pad mode but only if the original image is larger than the specified limit (width and height), in which case the image is scaled down to fill the specified dimensions while retaining the original aspect ratio and with all of the original image visible. This mode doesn't scale up the image if your requested dimensions are bigger than the original image's. If the proportions of the original image do not match the specified dimensions, padding is added to the image to reach the required size. You can also specify where the original image is placed by using the gravity parameter (set to center by default), and specify the color of the background in the case that padding is added.

Example 1: Scale down and pad an image

Limit the sample image to a bounding box of 400 x 150 pixels, and pad with a green background:

Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :background=>"green", :height=>150, :width=>400, :crop=>"lpad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('sample.jpg'))
  ->resize(Resize::limitPad()->width(400)
->height(150)
  ->background(
  Background::color(Color::GREEN))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("sample.jpg", array("background"=>"green", "height"=>150, "width"=>400, "crop"=>"lpad"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(background="green", height=150, width=400, crop="lpad")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {background: "green", height: 150, width: 400, crop: "lpad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().background("green").height(150).width(400).crop("lpad")).imageTag("sample.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("sample.jpg").resize(
  limitPad()
    .width(400)
    .height(150)
    .background(color("green"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('sample.jpg', {background: "green", height: 150, width: 400, crop: "lpad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {background: "green", height: 150, width: 400, crop: "lpad"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation background="green" height="150" width="400" crop="lpad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="sample.jpg" >
  <cld-transformation background="green" height="150" width="400" crop="lpad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation background="green" height="150" width="400" crop="lpad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Background("green").Height(150).Width(400).Crop("lpad")).BuildImageTag("sample.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setBackground("green").setHeight(150).setWidth(400).setCrop("lpad")).generate("sample.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().background("green").height(150).width(400).crop("lpad")).generate("sample.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("sample.jpg")
   resize(Resize.limitPad() { width(400)
 height(150)
   background(
  Background.color(Color.GREEN))
   }) 
}.generate()
Image lpadded to a width and height of 250 pixels

Example 2: Scale down and pad an image using aspect ratio

Limit the sample image to a bounding box specified by an aspect ratio of 0.66 with a width of 100 pixels, and pad with a green background:

Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :aspect_ratio=>"0.66", :background=>"green", :width=>100, :crop=>"lpad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('sample.jpg'))
  ->resize(Resize::limitPad()->width(100)
->aspectRatio(0.66)
  ->background(
  Background::color(Color::GREEN))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("sample.jpg", array("aspect_ratio"=>"0.66", "background"=>"green", "width"=>100, "crop"=>"lpad"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(aspect_ratio="0.66", background="green", width=100, crop="lpad")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {aspect_ratio: "0.66", background: "green", width: 100, crop: "lpad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("0.66").background("green").width(100).crop("lpad")).imageTag("sample.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("sample.jpg").resize(
  limitPad()
    .width(100)
    .aspectRatio(0.66)
    .background(color("green"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('sample.jpg', {aspectRatio: "0.66", background: "green", width: 100, crop: "lpad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {aspect_ratio: "0.66", background: "green", width: 100, crop: "lpad"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation aspectRatio="0.66" background="green" width="100" crop="lpad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="sample.jpg" >
  <cld-transformation aspect-ratio="0.66" background="green" width="100" crop="lpad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation aspect-ratio="0.66" background="green" width="100" crop="lpad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("0.66").Background("green").Width(100).Crop("lpad")).BuildImageTag("sample.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("0.66").setBackground("green").setWidth(100).setCrop("lpad")).generate("sample.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("0.66").background("green").width(100).crop("lpad")).generate("sample.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("sample.jpg")
   resize(Resize.limitPad() { width(100)
 aspectRatio(0.66F)
   background(
  Background.color(Color.GREEN))
   }) 
}.generate()
Image lpadded to a width and height of 250 pixels

mpad (minimum pad)

The mpad resize mode behaves the same as the pad mode but only if the original image is smaller than the specified minimum (width and height), in which case the image is unchanged but padding is added to fill the specified dimensions. This mode doesn't scale down the image if your requested dimensions are smaller than the original image's. You can also specify where the original image is placed by using the gravity parameter (set to center by default), and specify the color of the background in the case that padding is added.

Example 1: Pad an image to the specified width and height

Minimum pad the 100-pixel wide image sample_100 to a width and height of 200 pixels while retaining the aspect ratio:

Ruby:
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", :background=>"green", :height=>200, :width=>200, :crop=>"mpad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/sample_100.jpg'))
  ->resize(Resize::minimumPad()->width(200)
->height(200)
  ->background(
  Background::color(Color::GREEN))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", array("background"=>"green", "height"=>200, "width"=>200, "crop"=>"mpad"))
Python:
Copy to clipboard
CloudinaryImage("docs/sample_100.jpg").image(background="green", height=200, width=200, crop="mpad")
Node.js:
Copy to clipboard
cloudinary.image("docs/sample_100.jpg", {background: "green", height: 200, width: 200, crop: "mpad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().background("green").height(200).width(200).crop("mpad")).imageTag("docs/sample_100.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/sample_100.jpg").resize(
  minimumPad()
    .width(200)
    .height(200)
    .background(color("green"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/sample_100.jpg', {background: "green", height: 200, width: 200, crop: "mpad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/sample_100.jpg", {background: "green", height: 200, width: 200, crop: "mpad"})
React:
Copy to clipboard
<Image publicId="docs/sample_100.jpg" >
  <Transformation background="green" height="200" width="200" crop="mpad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/sample_100.jpg" >
  <cld-transformation background="green" height="200" width="200" crop="mpad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/sample_100.jpg" >
  <cl-transformation background="green" height="200" width="200" crop="mpad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Background("green").Height(200).Width(200).Crop("mpad")).BuildImageTag("docs/sample_100.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setBackground("green").setHeight(200).setWidth(200).setCrop("mpad")).generate("docs/sample_100.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().background("green").height(200).width(200).crop("mpad")).generate("docs/sample_100.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/sample_100.jpg")
   resize(Resize.minimumPad() { width(200)
 height(200)
   background(
  Background.color(Color.GREEN))
   }) 
}.generate()
Image mpadded to a width and height of 200 pixels

Example 2: Ensure an image fits a minimum bounding box using padding

Minimum pad the sample image to a square of 200 pixels, defined by aspect ratio and width. This results in the original larger image being delivered:

Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :aspect_ratio=>"1.0", :background=>"green", :width=>200, :crop=>"mpad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('sample.jpg'))
  ->resize(Resize::minimumPad()->width(200)
->aspectRatio(1.0)
  ->background(
  Background::color(Color::GREEN))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("sample.jpg", array("aspect_ratio"=>"1.0", "background"=>"green", "width"=>200, "crop"=>"mpad"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(aspect_ratio="1.0", background="green", width=200, crop="mpad")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {aspect_ratio: "1.0", background: "green", width: 200, crop: "mpad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().aspectRatio("1.0").background("green").width(200).crop("mpad")).imageTag("sample.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("sample.jpg").resize(
  minimumPad()
    .width(200)
    .aspectRatio("1.0")
    .background(color("green"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('sample.jpg', {aspectRatio: "1.0", background: "green", width: 200, crop: "mpad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {aspect_ratio: "1.0", background: "green", width: 200, crop: "mpad"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation aspectRatio="1.0" background="green" width="200" crop="mpad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="sample.jpg" >
  <cld-transformation aspect-ratio="1.0" background="green" width="200" crop="mpad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation aspect-ratio="1.0" background="green" width="200" crop="mpad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().AspectRatio("1.0").Background("green").Width(200).Crop("mpad")).BuildImageTag("sample.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setAspectRatio("1.0").setBackground("green").setWidth(200).setCrop("mpad")).generate("sample.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().aspectRatio("1.0").background("green").width(200).crop("mpad")).generate("sample.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("sample.jpg")
   resize(Resize.minimumPad() { width(200)
 aspectRatio(1.0F)
   background(
  Background.color(Color.GREEN))
   }) 
}.generate()
Image left unchanged

Example 3: Pad non-centered image to the specified width and height

Minimum pad the 100-pixel wide image sample_100 to a 175 x 125 pixel rectangle, positioned offset from the top-left:

Ruby:
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", :background=>"pink", :gravity=>"north_west", :height=>125, :width=>175, :x=>20, :y=>20, :crop=>"mpad")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('docs/sample_100.jpg'))
  ->resize(Resize::minimumPad()->width(175)
->height(125)
  ->gravity(
  Gravity::compass(
  Compass::northWest()))
->offsetX(20)
->offsetY(20)
  ->background(
  Background::color(Color::PINK))
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("docs/sample_100.jpg", array("background"=>"pink", "gravity"=>"north_west", "height"=>125, "width"=>175, "x"=>20, "y"=>20, "crop"=>"mpad"))
Python:
Copy to clipboard
CloudinaryImage("docs/sample_100.jpg").image(background="pink", gravity="north_west", height=125, width=175, x=20, y=20, crop="mpad")
Node.js:
Copy to clipboard
cloudinary.image("docs/sample_100.jpg", {background: "pink", gravity: "north_west", height: 125, width: 175, x: 20, y: 20, crop: "mpad"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().background("pink").gravity("north_west").height(125).width(175).x(20).y(20).crop("mpad")).imageTag("docs/sample_100.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("docs/sample_100.jpg").resize(
  minimumPad()
    .width(175)
    .height(125)
    .gravity(compass("north_west"))
    .offsetX(20)
    .offsetY(20)
    .background(color("pink"))
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('docs/sample_100.jpg', {background: "pink", gravity: "north_west", height: 125, width: 175, x: 20, y: 20, crop: "mpad"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("docs/sample_100.jpg", {background: "pink", gravity: "north_west", height: 125, width: 175, x: 20, y: 20, crop: "mpad"})
React:
Copy to clipboard
<Image publicId="docs/sample_100.jpg" >
  <Transformation background="pink" gravity="north_west" height="125" width="175" x="20" y="20" crop="mpad" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="docs/sample_100.jpg" >
  <cld-transformation background="pink" gravity="north_west" height="125" width="175" x="20" y="20" crop="mpad" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="docs/sample_100.jpg" >
  <cl-transformation background="pink" gravity="north_west" height="125" width="175" x="20" y="20" crop="mpad">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Background("pink").Gravity("north_west").Height(125).Width(175).X(20).Y(20).Crop("mpad")).BuildImageTag("docs/sample_100.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setBackground("pink").setGravity("north_west").setHeight(125).setWidth(175).setX(20).setY(20).setCrop("mpad")).generate("docs/sample_100.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().background("pink").gravity("north_west").height(125).width(175).x(20).y(20).crop("mpad")).generate("docs/sample_100.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("docs/sample_100.jpg")
   resize(Resize.minimumPad() { width(175)
 height(125)
   gravity(
  Gravity.compass(
  Compass.northWest()))
 offsetX(20)
 offsetY(20)
   background(
  Background.color(Color.PINK))
   }) 
}.generate()
Image mpadded to a width and height of 200 pixels with gravity west

Resizing and cropping interactive demo

Try out this interactive demo to see the results of different cropping methods, given a specific viewport size.

Things to know about this demo:

  • Not all combinations of cropping and gravity are valid, for example, gravity can't be used together with scale, or any of the fit or pad options (except fill with padding), and fill with padding only works with auto-gravity options.
  • The gravity options g_auto:handbag and g_handbag use the Cloudinary AI Content Analysis Add-on.
  • Although Cloudinary recommends storing your highest resolution images, and delivering scaled-down versions, here you can choose between two sizes of one of the images to show how some modes can give different results depending on the resolution, and to demonstrate the different fit modes.
  • The option to specify no dimensions is intended for use with g_handbag and a cropping option. You can also use it to compare the difference in bytes delivered with and without dimensions using other cropping modes, by inspecting the resulting image properties in your browser.

Choose your starting image, cropping mode, gravity and cropping/viewport dimensions, then click the button to generate the new image.

Square
Portrait
Landscape
Small

Original image size:
1920 x 1920

Gravity positions for crops

When used with cropping modes that crop out part of an image, the gravity qualifier (g in URLs) specifies which part of the original image to keep when one or both of the requested dimensions is smaller than the original.

Compass positions

The basic gravity value is specified by giving a compass direction to include: north_east, north, north_west, west, south_west, south, south_east, east, or center (the default value). The compass direction represents a location in the image, for example, north_east represents the top right corner.

For example, the uploaded jpg image named sample filled to a width and height of 250 pixels while retaining the aspect ratio:

  • Original image:
    Ruby:
    Copy to clipboard
    cl_image_tag("sample.jpg")
    PHP (cloudinary_php v2.x):
    Copy to clipboard
    (new ImageTag('sample.jpg'));
    PHP (cloudinary_php v1.x (legacy)):
    Copy to clipboard
    cl_image_tag("sample.jpg")
    Python:
    Copy to clipboard
    CloudinaryImage("sample.jpg").image()
    Node.js:
    Copy to clipboard
    cloudinary.image("sample.jpg")
    Java:
    Copy to clipboard
    cloudinary.url().transformation(new Transformation().imageTag("sample.jpg");
    JS (js-url-gen v1.x):
    Copy to clipboard
    new CloudinaryImage("sample.jpg");
    JS (cloudinary-core v2.x (legacy)):
    Copy to clipboard
    cloudinary.imageTag('sample.jpg').toHtml();
    jQuery:
    Copy to clipboard
    $.cloudinary.image("sample.jpg")
    React:
    Copy to clipboard
    <Image publicId="sample.jpg" >
    
    </Image>
    Vue.js:
    Copy to clipboard
    <cld-image public-id="sample.jpg" >
    
    </cld-image>
    Angular:
    Copy to clipboard
    <cl-image public-id="sample.jpg" >
    
    </cl-image>
    .NET:
    Copy to clipboard
    cloudinary.Api.UrlImgUp.BuildImageTag("sample.jpg")
    iOS:
    Copy to clipboard
    imageView.cldSetImage(cloudinary.createUrl().generate("sample.jpg")!, cloudinary: cloudinary)
    Android:
    Copy to clipboard
    MediaManager.get().url().transformation(new Transformation().generate("sample.jpg");
    Kotlin:
    Copy to clipboard
    cloudinary.image {
      publicId("sample.jpg") 
    }.generate()
    Original image
  • With gravity set to north:
    Ruby:
    Copy to clipboard
    cl_image_tag("sample.jpg", :gravity=>"north", :height=>250, :width=>250, :crop=>"fill")
    PHP (cloudinary_php v2.x):
    Copy to clipboard
    (new ImageTag('sample.jpg'))
      ->resize(Resize::fill()->width(250)
    ->height(250)
      ->gravity(
      Gravity::compass(
      Compass::north()))
      );
    PHP (cloudinary_php v1.x (legacy)):
    Copy to clipboard
    cl_image_tag("sample.jpg", array("gravity"=>"north", "height"=>250, "width"=>250, "crop"=>"fill"))
    Python:
    Copy to clipboard
    CloudinaryImage("sample.jpg").image(gravity="north", height=250, width=250, crop="fill")
    Node.js:
    Copy to clipboard
    cloudinary.image("sample.jpg", {gravity: "north", height: 250, width: 250, crop: "fill"})
    Java:
    Copy to clipboard
    cloudinary.url().transformation(new Transformation().gravity("north").height(250).width(250).crop("fill")).imageTag("sample.jpg");
    JS (js-url-gen v1.x):
    Copy to clipboard
    new CloudinaryImage("sample.jpg").resize(
      fill()
        .width(250)
        .height(250)
        .gravity(compass("north"))
    );
    JS (cloudinary-core v2.x (legacy)):
    Copy to clipboard
    cloudinary.imageTag('sample.jpg', {gravity: "north", height: 250, width: 250, crop: "fill"}).toHtml();
    jQuery:
    Copy to clipboard
    $.cloudinary.image("sample.jpg", {gravity: "north", height: 250, width: 250, crop: "fill"})
    React:
    Copy to clipboard
    <Image publicId="sample.jpg" >
      <Transformation gravity="north" height="250" width="250" crop="fill" />
    </Image>
    Vue.js:
    Copy to clipboard
    <cld-image public-id="sample.jpg" >
      <cld-transformation gravity="north" height="250" width="250" crop="fill" />
    </cld-image>
    Angular:
    Copy to clipboard
    <cl-image public-id="sample.jpg" >
      <cl-transformation gravity="north" height="250" width="250" crop="fill">
      </cl-transformation>
    </cl-image>
    .NET:
    Copy to clipboard
    cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("north").Height(250).Width(250).Crop("fill")).BuildImageTag("sample.jpg")
    iOS:
    Copy to clipboard
    imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("north").setHeight(250).setWidth(250).setCrop("fill")).generate("sample.jpg")!, cloudinary: cloudinary)
    Android:
    Copy to clipboard
    MediaManager.get().url().transformation(new Transformation().gravity("north").height(250).width(250).crop("fill")).generate("sample.jpg");
    Kotlin:
    Copy to clipboard
    // This code example is not currently available.
    Image filled to a width and height of 250 pixels with north gravity
  • With gravity set to south_east:
    Ruby:
    Copy to clipboard
    cl_image_tag("sample.jpg", :gravity=>"south_east", :height=>250, :width=>250, :crop=>"fill")
    PHP (cloudinary_php v2.x):
    Copy to clipboard
    (new ImageTag('sample.jpg'))
      ->resize(Resize::fill()->width(250)
    ->height(250)
      ->gravity(
      Gravity::compass(
      Compass::southEast()))
      );
    PHP (cloudinary_php v1.x (legacy)):
    Copy to clipboard
    cl_image_tag("sample.jpg", array("gravity"=>"south_east", "height"=>250, "width"=>250, "crop"=>"fill"))
    Python:
    Copy to clipboard
    CloudinaryImage("sample.jpg").image(gravity="south_east", height=250, width=250, crop="fill")
    Node.js:
    Copy to clipboard
    cloudinary.image("sample.jpg", {gravity: "south_east", height: 250, width: 250, crop: "fill"})
    Java:
    Copy to clipboard
    cloudinary.url().transformation(new Transformation().gravity("south_east").height(250).width(250).crop("fill")).imageTag("sample.jpg");
    JS (js-url-gen v1.x):
    Copy to clipboard
    new CloudinaryImage("sample.jpg").resize(
      fill()
        .width(250)
        .height(250)
        .gravity(compass("south_east"))
    );
    JS (cloudinary-core v2.x (legacy)):
    Copy to clipboard
    cloudinary.imageTag('sample.jpg', {gravity: "south_east", height: 250, width: 250, crop: "fill"}).toHtml();
    jQuery:
    Copy to clipboard
    $.cloudinary.image("sample.jpg", {gravity: "south_east", height: 250, width: 250, crop: "fill"})
    React:
    Copy to clipboard
    <Image publicId="sample.jpg" >
      <Transformation gravity="south_east" height="250" width="250" crop="fill" />
    </Image>
    Vue.js:
    Copy to clipboard
    <cld-image public-id="sample.jpg" >
      <cld-transformation gravity="south_east" height="250" width="250" crop="fill" />
    </cld-image>
    Angular:
    Copy to clipboard
    <cl-image public-id="sample.jpg" >
      <cl-transformation gravity="south_east" height="250" width="250" crop="fill">
      </cl-transformation>
    </cl-image>
    .NET:
    Copy to clipboard
    cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("south_east").Height(250).Width(250).Crop("fill")).BuildImageTag("sample.jpg")
    iOS:
    Copy to clipboard
    imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("south_east").setHeight(250).setWidth(250).setCrop("fill")).generate("sample.jpg")!, cloudinary: cloudinary)
    Android:
    Copy to clipboard
    MediaManager.get().url().transformation(new Transformation().gravity("south_east").height(250).width(250).crop("fill")).generate("sample.jpg");
    Kotlin:
    Copy to clipboard
    // This code example is not currently available.
    Image filled to a width and height of 250 pixels with north gravity

Special positions

There are a number of special positions available to use as the focal point for image cropping, for example g_face to automatically detect the largest face in an image and make it the focus of the crop, and g_custom to use custom coordinates that were previously specified (e.g., as part of the image upload method) and make them the focus of the transformation.

For a full listing of the available gravity positions, see special positions in the transformation reference guide.

You can also specify specific objects, by registering for the Cloudinary AI Content Analysis add-on.

Liquid rescaling

Setting the gravity transformation parameter to liquid (g_liquid in URLs), enables content-aware liquid rescaling (also sometimes known as 'seam carving'), which can be useful when changing the aspect ratio of an image. Normal scaling retains all image content even when aspect ratios change, so important elements of an image can be distorted. Liquid rescaling intelligently removes or duplicates 'seams' of pixels that may zig zag horizontally or vertically through the picture. The seams are determined using an algorithm that selects pixels with the least importance (least color change on either side of the seam). The result is an image where the most 'important' elements of the image are retained and generally do not appear distorted although the relative height or width of items in an image may change, especially if you significantly change the aspect ratio.

Tips and guidelines for liquid gravity:

  • It can be used only in conjunction with c_scale.
  • The liquid gravity works best when applied to scenic images with large 'unbusy' sections such as sky, grass, or water.
  • It also works best when applied to larger images. Thus, it is recommended to use this gravity to change aspect ratio using relative widths and heights, where one of the two dimensions remains at or close to 1.0. If you also want to resize the image, apply the resize on a different component of a chained transformation.
  • In some cases, over-aggressive liquid rescaling can result in significant artifacts.

For example, using liquid scaling to change an image to a square (aspect ratio of 1:1) based on the original image width, and then resize the result to 500x500:

Ruby:
Copy to clipboard
cl_image_tag("country_sunset.jpg", :transformation=>[
  {:aspect_ratio=>"1.0", :gravity=>"liquid", :width=>1.0, :crop=>"scale"},
  {:height=>500, :width=>500, :crop=>"scale"}
  ])
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('country_sunset.jpg'))
  ->resize(Resize::scale()->width(1.0)
->aspectRatio(1.0)
->liquidRescaling())
  ->resize(Resize::scale()->width(500)
->height(500));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("country_sunset.jpg", array("transformation"=>array(
  array("aspect_ratio"=>"1.0", "gravity"=>"liquid", "width"=>"1.0", "crop"=>"scale"),
  array("height"=>500, "width"=>500, "crop"=>"scale")
  )))
Python:
Copy to clipboard
CloudinaryImage("country_sunset.jpg").image(transformation=[
  {'aspect_ratio': "1.0", 'gravity': "liquid", 'width': "1.0", 'crop': "scale"},
  {'height': 500, 'width': 500, 'crop': "scale"}
  ])
Node.js:
Copy to clipboard
cloudinary.image("country_sunset.jpg", {transformation: [
  {aspect_ratio: "1.0", gravity: "liquid", width: "1.0", crop: "scale"},
  {height: 500, width: 500, crop: "scale"}
  ]})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation()
  .aspectRatio("1.0").gravity("liquid").width(1.0).crop("scale").chain()
  .height(500).width(500).crop("scale")).imageTag("country_sunset.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("country_sunset.jpg")
  .resize(scale().width("1.0").aspectRatio("1.0").liquidRescaling())
  .resize(scale().width(500).height(500));
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('country_sunset.jpg', {transformation: [
  {aspectRatio: "1.0", gravity: "liquid", width: "1.0", crop: "scale"},
  {height: 500, width: 500, crop: "scale"}
  ]}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("country_sunset.jpg", {transformation: [
  {aspect_ratio: "1.0", gravity: "liquid", width: "1.0", crop: "scale"},
  {height: 500, width: 500, crop: "scale"}
  ]})
React:
Copy to clipboard
<Image publicId="country_sunset.jpg" >
  <Transformation aspectRatio="1.0" gravity="liquid" width="1.0" crop="scale" />
  <Transformation height="500" width="500" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="country_sunset.jpg" >
  <cld-transformation aspect-ratio="1.0" gravity="liquid" width="1.0" crop="scale" />
  <cld-transformation height="500" width="500" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="country_sunset.jpg" >
  <cl-transformation aspect-ratio="1.0" gravity="liquid" width="1.0" crop="scale">
  </cl-transformation>
  <cl-transformation height="500" width="500" crop="scale">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .AspectRatio("1.0").Gravity("liquid").Width(1.0).Crop("scale").Chain()
  .Height(500).Width(500).Crop("scale")).BuildImageTag("country_sunset.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation()
  .setAspectRatio("1.0").setGravity("liquid").setWidth(1.0).setCrop("scale").chain()
  .setHeight(500).setWidth(500).setCrop("scale")).generate("country_sunset.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation()
  .aspectRatio("1.0").gravity("liquid").width(1.0).crop("scale").chain()
  .height(500).width(500).crop("scale")).generate("country_sunset.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("country_sunset.jpg")
   resize(Resize.scale() { width(1.0F)
 aspectRatio(1.0F)
 liquidRescaling() })
   resize(Resize.scale() { width(500)
 height(500) }) 
}.generate()
Scale with liquid gravity

Automatic gravity for crops (g_auto)

Cloudinary's intelligent gravity selection capabilities ensure that the most interesting areas of each image are selected as the main focus for the requested crop, not only for photos with faces, but for any content type. Each image is analyzed individually to find the optimal region to include while cropping. Automatically detected faces (or other elements) are, by default, given higher priority while analyzing the image content.

You apply automatic content-aware gravity by setting the gravity transformation parameter to auto (g_auto in URLs).

Here's an example of using automatic gravity when changing the aspect ratio of an image:

Original image Original Regular image thumbnail c_fill,g_center,h_300,w_200
Regular fill
Automatic image thumbnail with fill c_fill,g_auto,h_300,w_200
Automatic fill

Ruby:
Copy to clipboard
cl_image_tag("basketball_in_net.jpg", :gravity=>"auto", :height=>300, :width=>200, :crop=>"fill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('basketball_in_net.jpg'))
  ->resize(Resize::fill()->width(200)
->height(300)
  ->gravity(
  Gravity::autoGravity())
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("basketball_in_net.jpg", array("gravity"=>"auto", "height"=>300, "width"=>200, "crop"=>"fill"))
Python:
Copy to clipboard
CloudinaryImage("basketball_in_net.jpg").image(gravity="auto", height=300, width=200, crop="fill")
Node.js:
Copy to clipboard
cloudinary.image("basketball_in_net.jpg", {gravity: "auto", height: 300, width: 200, crop: "fill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("auto").height(300).width(200).crop("fill")).imageTag("basketball_in_net.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("basketball_in_net.jpg").resize(
  fill()
    .width(200)
    .height(300)
    .gravity(autoGravity())
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('basketball_in_net.jpg', {gravity: "auto", height: 300, width: 200, crop: "fill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("basketball_in_net.jpg", {gravity: "auto", height: 300, width: 200, crop: "fill"})
React:
Copy to clipboard
<Image publicId="basketball_in_net.jpg" >
  <Transformation gravity="auto" height="300" width="200" crop="fill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="basketball_in_net.jpg" >
  <cld-transformation gravity="auto" height="300" width="200" crop="fill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="basketball_in_net.jpg" >
  <cl-transformation gravity="auto" height="300" width="200" crop="fill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("auto").Height(300).Width(200).Crop("fill")).BuildImageTag("basketball_in_net.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("auto").setHeight(300).setWidth(200).setCrop("fill")).generate("basketball_in_net.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("auto").height(300).width(200).crop("fill")).generate("basketball_in_net.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.

Automatic gravity for crops is supported for the fill, lfill, fill_pad, thumb and crop modes.

Notes and tips

  • Automatic gravity can be further qualified with various focal gravity options, such as g_auto:faces.
  • If custom coordinates have been specified for an image (using the Upload API or the Management Console), the cropping or overlay will be based on that definition, taking the custom coordinates as-is and overriding the detection algorithm (the same as g_custom). This applies to all focal_gravity options except for g_auto:custom_no_override, g_auto:aoi_<custom coordinates> and g_auto:none.
  • You can add the getinfo flag (fl_getinfo in URLs) in your transformation to return the proposed g_auto cropping results in JSON instead of delivering the transformed image. You can then integrate the g_auto results into an external workflow, for example to display the proposed g_auto crop as the initial cropping suggestion in an external editing tool.
  • By default, g_auto applies an optimal combination of our AI and saliency-based algorithms to capture the best region to include in your cropped image. However, in certain situations you may want to explicitly request only one of the auto-cropping algorithms and/or adjust the default focal preference of the chosen algorithm.
  • Automatic gravity is not supported for animated images. If g_auto is used in an animated image transformation, center gravity is applied, except when c_fill_pad is also specified, in which case an error is returned.

Adjusting the automatic gravity focal preference

By default, both the saliency and subject auto-cropping algorithms give increased priority to detected faces. To adjust the focal preference of the automatic cropping algorithm, you can specify a different focal_gravity option.

For example, you can specify adv_face, adv_faces, or adv_eyes to instruct the algorithm to give priority to the eyes or faces detected by the Advanced Facial Attributes Detection add-on (as opposed to the coordinates selected by the built-in face-detection mechanism). Alternatively, you can use the value none if you don't want the algorithm to give special preference to any detected item, including faces.

If you are registered to the Cloudinary AI Content Analysis add-on, you can also instruct the gravity algorithm to give top priority to one or more specific objects or categories from an extensive list.

Additionally, the OCR Text Detection and Extraction Add-on lets you give a higher priority to text by setting the gravity qualifier to auto:ocr_text, while also giving priority to faces and other very prominent elements of an image.

For a complete list of all focal_gravity options, see the g_<special_position> section of the Transformation URL API Reference.

Automatic gravity with the fill mode

Use the fill mode with automatic gravity to keep most of the original image according to the requested dimensions of the derived image, ensuring that the most interesting regions of the original image are included in the resulting image.

Example of square aspect ratio cropping, regular vs. automatic:

Original image Original Regular image thumbnail c_fill,g_center,h_200,w_200
Regular fill
Automatic image thumbnail with fill c_fill,g_auto,h_200,w_200
Automatic fill

Ruby:
Copy to clipboard
cl_image_tag("face_left.jpg", :gravity=>"auto", :height=>200, :width=>200, :crop=>"fill")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('face_left.jpg'))
  ->resize(Resize::fill()->width(200)
->height(200)
  ->gravity(
  Gravity::autoGravity())
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("face_left.jpg", array("gravity"=>"auto", "height"=>200, "width"=>200, "crop"=>"fill"))
Python:
Copy to clipboard
CloudinaryImage("face_left.jpg").image(gravity="auto", height=200, width=200, crop="fill")
Node.js:
Copy to clipboard
cloudinary.image("face_left.jpg", {gravity: "auto", height: 200, width: 200, crop: "fill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("auto").height(200).width(200).crop("fill")).imageTag("face_left.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("face_left.jpg").resize(
  fill()
    .width(200)
    .height(200)
    .gravity(autoGravity())
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('face_left.jpg', {gravity: "auto", height: 200, width: 200, crop: "fill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("face_left.jpg", {gravity: "auto", height: 200, width: 200, crop: "fill"})
React:
Copy to clipboard
<Image publicId="face_left.jpg" >
  <Transformation gravity="auto" height="200" width="200" crop="fill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="face_left.jpg" >
  <cld-transformation gravity="auto" height="200" width="200" crop="fill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="face_left.jpg" >
  <cl-transformation gravity="auto" height="200" width="200" crop="fill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("auto").Height(200).Width(200).Crop("fill")).BuildImageTag("face_left.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("auto").setHeight(200).setWidth(200).setCrop("fill")).generate("face_left.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("auto").height(200).width(200).crop("fill")).generate("face_left.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.

Note
You can also use automatic gravity with the lfill cropping mode.

Automatic gravity with the thumb mode

Use the thumb mode with automatic gravity to apply more aggressive cropping than the fill mode. This mode attempts to further zoom in and crop out less interesting image regions when relevant in order to include the most interesting objects in the resulting derived image. The automatic cropping algorithm decides whether and how aggressively to zoom-in and crop according to the content and cropping ratio of each image individually. A numerical value between 0 and 100 can be added to the g_auto parameter in order to advise the algorithm regarding the desired aggressiveness level (e.g., g_auto:0 for the most aggressive thumb cropping).

Example of a square thumbnail, regular vs. automatic cropping:

Original Original Regular image thumbnail c_thumb,g_center
Regular thumbnail
Automatic image thumbnail c_thumb,g_auto
Automatic thumbnail
Automatic image thumbnail with the most aggressive cropping c_thumb,g_auto:0
Automatic thumbnail -
most aggressive

Ruby:
Copy to clipboard
cl_image_tag("sunset_shoes.jpg", :gravity=>"auto:0", :height=>150, :width=>150, :crop=>"thumb")
PHP (cloudinary_php v2.x):
Copy to clipboard
// This code example is not currently available.
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("sunset_shoes.jpg", array("gravity"=>"auto:0", "height"=>150, "width"=>150, "crop"=>"thumb"))
Python:
Copy to clipboard
CloudinaryImage("sunset_shoes.jpg").image(gravity="auto:0", height=150, width=150, crop="thumb")
Node.js:
Copy to clipboard
cloudinary.image("sunset_shoes.jpg", {gravity: "auto:0", height: 150, width: 150, crop: "thumb"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("auto:0").height(150).width(150).crop("thumb")).imageTag("sunset_shoes.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
// This code example is not currently available.
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('sunset_shoes.jpg', {gravity: "auto:0", height: 150, width: 150, crop: "thumb"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sunset_shoes.jpg", {gravity: "auto:0", height: 150, width: 150, crop: "thumb"})
React:
Copy to clipboard
<Image publicId="sunset_shoes.jpg" >
  <Transformation gravity="auto:0" height="150" width="150" crop="thumb" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="sunset_shoes.jpg" >
  <cld-transformation gravity="auto:0" height="150" width="150" crop="thumb" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sunset_shoes.jpg" >
  <cl-transformation gravity="auto:0" height="150" width="150" crop="thumb">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("auto:0").Height(150).Width(150).Crop("thumb")).BuildImageTag("sunset_shoes.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("auto:0").setHeight(150).setWidth(150).setCrop("thumb")).generate("sunset_shoes.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("auto:0").height(150).width(150).crop("thumb")).generate("sunset_shoes.jpg");
Kotlin:
Copy to clipboard
// This code example is not currently available.

Note
The numeric value supplied for auto gravity together with thumb cropping indicates your preference for more or less aggressive zooming and the algorithm takes that preference into account. However, the automatic gravity algorithm may still determine that for a particular image and aspect ratio, its default zoom selection is the appropriate zoom for this image. In such a case, you may not see a difference between the default g_auto and g_auto with a specific agressiveness level.

Automatic gravity with the crop mode

Use the crop mode with automatic gravity to crop a region of exactly the specified dimensions out of the original image while automatically focusing on the most interesting region of the original image that fits within the required dimensions. The portion of the interesting area depends on the resolution of the original image. The crop mode is less useful than the fill, lfill, and thumb modes, as it is only practical to use when both the dimensions of the original image and the size of the interesting region are already known.

Example of a square crop, regular vs. auto cropping:

Original image Original Regular image thumbnail c_crop,g_center
Regular crop
Automatic image thumbnail with crop c_crop,g_auto
Automatic crop

Ruby:
Copy to clipboard
cl_image_tag("fat_cat.jpg", :gravity=>"auto", :height=>200, :width=>200, :crop=>"crop")
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('fat_cat.jpg'))
  ->resize(Resize::crop()->width(200)
->height(200)
  ->gravity(
  Gravity::autoGravity())
  );
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("fat_cat.jpg", array("gravity"=>"auto", "height"=>200, "width"=>200, "crop"=>"crop"))
Python:
Copy to clipboard
CloudinaryImage("fat_cat.jpg").image(gravity="auto", height=200, width=200, crop="crop")
Node.js:
Copy to clipboard
cloudinary.image("fat_cat.jpg", {gravity: "auto", height: 200, width: 200, crop: "crop"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().gravity("auto").height(200).width(200).crop("crop")).imageTag("fat_cat.jpg");
JS (js-url-gen v1.x):
Copy to clipboard
new CloudinaryImage("fat_cat.jpg").resize(
  crop()
    .width(200)
    .height(200)
    .gravity(autoGravity())
);
JS (cloudinary-core v2.x (legacy)):
Copy to clipboard
cloudinary.imageTag('fat_cat.jpg', {gravity: "auto", height: 200, width: 200, crop: "crop"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("fat_cat.jpg", {gravity: "auto", height: 200, width: 200, crop: "crop"})
React:
Copy to clipboard
<Image publicId="fat_cat.jpg" >
  <Transformation gravity="auto" height="200" width="200" crop="crop" />
</Image>
Vue.js:
Copy to clipboard
<cld-image public-id="fat_cat.jpg" >
  <cld-transformation gravity="auto" height="200" width="200" crop="crop" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="fat_cat.jpg" >
  <cl-transformation gravity="auto" height="200" width="200" crop="crop">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Gravity("auto").Height(200).Width(200).Crop("crop")).BuildImageTag("fat_cat.jpg")
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setGravity("auto").setHeight(200).setWidth(200).setCrop("crop")).generate("fat_cat.jpg")!, cloudinary: cloudinary)
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().gravity("auto").height(200).width(200).crop("crop")).generate("fat_cat.jpg");
Kotlin:
Copy to clipboard
cloudinary.image {
  publicId("fat_cat.jpg")
   resize(Resize.crop() { width(200)
 height(200)
   gravity(
  Gravity.autoGravity())
   }) 
}.generate()

Selecting a single auto-cropping algorithm

The default g_auto option applies an optimal mixture of two different methods of identifying the most important region of your image:

  • AI-based algorithm (subject) - Uses deep-learning algorithms to identify the subjects of an image that are most likely to attract a person's gaze.
  • Saliency algorithm (classic) - Uses a combination of saliency heuristics, edge detection, light, skin-tone prioritization, and more to automatically detect and prioritize significant region(s) in the image.

In the majority of cases, the most salient elements in an image are also the main subjects of the photo, and thus both algorithms often produce very similar or identical results. However, in some cases, results can be somewhat different and therefore the weighted mixture that g_auto applies by default usually gives the best results.

But if you find that for the types of images you are delivering, one of the algorithms consistently gives a better result than the other, you can override the default combined mechanism and instead explicitly request your preferred algorithm by specifying auto:subject or auto:classic as the gravity value (g_auto:subject or g_auto:classic in URLs), with or without an additional focal gravity option.

For example, when cropping the landscape image below to a portrait view, the classic algorithm selects the areas with the most graphically salient spots, which include the tree against the bright blue and white sky, while the subject algorithm focuses on the subjects that the AI-engine predicts are most likely to capture human attention, primarily the boat, lake and mountains.

Original image Original image
Original image Default crop
(Center gravity)
Saliency method Auto-gravity
Saliency (classic) method
AI-based subject method Auto-gravity
AI-based subject method

Device Pixel Ratio (DPR)

Different devices support different DPR values, which is defined as the ratio between physical pixels and logical pixels. This means that a device with support for a higher DPR uses more physical pixels for displaying an image, resulting in a clearer, sharper image.

DPR illustration

Use the dpr parameter to set the DPR value of the delivered image. The parameter accepts a numeric value specifying the DPR multiplier.

The dpr parameter is especially useful when adding overlays, as you need the overlay image to be correctly resized according to the required pixel density of the device (along with the containing image). Setting the dpr transformation parameter applies the same resizing rules both to the containing image, and the included overlay.

For example, the following URL dynamically generates a 100x100 face-detection-based circular thumbnail of an image named lady, and adds another image named cloudinary_icon_white as a semi-transparent watermark with a width of 50 pixels. Setting the dpr value to 1.0, 2.0 (as in the code example) or 3.0 generates the following images, while resizing both the containing image and the overlay to match the required DPR.

Ruby:
Copy to clipboard
cl_image_tag("lady.jpg", :transformation=>[
  {:gravity=>"face", :height=>100, :width=>100, :crop=>"thumb"},
  {:radius=>"max"},
  {:overlay=>"cloudinary_icon_white"},
  {:flags=>"relative", :width=>0.5, :crop=>"scale"},
  {:opacity=>60},
  {:effect=>"brightness:100"},
  {:flags=>"layer_apply"},
  {:dpr=>2.0}
  ])
PHP (cloudinary_php v2.x):
Copy to clipboard
(new ImageTag('lady.jpg'))
  ->resize(Resize::thumbnail()->width(100)
->height(100)
  ->gravity(
  Gravity::focusOn(
  FocusOn::face()))
  )
  ->roundCorners(RoundCorners::max())
  ->overlay(Overlay::source(
  Source::image("cloudinary_icon_white")
  ->transformation((new Transformation())
  ->resize(Resize::scale()->width(0.5)
  ->relative()
  )
  ->adjust(Adjust::opacity(60))
  ->adjust(Adjust::brightness()->level(100)))
  ))
  ->delivery(Delivery::dpr(2.0));
PHP (cloudinary_php v1.x (legacy)):
Copy to clipboard
cl_image_tag("lady.jpg", array("transformation"=>array(
  array("gravity"=>"face", "height"=>100, "width"=>100, "crop"=>"thumb"),
  array("radius"=>"max"),
  array("overlay"=>"cloudinary_icon_white"),
  array("flags"=>"relative", "width"=>"0.5", "crop"=>"scale"),
  array("opacity"=>60),
  array("effect"=>"brightness:100"),
  array("flags"=>"layer_apply"),
  array("dpr"=>"2.0")
  )))