COS uses the imageMogr2 API provided by CI to crop images, such as regular cropping, scaling and cropping, inscribed circle cropping, rounded corner cropping, and smart cropping.
An image can be processed:
Note:Image processing is charged by CI. For detailed pricing, please see the image processing prices of CI.
download_url?imageMogr2/cut/<width>x<height>x<dx>x<dy>
/crop/<imageSizeAndOffsetGeometry>
/iradius/<radius>
/rradius/<radius>
/scrop/<Width>x<Height>
Note:Spaces and line breaks above are for readability only and can be ignored.
PUT /<ObjectKey> HTTP/1.1
Host: <BucketName-APPID>.cos.<Region>.myqcloud.com
Date: GMT Date
Authorization: Auth String
Pic-Operations:
{
"is_pic_info": 1,
"rules": [{
"fileid": "exampleobject",
"rule": "imageMogr2/cut/<width>x<height>x<dx>x<dy>
/crop/<imageSizeAndOffsetGeometry>
/iradius/<radius>
/rradius/<radius>
/scrop/<Width>x<Height>"
}]
}
POST /<ObjectKey>?image_process HTTP/1.1
Host: <BucketName-APPID>.cos.<Region>.myqcloud.com
Date: GMT Date
Content-length: Size
Authorization: Auth String
Pic-Operations:
{
"is_pic_info": 1,
"rules": [{
"fileid": "exampleobject",
"rule": "imageMogr2/cut/<width>x<height>x<dx>x<dy>
/crop/<imageSizeAndOffsetGeometry>
/iradius/<radius>
/rradius/<radius>
/scrop/<Width>x<Height>"
}]
}
Note:Authorization: Auth String (For more information, please see Request Signature.)
Parameter | Description |
---|---|
download_url | URL of the input image, formatted as &dxlt;BucketName-APPID>.cos.&dxlt;Region>.myqcloud.com/&dxlt;picture name> Example: examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/picture.jpeg |
/ignore-error/1 | If this parameter is carried and the image failed to be processed because it is too large, the input image will be returned with no error reported. |
Operation name: cut.
Parameter | Description |
---|---|
&dxlt;width> | Width of the output image |
&dxlt;height> | Height of the output image |
&dxlt;dx> | Horizontal offset relative to the upper-left vertex |
&dxlt;dy> | Vertical offset relative to the upper-left vertex |
Note:The values should be greater than 0 and smaller than the width/height of the input image.
Operation name: crop.
Parameter | Description |
---|---|
/crop/&dxlt;Width>x | Width of the output image with the height unchanged. The value must be greater than 0 and smaller than the width of the input image. |
/crop/x&dxlt;Height> | Height of the output image with the width unchanged. The value must be greater than 0 and smaller than the height of the input image. |
/crop/&dxlt;Width>x&dxlt;Height> | Width and height of the output image. The values of width and height should be greater than 0 and smaller than those of the input image, respectively. |
When performing scaling and cropping, you can also use the gravity
parameter to specify the start position of the operation. For more information, see [Scaling and Cropping](#Scaling and cropping).
Operation name: iradius.
Parameter | Description |
---|---|
/iradius/&dxlt;radius> | Refers to the inscribed circle cropping feature, where radius specifies the radius of the inscribed circle. The value of radius is an integer that is greater than 0 and less than half the length of the shorter side of the original image. The center of the inscribed circle is the center of the original image. This parameter is not supported if the image format is GIF. |
Operation: rradius
Parameter | Description |
---|---|
/rradius/&dxlt;radius&dxgt; | Refers to the rounded corner cropping feature, where radius specifies the radius of the rounded corner of the image. The value of radius is an integer that is greater than 0 and less than half the length of the shorter side of the original image. The rounded corner is tangent to the edge of the original image. This parameter is not supported if the image format is GIF. |
Operation name: scrop.
Parameter | Description |
---|---|
/scrop/&dxlt;Width>x&dxlt;Height> | Refers to scaling and cropping based on the location of the human face in the image. Width and Height respectively specify the width and the height of the target image. |
The 3x3 grid position diagram is as follows. Once you specify the gravity
parameter for an operation, the corresponding red dot becomes the reference point, and offsets will be relative to this point.
Note:
- If
gravity
is set tocenter
,dx
anddy
are invalid.- If
gravity
is set tonorth
orsouth
,dx
is invalid.- If
gravity
is set towest
oreast
,dy
is invalid.
Note:Processing upon download is used as an example here, which does not store the output image in a bucket. If you need to store the output image, please see Persistent Image Processing and use Processing upon upload or Processing in-cloud data.
This example shows how to translate by 100 pixels horizontally to the right and 10 pixels vertically to the bottom relative to the top-left vertex of the image. It also shows how to crop the image according to the specified target image resolution of 600 × 600 pixels:
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/cut/600x600x100x10
Input image:
Output image:
This example shows you how to scale and crop an image to 300x400 by using the center as the reference point.
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/crop/300x400/gravity/center
Output image:
This example shows you how to perform inscribed circle cropping when the radius is 200.
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/iradius/200
Output image:
This example shows you how to perform rounded corner cropping when the radius is 100.
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/rradius/100
Output image:
This example shows how to crop an image to the specified resolution of 100 × 600 pixels based on the location of the human face in the image:
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/scrop/100x600
Note:If no face is recognized, the input image will be returned.
This example processes the image in the same way as in the example above except that a signature is carried. The signature is joined with other processing parameters using an ampersand (&):
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?q-sign-algorithm=<signature>&imageMogr2/scrop/100x600
Note:You can obtain the value of
<signature>
by referring to Request Signature.
To prevent unauthorized users from accessing or downloading the input image by using a URL that does not contain any processing parameter, you can add the processing parameters to the request signature, making the processing parameters the key of the parameter with the value left empty. The following is a simple example for your reference (it might have expired or become inaccessible). For more information, please see Request Signature.
http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?q-sign-algorithm=sha1&q-ak=AKID********************&q-sign-time=1593342360;1593342720&q-key-time=1593342360;1593342720&q-header-list=&q-url-param-list=watermark%252f1%252fimage%252fahr0cdovl2v4yw1wbgvzlteyntewmdawmdqucgljc2gubxlxy2xvdwquy29tl3nodwl5aw4uanbn%252fgravity%252fsoutheast&q-signature=26a429871963375c88081ef60247c5746e834a98&watermark/1/image/aHR0cDovL2V4YW1wbGVzLTEyNTEwMDAwMDQucGljc2gubXlxY2xvdWQuY29tL3NodWl5aW4uanBn/gravity/southeast
Was this page helpful?