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
Parameter Required Description Value range m Yes Specifies 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.
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.
w Yes Specifies the width to which you want to resize the image. [1,16384] h Yes 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 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] 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 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] limit No Specifies 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.color Yes (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.
- 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.
- 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.
- 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.
- 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.
- lfit
Resize an image by percentage
Parameter | Required | Description | Value range |
---|---|---|---|
p | Yes | Specifies 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
.
- The following image formats are supported: JPG, PNG, BMP, GIF, WebP, and TIFF.
- 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:
- Proportionally resize the image
- Based on the width or heightConfigure the following parameters to resize the image:
- Resize the source image to a height of 100 pixels:
resize,h_100
- Set the resize type to lfit:
m_lfit
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit. - Resize the source image to a height of 100 pixels:
- Based on the longer side
Resize the source image based on the longer side that contains 100 pixels:
resize,l_100
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,l_100.
- Based on the width or height
- Resize the image based on the specified width and heightConfigure the following parameters to resize the image:
- Resize the source image to a width and a height of 100 pixels:
resize,h_100,w_100
- Set the resize type to fixed:
m_fixed
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_fixed,h_100,w_100. - Resize the source image to a width and a height of 100 pixels:
- Crop the image based on the specified width and height Configure the following parameters to resize the image:
- Resize the source image to a width and a height of 100 pixels:
resize,h_100,w_100
- Set the resize type to fill:
m_fill
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_fill,h_100,w_100. - Resize the source image to a width and a height of 100 pixels:
- Resize the source image based on the specified width and height, and fill the background with red Configure the following parameters to resize the image:
- Resize the source image to a width and a height of 100 pixels:
resize,h_100,w_100
- Set the resize type to pad:
m_pad
- Fill the background with red:
color_FF0000
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_pad,h_100,w_100,color_FF0000. - Resize the source image to a width and a height of 100 pixels:
- Proportionally resize an image
Configure the following parameters to resize the image:
Resize the source image by 50%:
resize,p_50
The URL used to apply the preceding parameters is https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,p_50.
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.