Getting started

Integration & migration

Image & video API

DAM user guide

API overview

Account

Resize and crop images

Learn how to resize and crop images using ImageKit.io's real-time URL-based transformation API.


Width - (w)

This parameter specifies the width of the output image. It accepts an integer value greater than 1. If a value between 0 and 1 is specified, it acts as a percentage width. Therefore, 0.1 means 10% of the original width, 0.4 means 40% of the original width, and so on.

You can also specify the auto value for this parameter (w-auto). Doing so will instruct ImageKit.io to read the width value from the Sec-CH-Width Client Hint request header. Learn more about client hints here.

You can also provide an arithmetic expression such as iw_div_2, cw_mul_0.8, or ih as a value for this parameter. Doing so will first calculate the value of the expression and then apply transformations using that value. Learn more about arithmetic expressions here.

Height - (h)

This parameter specifies the width of the output image. It accepts an integer value greater than 1. If a value between 0 and 1 is specified, the value acts as a percentage height. Therefore, 0.1 means 10% of the original height, 0.4 means 40% of the original height, and so on.

You can also provide an arithmetic expression such as ih_div_2, ch_mul_0.5, or ih as a value for this parameter. Doing so will first calculate the value of the expression and then apply transformations using that value. Learn more about arithmetic expressions here.

Aspect ratio - (ar)

Used to specify the aspect ratio of the output image or the ratio of width to height of the output image. This parameter must be used along with either the height(h) or width(w) parameter. The format for specifying this transformation is ar-<width>-<height>.

You can also provide an arithmetic expression such as iar_div_2 or car_mul_0.75 as a value for this parameter. Doing so will first calculate the value of the expression and then apply transformations using that value. Learn more about arithmetic expressions here.

Note: If you specify both height(h) and width(w) in the URL along with the aspect ratio(ar), then the aspect ratio is ignored.

Crop, Crop modes & Focus

If only one of the height(h) or width(w) dimensions is specified, ImageKit.io adjusts the other dimension accordingly to preserve the aspect ratio, and no cropping takes place.

But when you specify both height(h) and width(w) dimensions, you need to choose the right cropping strategy based on your website layout and desired output image.

Choosing the right cropping strategy

  • If you want to preserve the whole image content (no cropping) and need the exact same dimensions (height and width) in the output image as requested, choose either of the pad resize crop or forced crop strategy.

  • If you want to preserve the whole image content (no cropping), but it is okay if one or both the dimensions (height or width) in the output image are adjusted to preserve the aspect ratio. Then choose either of the max-size cropping or min-size cropping strategy. You can also use the max-size-enlarge cropping strategy if you want to allow enlarging of the image in case the requested dimensions are more than the original image dimension.

  • If you need the exact same dimensions (height and width) in the output image as requested but it's okay to crop the image to preserve the aspect ratio (or extract a region from the original image). Then choose either of the maintain ratio crop or extract crop or pad extract crop strategy. You can combine the extract crop strategy with different focus values to get the desired result.

Note: When choosing among different strategies for cropping, think in terms of your website layout and desired output image.

Pad resize crop strategy - (cm-pad_resize)

In the pad resize crop strategy, the output image's dimensions (height and width) are the same as requested, no cropping takes place, and the aspect ratio is preserved. This is accomplished by adding padding around the output image to match the exact dimension as requested.

Example - All padding on one side

In the examples above, we saw that when the image is padded using the pad resize crop strategy, the padding is equal on both sides of the image. However, there might be cases where we want all the padding to be added on only one side of the image. This can be done using the focus (fo) parameter.

https://ik.imagekit.io/ikmedia/tr:w-300,h-200,cm-pad_resize,bg-F3F3F3,fo-left/docs_images/examples/example_food_3.jpg

We added the fo-left transformation to our image. Now, all the padding is on the right of the image, while the image itself is on the left (determined by the value of focus(fo) parameter).

Forced crop strategy - (c-force)

In a forced crop strategy, the output image's dimension (height and width) is exactly the same as requested. No cropping takes place, but the aspect ratio is not preserved. It forcefully squeezes the original image to fit it completely within the output dimensions.

