Resize & Crop

Resizing and cropping images are the two basic geometry transformations. Like other Uploadcare image processing, they're non-destructive and applied at the CDN delivery stage.

The order of operations is important. For example, when you resize an image first, the following crop operation will use the coordinates of the resized image.

Preview

-/preview/
-/preview/:dimensions/

Downscales an image proportionally to fit the given width and height in pixels.

  • :dimensions — width and height in pixels separated by x.
    Example: 320x240.
    If the dimensions are not specified, it'll be 2048x2048.
/:uuid/-/preview/
/:uuid/-/preview/300x500/

Resize

-/resize/:one_or_two_dimensions/

Resizes an image to one or two dimensions. When you set both width and height explicitly, it may result in a distorted image. If you specify either side, this operation will preserve the original aspect ratio and resize the image accordingly.

  • :one_or_two_dimensions — width and height in pixels separated by x, one of the values can be omitted.
    Examples: 320x240, 320x, x240.
/:uuid/-/resize/320x240/
/:uuid/-/resize/320x/
/:uuid/-/resize/x240/

Change resize behavior

-/stretch/:mode/

Sets the resize and scale_crop behavior when a source image is smaller than the requested dimensions. The order is important: a stretch operation applies to its following resize or scale_crop. You can use multiple resizing operations with individual stretch behavior for each one.

  • :mode — one of the following values:
    • on — an image can be scaled up (default).
    • off — an image won't be scaled up.
    • fill — no image scaling up. The background color fills the remaining parts.
Resized image but stretched
/preview/160x160/-
/resize/220x/
Resized image, same dimensions
/preview/160x160/-
/stretch/off/-
/resize/220x/
Resized image, same placeholder size with a background fill
/preview/160x160/-
/stretch/fill/-
/resize/220x/

Smart resize

-/smart_resize/:dimensions/

Content-aware resize helps retaining original proportions of faces and other visually sensible objects while resizing the rest of the image using intelligent algorithms.

  • :dimensions — width and height in pixels separated by x.
    Example: 320x240.

Original image, 440x500

/smart_resize/440x600/

/smart_resize/440x300/

Smart resize works best for solid, gradient, and homogenous backgrounds that'll be expanded. If background stretching can't be done without artifacts, a solid fill with a matching color will be applied instead.

Note that we are working on improving Smart resize and behaviour may change. Contact us if you found out regression.

Crop

-/crop/:dimensions/
-/crop/:dimensions/:alignment/

Crops an image by using specified dimensions and alignment.

  • :dimensions — width and height in pixels or percents separated by x.
    Percent values are based on original size and should end with % or p char.
    Examples: 320x240, 50px240, 30%x25%.
  • :alignment — horizontal and vertical offsets in pixels or percents separated by ,.
    Offset in pixels is allowed to be negative. Percent values are a position of cropped area in full image canvas and should end with % or p char.
    Examples: 100,-50, 50p,240, 30%,25%.
    A single semantic shortcut is also allowed as an alignment value: center, top, right, bottom, left.
    If alignment is not specified, 0,0 value is used.

In URLs, % is an escape character and should be encoded with %25 escape sequence, e.g. /crop/200x200/80%25,80%25/. For convenience, you can use the p shortcut for percent which doesn't require encoding.

Original image
Original
Crop to center
/crop/2000x1325/center/
Crop to coordinates
/crop/640x424/2560,830/

If given dimensions are greater than the ones of the original image, the image is rendered inside a color-filled box. The color of that box can be changed via the setfill operation.

Crop by ratio

-/crop/:ratio/
-/crop/:ratio/:alignment/

Crops the image to the specified aspect ratio, cutting off the rest of the image.

  • :ratio — two numbers greater than zero separated by :.
    Ratio is the quotient from the division of these numbers.
  • :alignment — horizontal and vertical offsets in pixels or percents separated by ,.
    Offset in pixels is allowed to be negative. Percent values are a position of cropped area in full image canvas and should end with % or p char.
    Examples: 100,-50, 50p,240, 30%,25%.
    A single semantic shortcut is also allowed as an alignment value: center, top, right, bottom, left.
    If alignment is not specified, center value is used.

In URLs, % is an escape character and should be encoded with %25 escape sequence, e.g. /crop/4:3/80%25,80%25/. For convenience, you can use the p shortcut for percent which doesn't require encoding.

Original
Crop 4:3
Crop 16:9
Original/crop/4:3//crop/16:9/bottom/

Crop by objects

-/crop/:tag/
-/crop/:tag/:ratio/
-/crop/:tag/:ratio/:alignment/
-/crop/:tag/:dimensions/
-/crop/:tag/:dimensions/:alignment/

Crops the image to the object specified by the :tag parameter.

  • :tag — one of the following values:
    • face — the largest detected face in the image is used as a crop basis.
    • image — the whole image is used as a crop basis.
  • :ratio — two numbers greater than zero separated by :. Ratio is the quotient from the division of these numbers. The found object fits into the given aspect ratio.
  • :dimensions — width and height in percents separated by x.
    Percent values are based on the tag size and should end with % or p char.
    Examples: 150%x150%, 200%x100%.
  • :alignment — horizontal and vertical offsets in percentage values separated by comma (,).
    The offsets represent a position of cropped area that is based on the selected tag location (for instance, a face). The offset percentage values should end with % or p char.
    Examples: 30%,25%, 30p,25p
    A single semantic shortcut is also allowed as an alignment value: center, top, right, bottom, left.
    If alignment is not specified, center value is used.

