Skip to main content
Version: 3.x

Taro.compressImage(option)

Compresses images. Multiple types of compression quality are available.

Reference

Type

(option: Option) => Promise<SuccessCallbackResult>

Parameters

Option

PropertyTypeRequiredDescription
srcstringYesThe path to the image. It can be a relative path, a temporary file path, or a file storage path.
qualitynumberNoCompression quality. The value range is 0-100. A smaller value indicates poorer quality and higher compression rate (only for jpg).
complete(res: any) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: any) => voidNoThe callback function for a failed API call
success(res: Result) => voidNoThe callback function for a successful API call

SuccessCallbackResult

PropertyTypeDescription
tempFilePathstringThe temporary file path to the compressed image
errMsgstringCall result

Sample Code

Taro.compressImage({
src: '', // Path to the image
quality: 80 // Compression quality
})

API Support

APIWeChat Mini-ProgramH5React Native
Taro.compressImage✔️✔️