Loading

js图片URL转base64

export default function imgUrlToBase64 (url, callback, outputFormat) {
  var canvas = document.createElement('CANVAS')
  var ctx = canvas.getContext('2d')
  var img = new Image()
  img.crossOrigin = 'Anonymous'
  img.onload = function () {
    canvas.height = img.height
    canvas.width = img.width
    ctx.drawImage(img, 0, 0)
    var dataURL = canvas.toDataURL(outputFormat || 'image/png')
    //回调函数传递转换完成的base64编码
    callback.call(this, dataURL)
    canvas = null
  }
  img.src = url
}

posted @ 2022-09-21 13:59  资深if-else侠  阅读(964)  评论(0编辑  收藏  举报