Skip to content

Image

chooseImage

TIP

The API usage is as follows: wx.chooseImage(Object object)

  • Functional description: Select an image from the local album or take a photo with the camera.

  • Parameters and descriptions: Object object

    PropertiesTypeLegal valuesDefault valueRequiredDescription
    countnumber-9NoThe maximum number of images that can be selected
    sizeTypeArray.<string>original:Original image
    compressed:Compressed image
    ['original', 'compressed']NoThe size of the selected image
    sourceTypeArray.<string>album:Original image
    camera:Compressed image
    ['album', 'camera']NoSelect the source of the image
    successFunction--NoCallback function for successful interface call
    failFunction--NoCallback function for failed interface call
    completeFunction--NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilePathsArray.<string>List of local temporary file paths for images (local path)
    tempFilesArray.<Object>List of local temporary files for images
  • Structure of res.tempFiles

    PropertiesTypeDescription
    pathstringLocal temporary file path
    sizenumberLocal temporary file size, unit B
  • Sample code:

js
wx.chooseImage({
  count: 1,
  sizeType: ['original', 'compressed'],
  sourceType: ['album', 'camera'],
  success(res) {
    // tempFilePath can be used as the src attribute in an img tag to display the image
    const tempFilePaths = res.tempFilePaths
  }
})

compressImage

TIP

The API usage is as follows: wx.compressImage(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Compress the image interface, with optional compression quality.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    srcstring-YesImage path, support network images, temporary paths, code package paths
    qualitynumber80NoCompression quality, range 0~100, the smaller the value, the lower the quality and the higher the compression rate (only valid for jpg)
    compressedWidthnumber-NoThe width of the compressed image, in px, if not filled in, it will be proportionally scaled by compressedHeight by default
    compressedHeightnumber-NoThe height of the compressed image, in px, if not filled in, it will be proportionally scaled by compressedWidth by default
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilePathstringThe temporary file path of the compressed image (local path)
  • Sample code:

js
wx.compressImage({
  src: '', // Image path
  quality: 80 // Compression quality
})

getImageInfo

TIP

The API usage is as follows: wx.getImageInfo(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Get image information, network images must first configure the download domain name to take effect

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    srcstring-YesThe path of the image can be a relative path, temporary file path, storage file path, network image path
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    widthnumberThe original width of the image, in px, regardless of rotation
    heightnumberThe original height of the image, in px, regardless of rotation
    pathstringThe local path of the image
    orientationstringDevice orientation when taking pictures
    typestringImage format
  • res.orientationLegal value

    ValueDescription
    upDefault orientation (taking pictures with the phone held horizontally), corresponding to 1 in Exif, or no orientation information
    up-mirroredSame as up, but mirrored, corresponding to 2 in Exif
    downRotate 180 degrees, corresponding to 3 in Exif
    down-mirroredSame as down, but mirror flipped, corresponding to 4 in Exif
    left-mirroredSame as left, but mirror flipped, corresponding to 5 in Exif
    right91 degrees clockwise rotation, corresponding to 6 in Exif
    right-mirroredSame as right, but mirror flipped, corresponding to 7 in Exif
    left91 degrees counterclockwise rotation, corresponding to 8 in Exif
  • res.typeLegal value

    ValueDescription
    unknownUnknown format
    jpegjpeg compression format
    pngpng compression format
    gifgif compression format
    tifftiff compression format
  • Sample code:

js
wx.getImageInfo({
  src: 'images/a.jpg',
  success(res) {
    console.log(res.width)
    console.log(res.height)
  },
})

wx.chooseImage({
  success(res) {
    wx.getImageInfo({
      src: res.tempFilePaths[0],
      success(res) {
        console.log(res.width)
        console.log(res.height)
      },
    })
  },
})

previewImage

TIP

The API usage is as follows: wx.previewImage(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Preview the image in full screen in a new page. During the preview, users can save the image, send it to friends, etc.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    urlsArray.<string>-YesList of image links to be previewed. Supports cloud file ID
    currentstringThe first of urlsNoLink of the currently displayed image
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.previewImage({
  current: '', // The HTTP link to the currently displayed image.
  urls: [], // List of HTTP image links to be previewed.
})

previewMedia

TIP

The API usage is as follows: wx.previewMedia(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Preview pictures and videos.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    sourcesArray.<Object>-YesList of resources to be previewed
    currentnumber0NoThe currently displayed resource number
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

saveImageToPhotosAlbum

TIP

The API usage is as follows: wx.saveImageToPhotosAlbum(Object object)
This API is supported by mini programs, but not by mini games.

TIP

User authorization is required before calling scope.writePhotosAlbum

  • Functional description: Save the image to the system album.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    filePathstring-YesImage file path, which can be a temporary file path or a permanent file path (local path). Network image paths are not supported
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.saveImageToPhotosAlbum({
  success(res) {},
})