10 examples of 'convert image url to base64 javascript' in JavaScript

Every line of 'convert image url to base64 javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
658function img2base64(imgsrc) {
659 if (typeof imgsrc == 'undefined') return "";
660
661 const NSURI = "http://www.w3.org/1999/xhtml";
662 var img = new Image();
663 var that = this;
664 var canvas,
665 isCompleted = false;
666 img.onload = function() {
667 var width = this.naturalWidth,
668 height = this.naturalHeight;
669 canvas = document.createElementNS(NSURI, "canvas");
670 canvas.width = width;
671 canvas.height = height;
672 var ctx = canvas.getContext("2d");
673 ctx.drawImage(this, 0, 0);
674 isCompleted = true;
675 };
676 img.onerror = function() {
677 Components.utils.reportError("Count not load: " + imgsrc);
678 isCompleted = true;
679 };
680 img.src = imgsrc;
681
682 var thread = Cc['@mozilla.org/thread-manager;1'].getService().mainThread;
683 while (!isCompleted) {
684 thread.processNextEvent(true);
685 }
686
687 var data = canvas ? canvas.toDataURL("image/jpeg", 1) : "";
688 canvas = null;
689 return data;
690}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

10export function getDataUrlFromBase64(base64) {
11 if (!base64.startsWith('data:image/*;base64,')) {
12 return `data:image/*;base64,${base64}`;
13 }
14 return base64;
15}
626function getImageFromBase64(base64) {
627 var img = new Image();
628 img.src = "data:image/jpeg;base64, " + base64;
629
630 return img;
631}
210function getBase64(img, callback) {
211 const reader = new FileReader()
212 reader.addEventListener('load', () => callback(reader.result))
213 reader.readAsDataURL(img)
214}
79value: function toBase64(src, w, h) {
80 if (this.images[src]) return this.images[src];
81 this.canvas.width = w;
82 this.canvas.height = h;
83 var data = this.canvas.toDataURL('image/png');
84 this.images[src] = data;
85 return data;
86}
55export function decode(base64url: string, encoding: string = "utf8"): string {
56 return new Buffer(toBase64(base64url), "base64").toString(encoding);
57}
99function blobToBase64Png(image) {
100 var png = image.getImage();
101 var chunks = [];
102
103 return new Promise(function(resolve) {
104 png.pack();
105 png.on('data', function(chunk) {
106 chunks.push(chunk);
107 });
108 png.on('end', function() {
109 var result = Buffer.concat(chunks);
110 resolve('data:image/png;base64,' + result.toString('base64'));
111 });
112 });
113}
7function base64url(buf) {
8 return buf.toString('base64')
9 .replace(/\+/g, '-') // Convert '+' to '-'
10 .replace(/\//g, '_'); // Convert '/' to '_'
11}
23function base64ToUrl(s) {
24 return s.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
25}
5function urlize(base64) {
6 return base64.replace(/\+/g, '-')
7 .replace(/\//g, '_')
8 .replace(/=/g, '')
9}

Related snippets