何文海
发布于 2023-10-30 / 0 阅读 / 0 评论 / 0 点赞

把图片转成base64

 把图片转成base64 
getBase64(imgUrl) {
		return new Promise((resolve, reject) => {
			const img = new Image()
			img.setAttribute('crossOrigin', 'anonymous')
			img.src = imgUrl
			img.onload = function () {
				const canvas = document.createElement('canvas')
				canvas.width = img.width
				canvas.height = img.height
				const ctx = canvas.getContext('2d')
				ctx.drawImage(img, 0, 0, img.width, img.height)
				const dataURL = canvas.toDataURL('image/png')
				resolve(dataURL)
			}

			img.onerror = () => {
				resolve(imgUrl)
			}
		})
	},

评论