You can use custom crop parameters to crop a rectangular image based on a specific size from a source image stored in Object Storage Service (OSS). This topic describes how to crop images and provides examples.
Parameters
Operation: crop.
The following table describes the parameters.
Parameter | Description | Value range |
---|---|---|
w | The width of the area that you want to crop. | [0, image width] Default value: the maximum value. |
h | The height of the area that you want to crop. | [0, image height] Default value: the maximum value. |
x | The X coordinate of the area that you want to crop. The default value is the X coordinate of the upper-left corner of the image. | [0, image bound] |
y | The Y coordinate of the area that you want to crop. The default value is the Y coordinate of the upper-left corner of the image. | [0, image bound] |
g | The position of the area that you want to crop in a 3 x 3 grid. The image is located in a 3 x 3 grid. The grid has nine tiles. |
For more information about how to calculate the position of each tile, see the following table. |
The following table describes how to calculate the position of each tile in a 3 x 3 grid. srcW specifies the width of the source image and srcH specifies the height of the source image.
Tile | Calculation method |
---|---|
nw | 0, 0 |
north | srcW/2 - w/2, 0 |
ne | srcW - w, 0 |
west | 0, srcH/2 - h/2 |
center | srcW/2 - w/2, srcH/2 - h/2 |
east | srcW - w, srcH/2 - h/2 |
sw | 0, srcH - h |
sourth | srcW/2 - w/2, srcH - h |
se | srcW - w, srcH - h |
Usage notes
- If the specified starting abscissa or ordinate values exceed those of the source image, the system returns
BadRequest
and the Advance cut's position is out of image. error message. - If the width and height specified from the starting point exceed those of the source image, the source image is cropped to the boundaries.
Examples
You can use object URLs and OSS SDKs, or call API operations to configure Image Processing (IMG) parameters that are used to process images. In this topic, 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 call API operations. For more information, see IMG implementation modes.
In the following examples, an image in a bucket named oss-console-img-demo-cn-hangzhou-3az in the China (Hangzhou) region is used as the source image. The image can be accessed over the Internet by using the following URL:
- Crop an image from the starting point (800, 50) to the boundariesConfigure the parameters based on the following requirements:
- From the starting point (800, 50):
crop,x_800,y_50
. - To the boundaries: By default, the maximum values of w and h are used to crop the image. You can ignore the w and h parameters.
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/crop,x_800,y_50 - From the starting point (800, 50):
- Crop an area of 300 × 300 pixels from the starting point (800, 500) Configure the parameters based on the following requirements:
- From the starting point (800, 500):
crop,x_800,y_500
. - An area of 300 × 300 pixels:
w_300,h_300
.
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/crop,x_800,y_500,w_300,h_300
- From the starting point (800, 500):
- Crop an area of 900 × 900 pixels in the lower-right corner of the source imageConfigure the parameters based on the following requirements:
- From the starting point in the lower-right corner of the source image:
crop,g_se
- An area of 900 × 900 pixels:
w_900,h_900
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/crop,w_900,h_900,g_se
- From the starting point in the lower-right corner of the source image:
- Crop an area of 900 × 900 pixels in the lower-right corner of an image and stretch the cropped area downward by (100, 100)Configure the parameters based on the following requirements:
- From the starting point in the lower-right corner of an image and stretch the cropped area downward by (100, 100):
crop,g_se,x_100,y_100
- An area of 900 × 900 pixels:
w_900,h_900
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/crop,x_100,y_100,w_900,h_900,g_se
- From the starting point in the lower-right corner of an image and stretch the cropped area downward by (100, 100):