Cropping

Last updated: 2021-07-07 14:28:38

    Overview

    CI uses the imageMogr2 API to crop images, such as regular cropping, scaling and cropping, inscribed circle cropping, rounded corner cropping, and smart cropping.

    API Format

    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.

    Parameters

    Parameter Description
    download_url URL of the input image, formatted as <bucketname-appid>.cos.<picture region="">.<domain>.com/<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.

    Regular cropping

    Operation name: cut.

    Parameter Description
    &dxlt;width> Width of the output image
    &dxlt;height> Height of the output image
    &dxlt;dx> Horizontal offset with respect to the upper-left vertex
    &dxlt;dy> Vertical offset with respect to the upper-left vertex
    Note:

    The values should be greater than 0 and smaller than the width/height of the input image.

    Scaling and cropping

    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 that of the input image, respectively.

    During the crop operation, you can also use the gravity parameter to specify the location of the start point. For more information, see the example of the scaling and cropping.

    Inscribed circle cropping

    Operation name: iradius.

    Parameter Description
    /iradius/&dxlt;radius> Inscribed circle cropping. radius indicates the radius of the inscribed circle. The value is an integer that is greater than 0 and smaller than half of the smallest edge of the input image. The center of the inscribed circle is the center of the image. This parameter is not supported when the image format is GIF.

    Rounded corner cropping

    Operation name: rradius

    Parameter Description
    /rradius/ Rounded corner cropping. radius indicates the rounded corner edge radius of the image. The value is an integer that is greater than 0 and smaller than half of the smallest edge of the original image. The rounded corner is tangent to the edge of the original image. This parameter is not supported when the image format is gif.

    Smart cropping

    Operation name: scrop.

    Parameter Description
    /scrop/x Scales and crops an image based on the face position in the image. The width and height of the target image are Width and Height.

    3x3 Grid Position Diagram

    The 3x3 grid position diagram is as follows. Once you specify the gravity parameter to position your watermark, the corresponding red dot becomes the reference point of the square, and offsets will be relative to this point.

    Note:

    • If gravity is set to center, dx and dy are invalid.
    • If gravity is set to north or south, dx is invalid and the watermark will be centered horizontally.
    • If gravity is set to west or east, dy is invalid and the watermark will be centered vertically.

    Example

    Regular cropping

    This example shows you how to horizontally move rightward by 100 pixels and vertically move downward by 10 pixels relative to the upper-left vertex of the image and specify the size of the target image as 600x600 for cropping.

    http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?imageMogr2/cut/600x600x100x10
    

    Input image:

    Output image:

    Scaling and cropping

    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:

    Inscribed circle cropping

    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:

    Rounded corner cropping

    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:

    Smart cropping

    This example shows you how to scale and crop an image to 100x600 based on the face position 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.

    Smart cropping with a signature carried

    This example processes the image as the example above but also adds a signature, which 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.