tencent cloud

Feedback

Image Processing

Last updated: 2024-03-04 10:30:23

    Overview

    CI provides the CIResponsiveTransformation (auto processing) and CITransformation (custom processing) image processing methods.
    Note:
    Format: Currently, JPG, BMP, GIF, PNG, and WEBP images can be processed, and HEIF images can be decoded and processed.
    Size: The input image cannot be larger than 32 MB, with its width and height not exceeding 30,000 pixels respectively, and the total number of pixels not exceeding 250 million. The width and height of the output image cannot exceed 9,999 pixels respectively. For an animated input image, the total number of pixels (width height number of frames) cannot exceed 250 million.
    Frames (for animated images): For GIF images, the number of frames cannot exceed 300.

    Resources

    For SDK APIs and parameters, see Scaling.

    CIResponsiveTransformation

    ResponsiveTransformation includes format conversion (converting formats according to OS versions) and scaling (scaling images according to the imageView control sizes and ScaleType) and is described as follows:
    
    1. CIResponsiveTransformation refers to processing images automatically.
    2. It automatically adjusts the size of the image according to the size of the current imageView control.
    3. It automatically selects the optimal image format according to the system version (HEIC for iOS 11 and later or the original format for earlier versions).
    
    Objective-C
    CIResponsiveTransformation * sTransform = [[CIResponsiveTransformation alloc]initWithView:imageView scaleType:ScaleTypeAUTOFit]
    swift
    let sTransform = CIResponsiveTransformation(view: nil, scale: ScaleType.autoFit)

    CITransformation

    The CITransformation class in the CI SDK has encapsulated the following basic image processing features of CI:
    
    
    Before using the basic image processing features of CI, instantiate the CITransformation class first as follows. Note that instantiation is required for all the operations described below. Objective-C
    CITransformation * transform = [CITransformation new];
    swift
    let transform = CITransformation();

    Scaling

    Note:
    For the API documentation, see Scaling.

    Scaling by percentage

    Objective-C
    // Scale to 50%
    [transform setZoomWithPercent:50 scaleType:ScalePercentTypeALL];
    
    // `scaleType` can be specified as the following types:
    // Scale the width without changing the height
    ScalePercentTypeOnlyWidth = 1,
    // Scale the height without changing the width
    ScalePercentTypeOnlyHeight,
    // Scale both the width and height
    ScalePercentTypeALL,
    swift
    transform.setZoomWithPercent(50, scale: ScalePercentType.ALL)

    Scaling with specified width and height

    Objective-C
    // Specify both the width and height as 100 and the scaling type as `ScaleTypeAUTOFit` for proportional scaling
    [transform setZoomWithWidth:100 height:100 scaleType:ScaleTypeAUTOFit];
    
    // `scaleType` can be specified as the following types in case of scaling by fixed height and width:
    // Specify both the height and width. The output image may be distorted.
    ScaleTypeAUTOFill = 1,
    // Specify the maximum width and height of a thumbnail as `Width` and `Height` respectively for proportional scaling.
    ScaleTypeAUTOFit,
    // Specify the minimum width and height of a thumbnail as `Width` and `Height` respectively for proportional scaling.
    ScaleTypeAUTOFITWithMin,
    // Specify the target image width as `Width` and pass in `0` for the height to proportionally scale down the height
    ScaleTypeOnlyWidth,
    // Specify the target image height as `Height` and pass in `0` for the width to proportionally scale down the width
    ScaleTypeOnlyHeight
    swift
    transform.setZoomWithWidth(10, height: 100, scaleType: ScaleType.autoFit)

    Proportional scaling

    This feature scales images by limiting the resolution of the output image (the total number of pixels cannot exceed the specified value).
    Objective-C
    // Scale an image so that the total number of its pixels does not exceed 1,000
    [transform setZoomWithArea:1000];
    swift
    transform.setZoomWithArea(1000)

    Cropping

    Note:
    For the API documentation, see Cropping.

    Regular cropping

    Objective-C
    // Perform cropping based on the specified target width, height, and horizontal offset and vertical offset relative to the upper-left vertex of the output image
    [transform setCutWithWidth:100 height:100 dx:30 dy:30];
    swift
    transform.setCutWithWidth(100, height: 100, dx: 30, dy: 30)

    Inscribed circle cropping

    radius specifies the radius of the inscribed circle, which should be an integer greater than zero and less than half of the shorter side of the input image. The center of the inscribed circle is the center of the image. This parameter is not supported for GIF images. Objective-C
    // Set the radius to 100.
    [transform setCutWithIRadius:100];
    swift
    transform.setCutWithIRadius(100)

    Rounded corner cropping

    radius specifies the radius of the rounded corner of the image, which should be an integer greater than zero and less than half of the shorter side of the input image. The two sides of the image are the tangent lines of the rounded corner. This parameter is not supported for GIF images. Objective-C
    // Set the corner radius to 100.
    [transform setCutWithRRadius:100];
    swift
    transform.setCutWithRRadius(100)

    Scaling and cropping

    Note:
    For more information on the 3x3 grid position diagram, see Cropping.
    Objective-C
    // Specify the width and height for scaling and cropping and change nothing in case of `0`
    [transform setCutWithCrop:100 height:100];
    
    // Specify width, height, and gravity for scaling and cropping
    [transform setCutWithCrop:100 height:100 gravity:CIGravityCenter];
    swift
    transform.setCutWithCrop(100, height: 100)
    transform.setCutWithCrop(100, height: 100, gravity: CloudInfiniteGravity.CIGravityCenter);

    Smart cropping

    Scale and crop an image based on the face position in the image. The width and height of the target image are specified by Width and Height respectively.
    Objective-C
    // Crop the face from the image and scale both the width and height to 100.
    [transform setCutWithScrop:100 height:100];
    swift
    transform.setCutWithScrop(100, height: 100)

    Rotating

    Note:
    For the API documentation, see Rotation.

    Rotating an image by a specified angle

    Rotate an image clockwise by a specified angle (0−360 degrees). By default, the image is not rotated.
    Objective-C
    // Rotate the image by 45 degrees.
    [transform setRotateWith:45];
    swift
    transform.setRotateWith(45)

    Rotating an image automatically

    Rotate an image automatically according to the EXIF data of the input image. Objective-C
    [transform setRotateAutoOrient];
    swift
    transform.setRotateAutoOrient()

    Format Conversion

    Note:
    For the API documentation, see Converting Format.

    Format conversion

    The target format can be TPG, JPG, BMP, GIF, PNG, HEIC, WEBP, YJPEG (which is optimized based on JPEG and is essentially JPG), or AVIF. The default format is the format of the input image.
    Objective-C
    // Convert an image into JPG.
    [transform setFormatWith:CIImageTypeJPG];
    
    // Specify input parameters:
    [transform setFormatWith:CIImageTypeTPG options:CILoadTypeUrlFooter];
    swift
    transform.setFormatWith(CIImageFormat.typeJPG);
    transform.setFormatWith(CIImageFormat.typeJPG, options: CILoadTypeEnum.urlFooter);
    CILoadTypeEnum
    // Loading type option 1: Include the accept header accept:image/ ***
    CILoadTypeAcceptHeader = 0,
    // Loading type option 2: Add imageMogr2/format/ *** after the URL
    // If option 2 is needed, use this value. If this parameter is not specified, option 1 will be used by default.
    CILoadTypeUrlFooter,
    Note:
    To use the HEIC format, iOS 11 or later is required. GIF images cannot be converted to HEIC.
    If you pass in parameters using CILoadTypeAcceptHeader and have specified other image processing rules, the header will not take effect. Instead, the SDK will use footer automatically.
    When using format conversion to convert an image to TPG format, you need to depend on the CloudInfinite/TPG module.
    When using format conversion to convert an image to AVIF format, you need to depend on the CloudInfinite/AVIF module.
    When using format conversion to convert an image to WEBP format, you need to depend on the SDWebImageWebPCoder library.
    pod 'CloudInfinite/TPG'
    pod 'CloudInfinite/AVIF'

    GIF optimization

    This feature only optimizes GIF images to reduce frames and image colors.
    FrameNumber=1: Cuts the input GIF to the default number of frames (30). If the original number of frames is greater than 30, the exceeded frames will be cut.
    FrameNumber=(1,100]: Compresses the image to the specified number of frames (`FrameNumber`).
    Objective-C
    [transform setCgif:50];
    swift
    transform.setCgif(50)

    Progressive mode of the output JPG image

    Mode can be 0 or 1. 0 (default value): Disables the progressive mode; 1: Enables the progressive mode. This parameter takes effect only when the output image is in JPG format.
    Objective
    [transform setInterlace:YES];
    swift
    transform.setInterlace(true)

    Quality Change

    CI allows you to change the quality of an image. For the API documentation, see Quality Change.
    Objective-C
    // Change the absolute quality of the image to 60
    // `type` is the change type as detailed below:
    [transform setQualityWithQuality:60 type:CIQualityChangeAbsolute];
    swift
    transform.setQualityWithQuality(60, type: CIQualityChangeEnum.absolute)
    CI supports change to three types of image quality: absolute, relative, and lowest. The absolute quality change supports forcibly specifying or not specifying the quality as follows:
    // The absolute quality of the image. The value can be 0–100. The default value is the original image quality.
    CIQualityChangeAbsolute = 1,
    
    // The absolute quality of the image. The value can be 0–100. The specified quality value is forcibly used, such as `90!`.
    CIQualityChangeAbsoluteFix,
    
    // The image quality relative to that of the input image. The value can be 0−100. For example, if the input image quality is 80 and `rquality` is set to `80`, the quality of the output image will be 64 (80 * 80%).
    CIQualityChangeRelative,
    
    // Set the lowest quality of the output image. Value range: 0−100.
    // If the input image quality is 85 and `lquality` is set to 80, the quality of the output image is 85.
    // If the input image quality is 60 and `lquality` is set to 80, the quality of the output image will be improved to 80.
    CIQualityChangeLowest,
    Note:
    This operation is only available for JPG and WEBP images.

    Gaussian Blurring

    Gaussian blurring supports a blur radius ranging from 1 to 50, and the standard deviation of the normal distribution must be greater than 0. For the API documentation, see Gaussian Blurring.
    Objective-C
    // Set the blur radius to 20 and the standard deviation of the normal distribution to 20.
    [transform setBlurRadius:20 sigma:20];
    swift
    transform.setBlurRadius(20, sigma: 20)
    Note:
    This operation is not available for GIF images.

    Sharpening

    The sharpening value can be an integer ranging from 10 to 300 (70 is recommended). The greater the value, the more obvious the sharpening. For the API documentation, see Sharpening.
    Objective-C
    // Set the sharpening value to 100.
    [transform setSharpenWith:100];
    swift
    transform.setSharpenWith(100)

    Watermarking

    Note:
    For the API documentation, see Image Watermarking and Text Watermarking.
    For more information on the 3x3 grid position diagram, see Text Watermarking.

    Image watermark

    Objective-C
    // imageUrl: Watermark image address.
    // gravity: Position of the text watermark in the 3x3 grid position diagram. Default value: `SouthEast`.
    // dx: Horizontal offset in px. Default value: `0`.
    // dy: Vertical offset in px. Default value: `0`.
    // blogo: Adaptation mode for an image watermark that is larger than the input image. Valid values:
    /// `1`: Scales the image watermark to the size of the input image.
    /// `2`: Crops the image watermark to the size of the input image.
    [tran setWaterMarkWithImageUrl:@"http://tpg-1253653367.cos.ap-guangzhou.myqcloud.com/google.jpg" gravity:0 dx:0 dy:0 blogo:0];
    swift
    transform.setWaterMarkWithImageUrl("", gravity: CloudInfiniteGravity.CIGravityCenter, dx: 0, dy: 0, blogo: CIWaterImageMarkBlogoEnum.none)

    Text watermark

    Objective-C
    // text: Watermark content
    // font: Watermark font
    // color: Font color. Default value: #3D3D3D.
    // dissolve: Text opacity. Value range: 1−100. Default value: `90`.
    // gravity: Position of the text watermark. Default value: `SouthEast`.
    // dx: Horizontal offset in px. Default value: `0`.
    // dy: Vertical offset in px. Default value: `0`.
    // batch: Whether to tile the text watermark. If this parameter is set to `1`, the text watermark will be tiled across the input image.
    // degree: The angle to rotate the text watermark. Value range: 0−360. Default value: `0`
    [transform setWaterMarkText:@"Tencent Cloud CI" font:nil textColor:nil dissolve:90 gravity:CIGravitySouth dx:100 dy:100 batch:YES degree:45];
    swift
    transform.setWaterMarkText("Tencent Cloud CI", font: nil, textColor: nil, dissolve: 90, gravity: CloudInfiniteGravity.CIGravityCenter, dx: 100, dy: 100, batch: true, degree: 45)

    Obtaining Image Average Hue

    CI uses the imageAve API to obtain the average hue of an image. For more information on the API, see Obtaining Image's Average Hue.
    Objective-C
    [transform setViewBackgroudColorWithImageAveColor:YES]
    swift
    transform.setViewBackgroudColorWithImageAveColor(true)

    Removing Image Metadata

    CI uses the imageMogr2 API to remove image metadata, including the EXIF data. For the API documentation, see Removing Image Metadata.
    Objective-C
    [transform setImageStrip];
    swift
    transform.setImageStrip()

    Combined Operations

    CITransformation * tran = [CITransformation new];
    // ***************Scaling*************
    // Scale to 50%
    [transform setZoomWithPercent:50 scaleType:ScalePercentTypeALL];
    
    
    // **Format conversion // Convert to TPG format [tran setFormatWith:CIImageTypeTPG]; // **Cropping // Crop the image. Width and height: 100; top left and left: 30 30 [tran setCutWithWidth:100 height:100 dx:30 dy:30];
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support