Max-size cropping strategy - (c-at_max)

In the max-size crop strategy, the whole image content is preserved (no cropping), and the aspect ratio is preserved, but one of the dimensions (height or width) is adjusted.

The output image is less than or equal to the dimensions specified in the URL,i.e., at least one dimension will exactly match the output dimension requested, and the other dimension will be equal to or smaller than the corresponding output dimension requested.

Note:

  • This mode is particularly useful if you have a container and you want to ensure that the image will never be larger than that container.
  • If the requested dimension is more than the original dimension of the image, then the original image is returned without cropping. To enlarge the images more than the original dimensions, check max-size-enlarge cropping strategy.

Max-size-enlarge cropping strategy - (c-at_max_enlarge)

This strategy is similar to the max-size cropping strategy, except it also allows an image to be enlarged beyond its original dimensions.

The output image is less than or equal to the dimensions specified in the URL,i.e., at least one dimension will exactly match the output dimension requested, and the other dimension will be equal to or smaller than the corresponding output dimension requested.

Min-size cropping strategy - (c-at_least)

This strategy is similar to the max-size cropping strategy, with the only difference being that, unlike the max-size strategy, the image is equal to or larger than the requested dimensions.

One of the dimensions will be exactly the same as what is requested, while the other dimension will be equal to or larger than what is requested.

Note: This is useful for cases where you want to have an image that is always at least as large as the container.

Maintain ratio crop strategy - (c-maintain_ratio)

This is the default crop strategy. If nothing is specified in the URL, this strategy gets applied automatically.

In this strategy, the output image's dimension (height and width) is the same as requested, and the aspect ratio is preserved. This is accomplished resizing the image to the requested dimension and in the process cropping parts from the original image.

Note: By default, ImageKit.io extracts the image from the center, but you can change this using the focus parameter.

Examples - Object-aware cropping (maintain-ratio)

We can also provide an object-specific focus to crop the image, ensuring that the object is centered while cropping out the less important parts of the image.

Extract crop strategy - (cm-extract)

In this strategy, the output image's dimension (height and width) is exactly the same as requested, and the aspect ratio is preserved.

Instead of trying to resize the image as we did in the maintain ratio strategy, we extract out a region of the requested dimension from the original image.

Note: By default, ImageKit.io extracts the image from the center, but you can change this using the focus parameter.

Example - Center and relative focus

Example - Focus using cropped image coordinates

In the coordinate method of focus, we use the parameters x and y to determine the position of the top-left corner from where the extract would begin.

You can also provide arithmetic expressions such as iw_mul_0.4 or ih_mul_0.4 as values for these parameters. This will first calculate the value of the expression and then use those values for extraction. Learn more about arithmetic expressions here.

Example URL - https://ik.imagekit.io/ikmedia/tr:w-300,h-300,cm-extract,x-100,y-300/docs_images/examples/example_food_3.jpg

The extract is made starting from (100,300) point on the original image.

Examples - Object-aware cropping (cm-extract)

Instead of specifying the x, y, xc or yc coordinates to focus on a particular area, you can also set the fo value to an object from the supported list, which will intelligently detect xc and yc values as the center of the provided object and extract the image with the given width and height. Learn more about object-aware cropping here.

Example - Focus using custom coordinates

Instead of specifying the x, y, xc or xy coordinates to focus on a particular area, you can also specify the focus area in an image while uploading the image or from the media library and then use fo-custom transformation in the image URL. ImageKit will then utilize the custom crop area specified with the image for all crop operations. This custom focus mode works for both the extract crop and the default maintain ratio crop strategy.

Unexpected behavior with auto rotation

ImageKit.io can be configured to auto-rotate images based on the Orientation value in the image metadata. This could result in unexpected behavior when using cm-extract. In that case, you will have to adjust the values of x and y to accommodate for the oriented image.

Pad extract crop strategy - (cm-pad_extract)

The pad extract crop strategy is an extension of the extract crop strategy. In the extract crop strategy, we were extracting out a smaller area from a larger image. Now, there can be a scenario where the original image is small and we want to extract out a larger area (which is practically not possible without padding). So, the pad extract mode adds a solid colored padding around the image to match the exact size requested.

