All the solutions can work, yet I think the issue is discussed here: CORS errors trying to convert remote image to base64 data
Consider the following code:
$(function() {
function toDataURL(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
var imgUrl = $("#imgdiv > img").attr("src");
console.log("Img Url: " + imgUrl);
toDataURL(imgUrl, function(dataUrl) {
console.log('RESULT:', dataUrl)
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="imgdiv">
<img id="img" src="https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01">
</div>
When I test this it works, to a point, and then generates an error:
Img Url: https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01 js:31:11
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
If you have access to Server-Side Scripting, like PHP, this could be done in a snap.
Update
If URL data grab won't work for you then you must use Canvas method: CONVERT Image url to Base64
$(function() {
function getBase64Image(img) {
var canvas = $("<canvas>");
canvas.width(img.width());
canvas.height(img.height());
var ctx = canvas[0].getContext("2d");
ctx.drawImage(img[0], 0, 0);
var dataURL = canvas[0].toDataURL();
return dataURL;
}
function clearData(sStr) {
return sStr.replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
}
var imgUrl = $("#imgdiv > img");
var imgSrc = getBase64Image(imgUrl);
var imgBase = clearData(imgSrc);
console.log("Img Url: " + imgUrl.attr("src"));
console.log("Img Src: " + imgSrc);
console.log("Base64: " + imgBase);
$("#imgdiv").after("<img src='" + imgSrc + "'>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="imgdiv">
<img id="img" src="https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01">
</div>
This provides the following response:
Img Url: https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01
Img Src: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAAxUlEQVR4nO3BMQEAAADCoPVPbQhfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOA1v9QAATX68/0AAAAASUVORK5CYII=
Base64: iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAAxUlEQVR4nO3BMQEAAADCoPVPbQhfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOA1v9QAATX68/0AAAAASUVORK5CYII= js:33:11
Update 2
You can try using an API and see if you can have it convert the image for you.
$(function() {
function postToForm(url) {
$.post("https://www.askapache.com/online-tools/base64-image-converter/", {
http_remote_url: url,
http_compressimage: 1,
TF_nonce: "4bd57a0b93",
_wp_http_referer: "/online-tools/base64-image-converter/",
aatoolstoken: "1fe4rpn"
}, function(resp) {
console.log(resp);
});
}
var imgUrl = $("#imgdiv > img").attr("src");
console.log("Img Url: " + imgUrl);
postToForm(imgUrl);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="imgdiv">
<img id="img" src="https://firebasestorage.googleapis.com/v0/b/ingrum-id.appspot.com/o/images%2FMartian.jpg?alt=media&token=35aab234-2903-46b2-9a49-e48c100d4e01">
</div>
Again, this may encounter CORS issues and it looks like the site uses Tokens to help prevent abuse of their API.
If your web server supports Scripting, you may consider making your own API that you can use to convert a Image URL to Base64 data.