You can use custom crop parameters to crop a rectangular image based on a specified 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 | Valid value |
---|---|---|
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 #table_xdo_tzc_rfu. |
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 |
south | 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 error message Advance cut's position is out of image.. - 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 process images by using object URLs, OSS SDKs, or API operations. In this example, object URLs are used. For more information about how to use OSS SDKs and API operations to process images, see IMG implementation modes.
In the following examples, an image in a bucket named oss-console-img-demo-cn-hangzhou 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 (100, 50) to the boundaries
Configure the parameters based on the following requirements:
- From the starting point (100, 50):
crop,x_100,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.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/crop,x_100,y_50 - From the starting point (100, 50):
- Crop an area of 100 × 100 pixels from the starting point (100, 50)
Configure the parameters based on the following requirements:
- From the starting point (100, 50):
crop,x_100,y_50
- An area of 100 × 100 pixels:
w_100,h_100
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/crop,x_100,y_50,w_100,h_100
- From the starting point (100, 50):
- Crop an area of 200 × 200 pixels in the lower-right corner of the source image
Configure 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 200 × 200 pixels:
w_200,h_200
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/crop,w_200,h_200,g_se
- From the starting point in the lower-right corner of the source image:
- Crop an area of 200 × 200 pixels in the lower-right corner of an image and stretch
the cropped area downward by (10, 10)
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 (10, 10):
crop,g_se,x_10,y_10
- An area of 200 × 200 pixels:
w_200,h_200
The URL used to process the image is in the following format: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/crop,x_10,y_10,w_200,h_200,g_se
- From the starting point in the lower-right corner of an image and stretch the cropped
area downward by (10, 10):