my.compressImage compresses large images to fulfill the size restrictions from some mini programs. You can specify the compression quality level based on your needs.
my.compressImage({
apFilePaths: ['path/to/image1.jpg', 'path/to/image2.jpg'],
compressLevel: 2,
success: (res) => {
console.log('Compressed images:', res.apFilePaths);
},
fail: (err) => {
console.log('Compression failed:', err);
}
});
| Property | Type | Required | Description |
|---|---|---|---|
apFilePaths | Array | Yes | An array of the image paths to be compressed. |
compressLevel | int | No | The image quality after the compress. Valid values are: • 0: Low quality• 1: Medium quality• 2: High quality• 4: The quality depends on the network. When the network is Wi-Fi, the image is compressed to the high-quality image. Otherwise, the image is compressed to the medium-quality image.The default value is 4. |
success | Function | No | The callback method that indicates a successful call. |
fail | Function | No | The callback method that indicates a failed call. |
complete | Function | No | The callback method that indicates the call is completed (this will be executed regardless of whether the call succeeds or fails). |
| Property | Type | Required | Description |
|---|---|---|---|
apFilePaths | Array | Yes | The local file path of the compressed image. |
| Error Code | Error Message | Description |
|---|---|---|
2 | Compress failed | Failed to compress all images. |
| Invalid parameter | Failed to compress one of all images. |
compressLevel parameter controls the quality-size tradeoff4 (default) automatically adjusts based on network conditions