Created
September 23, 2024 16:04
-
-
Save fzn0x/9b0e1c27829f9ce08640ef1ea0176811 to your computer and use it in GitHub Desktop.
Fix issue with tainted canvas with huge data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fix issue for huge base64 image | |
const compressImageFromUrl = (url, quality = 0.7) => { | |
const img = new Image(); | |
img.crossOrigin = "anonymous"; | |
img.src = "/image/" + (url || "").split("/").pop(); | |
img.onload = () => { | |
const canvas = document.createElement("canvas"); | |
const ctx = canvas.getContext("2d"); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
ctx.drawImage(img, 0, 0); | |
const compressedDataUrl = canvas.toDataURL("image/jpeg", quality); | |
setCompressedImage(compressedDataUrl); | |
}; | |
img.onerror = (error) => { | |
console.error("Error loading the image: ", error); | |
}; | |
}; | |
const handleDownloadImage = async () => { | |
setIsDownload(true); | |
compressImageFromUrl(compressedImage, 0.1); | |
var node = document.getElementById("print"); | |
try { | |
const dataUrl = await htmlToImage.toJpeg(node, { quality: 1 }); | |
var link = document.createElement("a"); | |
link.href = dataUrl; | |
link.download = "YEY-campaign.jpg"; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} catch (error) { | |
console.error("oops, something went wrong!", error); | |
} finally { | |
setIsDownload(false); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment