const getImageBase64 = image => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
ctx.drawImage(image, 0, 0, image.width, image.height)
const extension = image.src
.substring(image.src.lastIndexOf('.') + 1)
.toLowerCase()
return canvas.toDataURL('image/' + extension, 1)
}
export const downloadImage = (url, downloadName) => {
const link = document.createElement('a')
link.setAttribute('download', downloadName)
const image = new Image()
image.src = url + '?timestamp=' + new Date().getTime()
image.setAttribute('crossOrigin', 'Anonymous')
image.onload = () => {
link.href = getImageBase64(image)
link.click()
}
}