Last updated: Oct-31-2023
Responsive design and art direction generally requires displaying images at a variety of sizes, often much smaller than the original.
If you deliver full size images and rely on browser-side resizing (using CSS or HTML width and height attributes), users are forced to download unnecessarily large files. Therefore, images 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 asset 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:
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_150sets 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.5sets the width to half the original width.
iwas values sets the dimension to the initial height or initial width of the original image respectively. For example,
w_iwsets 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:
asignifies the relative width and
bthe relative height (e.g.,
- a decimal value representing the ratio of the width divided by the height (e.g.,
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_200is 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 cropping modes only. Not supported for
- 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
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:
|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.|
|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.|
|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.|
|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.|
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 using SDKs), 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.
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:
You could deliver the
c_crop transformation shown above as follows:
The following sections explain how each of the crop modes behave.
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).
Fill a 250-pixel square with the models image:
Fill a 250-pixel square with the top-left part (gravity northwest) of the models image:
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).
Fill a 150 x 200 pixel area with the
models image and limiting the size to no larger than the original image:
Scale down the
models image to fill a 200-pixel square defined by aspect ratio and height:
fill_pad cropping mode tries to prevent a "bad crop" by first attempting to use the
fill mode, but adds padding if it is determined that more of the original image needs to be included in the final image. This is especially useful if the aspect ratio of the delivered image is considerably different from the original's aspect ratio. It is only supported in conjunction with Automatic cropping (g_auto), and not supported for animated images.
lady image delivered as an 80 x 400 image using the
fill mode on the left, with the
fill_pad mode on the right:
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.
models image to a width of 200 pixels and a height of 150 pixels, with northwest gravity:
models image to a width of 450 pixels and an aspect ratio of 2.5:
woman image without specifying dimensions, to keep only the face.
cld-decompose_tile special gravity position to crop an image composed of many images, such as this, keeping only the largest "tile":
You can specify a region of the original image to crop by giving the
y coordinates of the top left corner of the region together with the
height of the region. You can also use percentage based numbers instead of the exact coordinates for
h (e.g., 0.5 for 50%) - in this case all four parameters must be percentage based.
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.
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:
The image can be further resized with chained transformations. For example, the 300x200 cropped version above, also scaled down to half that size:
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.
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:
Create a 150-pixel high thumbnail with aspect ratio 5:6 and face detection of the
woman image, zoomed out by 75%.
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.
Scale the models image to a width of 150 pixels (maintains the aspect ratio by default):
Scale the models image to a width and height of 150 pixels without maintaining the aspect ratio:
Scale the models image to a width of 25% (maintains the aspect ratio by default):
Scale the models image to a width of 100, changing the aspect ratio to 1:2:
Scale the models image to a height of 150, ignoring the aspect ratio:
scalemode can be used to scale up or scale down an image, however when scaling up, the image often loses clarity. To retain clarity while upscaling an image, consider upscaling with super resolution.
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.
Resize the models image to fit within a width and height of 250 pixels while retaining the aspect ratio:
Resize the models image to fit within a 150-pixel square defined by aspect ratio and height:
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.
Limit the models image to a width and height of 250 pixels while retaining the aspect ratio:
Limit the models image to a 150-pixel square defined by aspect ratio and height:
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.
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:
Scale up the 100-pixel wide
sample_100 image to fit a 150-pixel square defined by aspect ratio and height.
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.
Resize and pad the models image with a black background to a width and height of 250 pixels:
Resize and pad the models image with a black background to a rectangle of height of 150 pixels, and aspect ratio 2:1:
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.
sample image to a bounding box of 400 x 150 pixels, and pad with a green background:
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:
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.
Minimum pad the 100-pixel wide image
sample_100 to a width and height of 200 pixels while retaining the aspect ratio:
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:
Minimum pad the 100-pixel wide image
sample_100 to a 175 x 125 pixel rectangle, positioned offset from the top-left:
Try out this interactive demo to see the results of different cropping methods, given a specific viewport size.
- Not all combinations of cropping and gravity are valid, for example, gravity can't be used together with
scale, or any of the
fill with padding), and
fill with paddingonly works with auto-gravity options.
- The gravity options
g_handbaguse 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_handbagand 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.
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.
The basic gravity value is specified by giving a compass direction to include:
center (the default value). The compass direction represents a location in the image, for example,
north_east represents the top right corner.
For example, fill a 250-pixel square with the sample image while retaining the aspect ratio:
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.
gravity transformation parameter to
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.
liquidgravity 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:
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
g_auto in URLs).
Here's an example of using automatic gravity when changing the aspect ratio of an image:
- Automatic gravity can be further qualified with various focal gravity options, such as
- If custom coordinates have been specified for an image (using the Upload API or the Cloudinary 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_gravityoptions except for
- You can add the getinfo flag (
fl_getinfoin URLs) in your transformation to return the proposed
g_autocropping results in JSON instead of delivering the transformed image. You can then integrate the
g_autoresults into an external workflow, for example to display the proposed
g_autocrop as the initial cropping suggestion in an external editing tool.
- By default,
g_autoapplies 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_autois used in an animated image transformation,
centergravity is applied, except when
c_fill_padis also specified, in which case an error is returned.
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
For example, you can specify
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.
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:
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:
Automatic thumbnail -
autogravity together with
thumbcropping 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_autowith a specific aggressiveness level.
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:
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:classic as the gravity value (
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.
(Center gravity) Auto-gravity
Saliency (classic) method Auto-gravity
AI-based subject method
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 parameter to set the DPR value of the delivered image. The parameter accepts a numeric value specifying the DPR multiplier.
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.
Now you can create a 100x100 HTML image tag and deliver an image with the resolution that best matches the specified pixel density of your users' devices. The three images below are all displayed within a 100x100 logical square using the
<img> tag width and height attributes, while you see more details and a better visual result for the last two images if you view this documentation using a device that supports a higher DPR.
You can alternatively use
dpr_auto to automatically deliver the best image, depending on the requesting device's support for DPR. For details, see the Responsive images documentation.
Normally, when you upscale a small image, the image loses detail and quality. Take, for example, this 200 x 303 pixel image of a hall:
If you upscale it to four times its dimensions using
c_scale,w_4.0, this is the result:
Using the generative AI upscale effect, you can retain much more detail for the same scaling factor:
upscale effect scales each dimension by four, multiplying the total number of pixels by 16, and uses AI-based prediction to fill in the details.
You can chain other transformations after the upscale, for example, creating a square image of the ceiling using the fill cropping mode, and switching it to grayscale:
- To use the upscale effect, the input image must be smaller than 0.25 megapixels (the equivalent of 625 x 400 pixels).
- You can combine the upscale effect with other transformation parameters, but the upscale effect must be the first component in the chain.
- There is a special transformation count for the upscale effect.
- The result of the upscale effect is cached, so even if it is used again together with different transformations, no further transformations are counted for its use on the same image.
- The upscale effect is not supported for animated images or fetched images.