Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Converts an image to a base64 string
- * Param[in] : url, the path to the image
- */
- function toBase64(url) {
- var canvas = document.createElement("canvas");
- var ctx = canvas.getContext('2d');
- var img = new Image();
- img.src = url;
- var height = img.height, width = img.width;
- canvas.height = height;
- canvas.width = width;
- ctx.drawImage(img, 0, 0, width, height);
- try {
- var dataURL = canvas.toDataURL("image/jpg");
- return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
- }
- catch (err) { console.log("ERROR " + err);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement