You can use the resize parameters to adjust the size of images that are stored in Object Storage Service (OSS). This topic describes the parameters that are used to resize images and provides examples on how to resize images.

Parameter description

Operation: resize

Resize an image based on the specified height and width

  • ParameterRequiredDescriptionValue range
    mYesSpecifies the type of the resize operation. Default value: lfit.
    • lfit: OSS proportionally resizes the source image as large as possible in a rectangle based on the specified width and height.
    • mfit: OSS proportionally resizes the source image as small as possible outside a rectangle based on the specified width and height.
    • fill: OSS proportionally resizes the source image as small as possible outside a rectangle, and then crops the resized image from the center based on the specified width and height.
    • pad: OSS resizes the source image as large as possible in a rectangle based on the specified width and height, and fills the empty space by using a specified color.
    • fixed: OSS forcibly resizes the source image based on the specified width and height.
    For more information, see the "Examples" section below the table.
    Important
    • If you set this parameter to lfit or mfit, the width and height ratio of the source image is rounded to an integer number if the ratio is a decimal.
    • If you specify the m parameter and specify a value for w or h, the values specified for l and s do not take effect.
    wYesSpecifies the width to which you want to resize the image. [1,16384]
    hYesSpecifies the height to which you want to resize the image. [1,16384]
    lYesSpecifies the length of the longer side to which you want to resize the image.
    Note The longer side is the side for which the source-to-target size ratio is larger. For example, if a source image is resized from 400 × 200 pixels to 800 × 100 pixels, the source-to-target size ratios are 0.5 (400/800) and 2 (200/100). 0.5 is smaller than 2. As a result, the side that contains 200 pixels is used as the longer side.
    [1,16384]
    sYesSpecifies the length of the shorter side to which you want to resize the image.
    Note The shorter side is the side for which the source-to-target size ratio is smaller. For example, if a source image is resized from 400 × 200 pixels to 800 × 100 pixels, the source-to-target ratios are 0.5 (400/800) and 2 (200/100). 0.5 is smaller than 2. As a result, the side that contains 400 pixels is used as the shorter side.
    [1,16384]
    limitNoSpecifies whether to resize the image when the resolution of the destination image is higher than the resolution of the source image. Default value: 1.
    • 1: OSS returns the image that is processed based on the resolution of the source image. The volume of the returned image may be different from the volume of the source image.
    • 0: OSS resizes the source image based on the specified value.
    Note You can only scale down GIF images.
    colorYes (only when the value of m is pad)If you set the resize type to pad, you can select a color to fill the empty space. RGB color values. For example, 000000 indicates black, and FFFFFF indicates white.

    Default value: FFFFFF (white).

  • Examples

    The size of the source image is 200 × 100 pixels. The w parameter is set to 150 pixels, and the h parameter is set to 80 pixels. The source image is resized to different sizes when you specify different resize types.

    • lfit
      • Proportional resizing: The value of the width and height ratio of the source image must be equal to the width and height ratio of the resized image. If the w parameter is set to 150 pixels, the value of the h parameter is 75 pixels. If the h parameter is set to 80 pixels, the value of the w parameter is 160 pixels.
      • Maximum image size in a rectangle based on the specified width and height: The value of the width and height ratio of the resized image cannot exceed 150 × 80 pixels.

      The size of the resized image is 150 × 75 pixels based on the preceding conditions.

      lfit
    • mfit
      • Proportional resizing: The value of the width and height ratio of the source image must be equal to the width and height ratio of the resized image. If the w parameter is set to 150 pixels, the value of the h parameter is 75 pixels. If the h parameter is set to 80 pixels, the value of the w parameter is 160 pixels.
      • Minimum image size outside a rectangle based on the specified width and height: The resized image must be a minimum rectangle whose size is greater than 150 × 80 pixels.

      The size of the resized image is 160 × 80 pixels based on the preceding conditions.

      mfit
    • fill
      The fill parameter is used to proportionally resize the source image as small as possible outside a rectangle and crop the resized image based on the specified width and height. The source image is resized to 160 × 80 pixels, and the w parameter is cropped to 150 pixels from the center to obtain a resized image of 150 × 80 pixels. fill
    • pad
      The pad parameter is used to resize the source image as large as possible in a rectangle and fill the empty space based on the specified width and height. The source image is resized to 150 × 75 pixels, and the h parameter is centered and filled to 80 pixels to obtain a resized image of 150 × 80 pixels. pad
    • fixed
      The fixed parameter is used to resize the image based on the specified width and height. If the width and height ratio of the image is different from the width and height ratio of the source image, the image is deformed. fixed

Resize an image by percentage

ParameterRequiredDescriptionValue range
pYesSpecifies the percentage to which you want to resize the image. [1,1000]

A percentage smaller than 100 specifies that the image is scaled down. A percentage greater than 100 specifies that the image is scaled up.

Usage notes

  • Limits on source images
    • The following image formats are supported: JPG, PNG, BMP, GIF, WebP, and TIFF.
      Important
      • GIF images can be scaled down based on the specified width and height but cannot be scaled down by percentage. GIF images become static images when you scale down the images by percentage.
      • GIF images cannot be enlarged.
    • The source image cannot exceed 20 MB in size. If you want to modify the limit on the size of the original image, submit a ticket.
    • The width or height of the source image cannot exceed 30,000 pixels. The total number of pixels of the source image cannot exceed 250 million.

      The total pixel number of a dynamic image, such as a GIF image, is calculated based on the following formula: Width × Height × Number of image frames. The total pixel number of a static image, such as a PNG image, is calculated based on the following formula: Width × Height.

  • Limits on resized images

    The width or height of a resized image cannot exceed 16,384 pixels. The total pixel number of the resized image cannot exceed 16,777,216.

  • Priority of resize operations

    If you use a URL to resize an image based on the specified height and width and proportion, the image is resized based on the specified height and width.

  • Resize an image based on the specified width or height
    • The source image is proportionally resized when proportional resizing is performed. For example, if you resize the height of a source image from 200 × 100 pixels to 100 pixels, the width of the source image is resized to 50 pixels.
    • The source image is resized based on the specified width and height. For example, if you resize the height of a source image from 200 × 100 pixels to 100 pixels, the width of the source image is resized to 100 pixels.
  • If the size of the resized image is larger than the size of the source image, the source image is returned. You can add the limit_0 parameter to enlarge the image. Example: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_500,limit_0.

Examples

This topic provides examples on how to resize an image in the oss-console-img-demo-cn-hangzhou bucket in the China (Hangzhou) region. The image is hosted at the following URL:

FAQ

How do I access a resized image if the access control list (ACL) of the image is private?

To access the image, add signature information to the URL of the image. For more information, see How do I obtain the URL of a single object or the URLs of multiple objects?

How do I compress an image if the ACL of the image is private?

If the ACL of the image that you want to compress is private, you can use OSS SDKs to add IMG operations to the URL of the object to compress the image. You cannot compress an image whose URL is private by adding parameters to the object URL. For more information, see Use OSS SDKs to process images.

Am I charged for sending image compression requests?

Yes, you are charged for sending image compression requests. For more information, see Data processing fees.

Is the traffic fee for an image resizing request calculated based on the source image or the resized image?

The traffic fee for an image resizing request is calculated based on the resized image. For example, if you request to resize a 20 MB image to a 2 MB image, the traffic fee generated for the request is calculated based on the resized image, which is 2 MB.