This transformation is specified using the value cm-pad_extract transformation parameter in the URL.

Focus - (fo)

This parameter can be used along with pad resize, maintain ratio, or extract crop to change the behavior of padding or cropping. Learn more from the different examples shown in respective sections.

This parameter can have the following values depending on where it is being used:

  1. left, right, top, bottom can be to control the position of padding when used with pad resize. Learn from examples.

  2. fo-custom can be used to define a specific focus area when used with maintain ratio and extract crop.

  3. center, top, left, bottom, right, top_left, top_right, bottom_left and bottom_right can be used to define relative cropping during extract crop. Learn from examples.

Apart from the above, the fo parameter also has two additional options that intelligently detect the most important part of an image to create thumbnails, i.e., auto and face. Let's see them in action:

Auto smart cropping - (fo-auto)

In this mode, ImageKit.io automatically determines the most important part of the image and preserves it in the output thumbnail. This is enabled by passing the fo-auto parameter in the URL transformation parameters.

https://ik.imagekit.io/ikmedia/docs_images/examples/Smart_crop_example.jpg

For example, in the image below, the child is the main subject, and he is slightly towards the right from the center of the image.

Face cropping - (fo-face)

In face crop, the crop works more like the extract crop strategy, but instead of focusing on the center of the image, it finds out the face (or multiple faces) in the image and focuses around that.

This gives you perfect thumbnails with just the subject's face that make up for good profile pictures.

This mode is enabled using the 'fo-face' parameter in the URL transformation parameters.

Note: Smart crop may not give accurate results for some images. There is a trade-off between speed (needed for real-time transformations) and accuracy.

Object-aware cropping - (fo-object name)

To automatically provide high focus to a particular object while cropping, you can set the fo value to an object from the supported list. This allows for intelligent cropping of the image based on the specific objects detected within it.

It can be used along with maintain ratio or extract crop to intelligently crop or extract parts of the image as needed. Learn more from the different examples shown in respective sections.

We can also choose not to provide dimensions and deliver an image that is tightly cropped to the object.

Tightly crop object

To deliver an image that is tightly cropped to the object, provide fo-<object name> without specifying height and width dimensions. This will return an image that contains only the given object.

We can also specify aspect ratio along with the fo-<object name> focus option without providing height and width. This keeps the object in focus but may include more of the image to fit the specified aspect ratio.

We can also crop out a particular object of interest out of multiple different objects present in an image.

Supported object list

We currently support all the objects included in the COCO classes. The object list is mentioned below:

COCO Classes
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
trafficLight
fireHydrant
stopSign
parkingMeter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sportsBall
kite
baseballBat
baseballGlove
skateboard
surfboard
tennisRacket
bottle
wineGlass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hotDog
pizza
donut
cake
chair
couch
pottedPlant
bed
diningTable
toilet
tv
laptop
mouse
remote
keyboard
cellPhone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddyBear
hairDrier
toothbrush

Zoom - (z)

This parameter accepts a number that determines how much to zoom in or out of the cropped area. It must be used along with fo-face. A value less than 1.0 zooms out to include more background surrounding the face, whereas a value larger than 1.0 zooms in to exclude more background surrounding the face.

Default Value - z = 1.0

Possible Values - 0 < z < 1 (zoom out); z > 1 (zoom in)

DPR - (dpr)

Used to specify the device pixel ratio that is used to calculate the dimensions of the output image. Extremely helpful when creating image transformations for devices with a high device pixel ratio (DPR > 1), like the iPhone or high-end Android devices.

The dpr parameter can only be used when either the height or width of the desired output image is specified.

If the output image's height or width after considering the specified DPR is less than 1px or greater than 5000px, the value of DPR is not considered and the height or width used in the URL is used.

Possible values0.1  to 5.

Alternatively, you can specify the auto value for this parameter (dpr-auto). Doing so will instruct ImageKit to read the dpr value from the Sec-CH-DPR Client Hint request header. Learn more about client hints here.