In URLs, % is an escape character and should be encoded with %25 escape sequence, e.g. /crop/face/4:3/80%25,80%25/. For convenience, you can use the p shortcut for percent which doesn't require encoding.

Original
Cropped image by face with square sides
Cropped image by face with parameters
Original/crop/face/1:1//crop/face/200px200p/
-/crop/1:1/50p,30p/

Scale crop

-/scale_crop/:dimensions/
-/scale_crop/:dimensions/:alignment/

Scales an image until it fully covers the specified dimensions; the rest gets cropped. Mostly used to place images with various dimensions into placeholders (e.g., square shaped).

  • :dimensions — width and height in pixels separated by x.
    Examples: 320x240, 440x440.
  • :alignment — horizontal and vertical offsets in percentage values separated by comma (,).
    The offsets represent a position of cropped area that is based on the selected tag location (for instance, a face). The offset percentage values should end with % or p char.
    Examples: 30%,25%, 30p,25p
    A single semantic shortcut is also allowed as an alignment value: center, top, right, bottom, left.
    If alignment is not specified, 0,0 value is used.

In URLs, % is an escape character and should be encoded with %25 escape sequence, e.g. /scale_crop/440x440/80%25,80%25/. For convenience, you can use the p shortcut for percent which doesn't require encoding.

Resize image to a size, stretched
/resize/1024x1024/
Requested size
Distorted
Adjust size, keep dimensions
/preview/1024x1024/
Adjusted size
No distortion
Resize image to a placeholder, zoom and crop
/scale_crop/1024x1024/center/
Requested size
No distortion

Smart crop

-/scale_crop/:dimensions/:type/
-/scale_crop/:dimensions/:type/:alignment/

Switching the crop type to one of the smart modes enables the content-aware mechanics. Uploadcare applies AI-based algorithms to detect faces and other visually sensible objects to crop the background and not the main object.

  • :dimensions — width and height in pixels separated by x.
    Examples: 320x240, 440x440.
  • :type — one of the following values:
    • smart (alias for smart_faces_objects_points)
    • smart_faces_objects
    • smart_faces_points
    • smart_objects_faces_points
    • smart_objects_faces
    • smart_objects_points
    • smart_points
    • smart_objects
    • smart_faces
  • :alignment — horizontal and vertical offsets in percentage values separated by comma (,).
    The offsets represent a position of cropped area that is based on the selected tag location (for instance, a face). The offset percentage values should end with % or p char.
    Examples: 30%,25%, 30p,25p
    A single semantic shortcut is also allowed as an alignment value: center, top, right, bottom, left.
    If alignment is not specified, 0,0 value is used.

In URLs, % is an escape character and should be encoded with %25 escape sequence, e.g. /scale_crop/440x440/smart/80%25,80%25/. For convenience, you can use the p shortcut for percent which doesn't require encoding.

Smart crop to an object
/scale_crop/1024x1024/smart/
Smart cropping

Content-aware mechanics of the smart crop are based on the following methods of image analysis:

  • Face Detection, :type is set to smart_faces. Face detection identifies and locates human faces in digital images.
  • Object Detection, :type is set to smart_object. Object detection identifies and locates most visually important regions in the image.
  • Corner Points Detection, :type is set to smart_points. This method analyses image pixels to find the high contrast corners in the image, useful for abstract, landscape, and art images. The corner points have much less semantic information than other methods and designed to be used as a fallback.

The methods you include separating by underscore are applied sequentially. The algorithm switches to the next method only if no regions were found by the previous one. For example smart_faces_objects_points applies face detection initially as the first step. Only when no faces are found on an image, the object detection will be used. Finally, when no objects are found, the corner points will be detected.

Cropped image, auto centered on a face
smart_faces_objects
Centering on faces or objects
when no faces found
Cropped image, auto centered on objects and faces
smart_objects_faces
Centering on objects or faces
when no objects found
Image centering on hard edges
smart_points
Centering on corner points

Smart crop always tries to save the maximum amount of pixels from the original image. It first applies scale operation and then cuts horizontal or vertical excesses.

Set fill color

-/setfill/:color/

Sets the fill color used with crop, stretch or when converting an alpha channel enabled image to JPEG.

  • :color — color in hexadecimal notation with optional transparency.
    Example: 99ff99, 9f9, 99ff9920.
Image, cropped to center, resized on a white background
/preview/440x380/-
/crop/440x380/center/
Image, cropped to center, resized with a color filled background
/preview/440x380/-
/setfill/ece3d2/-
/crop/440x380/center/
PNG image with alpha channel converted to JPEG with a background fill
/setfill/ece3d2/-
/format/jpeg/

Zoom objects

-/zoom_objects/:zoom/

Zoom objects operation is best suited for images with solid or uniform backgrounds.

  • :zoom — sets the object's size in the image from 1 to 100.

As a result of the operation, the object's size in the image will change. The resolution of the image after the operation will remain the same. For example, an object in an image with applied operation -/zoom_objects/100/ is the object that occupies the maximum area of this image.


/zoom_objects/20/

/zoom_objects/50/

/zoom_objects/100/