All Products
Search
Document Center

Object Storage Service:Resize images

Last Updated:Feb 04, 2024

You may want to resize an image to accelerate image loading, reduce storage usage, lower bandwidth costs, or present the image in a preferred size. Object Storage Service (OSS) provides the resize action that you can use to resize an image in a bucket. This topic describes the parameters used in the resize action and provides some examples of image resizing.

Parameter description

Action: resize

Resize an image based on the specified height and width

  • Parameters

    Parameter

    Required

    Description

    Value range

    m

    Yes

    Specifies the type of the resize action.

    • lfit: OSS proportionally resizes the source image as large as possible in a rectangle based on the specified width and height. This is the default value.

    • 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 with a specific color.

    • fixed: OSS forcibly resizes the source image based on the specified width and height.

    For more information about examples of each resizing type, see Examples.

    Important
    • If you set this parameter to lfit or mfit, the aspect ratio (the ratio of the width to the height) of the source image is rounded to an integer 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.

    w

    No

    Specifies the width to which you want to resize the image.

    [1,16384]

    h

    No

    Specifies the height to which you want to resize the image.

    [1,16384]

    l

    Yes

    Specifies the length of the longer side to which you want to resize the image.

    Note

    The longer side is the side for which the ratio of the source length to the target length 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]

    s

    Yes

    Specifies the length of the shorter side to which you want to resize the image.

    Note

    The shorter side is the side for which the ratio of the source length to the target length 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]

    limit

    No

    Specifies whether to resize the image when the resolution of the target image is higher than the resolution of the source image.

    • 1: This is the default value. OSS returns the image that is processed based on the resolution of the source image. The size of the returned image may be different from the size of the source image.

    • 0: OSS resizes the source image based on the specified value.

    Note

    The size of a GIF image can only be reduced.

    color

    Yes (only when the value of m is pad)

    If you set the resizing 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, and the h parameter is set to 80. The source image is resized to different sizes when you specify different resize types.

    lfit

    • Proportional resizing: The aspect ratio of the resized image must be equal to the aspect ratio of the source image. If the width of the resized image is 150 pixels, the height of the resized image is 75 pixels. If the height of the resized image is 80 pixels, the width of the resized image is 160 pixels.

    • Maximum image size in a rectangle based on the specified width and height: The width and height of the resized image cannot exceed 150 pixels and 80 pixels, respectively.

    In this case, the size of the resized image is 150 × 75 pixels.

    lfit

    mfit

    • Proportional resizing: The aspect ratio of the resized image must be equal to the aspect ratio of the source image. If the width of the resized image is 150 pixels, the height of the resized image is 75 pixels. If the height of the resized image is 80 pixels, the width of the resized image 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.

    In this case, the size of the resized image is 160 × 80 pixels.

    mfit

    fill

    The fill resize type is used to proportionally resize the source image to an image as small as possible outside a rectangle with the specified w and h as its side lengths and then crop the image from center to the specified width and height. In this case, the source image is resized to 160 × 80 pixels. The resulting image is then cropped to a width of 150 pixels from center, producing a final image of 150 × 80 pixels.fill

    pad

    The pad resize type is used to proportionally resize the source image to an image as large as possible in a rectangle with the specified w and h as its side lengths and then fill the remaining empty space in the rectangle based on the specified width and height to produce a final image that has the intended size. In this case, the source image is resized to 150 × 75 pixels. The height of the resulting image is filled to 80 pixels from center, producing a final image of 150 × 80 pixels.pad

    fixed

    The fixed resize type resizes the source image based on the specified width and height. If the aspect ratio of the intended image is different from that of the source image, the resulting image has distortions.fixed

Resize an image by percentage

Parameter

Required

Description

Value range

p

Yes

Specifies the percentage by which you want to resize the image.

[1,1000]

A value smaller than 100 specifies that the image size is reduced. A value greater than 100 specifies that the image is enlarged.

Usage notes

  • Limits on source images

    • The following image formats are supported: JPG, PNG, BMP, GIF, WebP, and TIFF.

      Important
      • The size of a GIF image can be reduced based on the specified width and height but cannot be reduced by percentage. GIF images become static images if you reduce the sizes of the images by percentage.

      • GIF images cannot be enlarged.

    • A source image cannot exceed 20 MB in size. You can apply to adjust the limit on the size of source images in the Quota Center.

      Note

      The limit on the size of source images is adjustable only when you use image resizing parameters to resize images.

    • The width or height of a source image cannot exceed 30,000 pixels. The total number of pixels of a source image cannot exceed 250 million.

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

  • Limits on resized images

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

  • Resizing priorities

    If you specify parameters both for resizing based on the width and height and for proportionally resizing in a URL, the image is resized based on the specified width and height.

  • Image resizing 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 of 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 or height. For example, if you resize the height of a source image of 200 × 100 pixels to 100 pixels, the width of the source image is also 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

You can use object URLs, OSS SDKs, or API operations to configure IMG parameters that are used to process images. In this example, object URLs are used. You can use object URLs to configure IMG parameters only for public-read images. If you want to configure IMG parameters for private images, use OSS SDKs or API operations. For more information, see IMG implementation modes.

This section provides examples on how to resize an image in the oss-console-img-demo-cn-Hangzhou bucket in the China (Hangzhou) region. The image has 400 × 300 pixels and is hosted at the following URL:

FAQ

How do I access a compressed 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 Share objects with object URLs.

How do I compress an image whose ACL is private?

If the ACL of the image that you want to compress is private, you can use OSS SDKs to compress the image by including your signature and the compression parameters in the URL. You cannot compress an image whose ACL is private by directly adding the compression 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.

What do I do if image enlargement by width or height fail to produce the desired result?

To enlarge an image by width or height, set the limit parameter to 0. Otherwise, the enlargement does not take effect.

The following URL enlarges an image by height from 300 pixels to 500 pixels.

https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_500,limit_0