-
-
Save GibranPolonsky/a5bb3aa9badb648787b52e946339c93f to your computer and use it in GitHub Desktop.
Helper Class for Convert PNG to Canvas and Canvas to JPEG
This file contains 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
class ImageConversor{ | |
constructor(){ | |
} | |
static convertImageToCanvas(base64){ | |
let image = new Image(); | |
image.src = base64; | |
let canvas = document.createElement("canvas"); | |
canvas.width = image.width; | |
canvas.height = image.height; | |
canvas.getContext("2d").fillStyle = '#fff'; /// set white fill style | |
canvas.getContext("2d").fillRect(0, 0, canvas.width, canvas.height); | |
canvas.getContext("2d").drawImage(image, 0, 0); | |
return canvas; | |
} | |
static convertCanvasToImage(canvas) { | |
let image = new Image(); | |
image.src = canvas.toDataURL("image/jpeg"); | |
return image.src; | |
} | |
static hexToBase64(str) { | |
return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))); | |
} | |
} | |
export default ImageConversor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment