I'm working on an offline mode for a simple application, and I'm using Indexeddb (PounchDB as a library), I need to convert an Image to Base64 or BLOB to be able to save it.
I have tried this code which works for only one Image (the Image provided) I don't know why it doesn't work for any other image:
function convertImgToBase64URL(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'), dataURL;
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}
convertImgToBase64URL('http://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png', function(base64Img){
alert('it works');
$('.output').find('img').attr('src', base64Img);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"> <img> </div>
It works fine, but only with one Image, If I try any other Image it doesn't work
The image you're using has CORS headers
Accept-Ranges : bytes
Access-Control-Allow-Origin : * // this one
Age : 69702
Connection : keep-alive
which is why it can be modified in a canvas like that.
Other images that don't have CORS headers are subject to the same-origin policy, and can't be loaded and modified in a canvas in that way.
I need to convert Image URL to Base64
See
Understanding the HTML5 Canvas image security rules
Browser Canvas CORS Support for Cross Domain Loaded Image Manipulation
Why does canvas.toDataURL() throw a security exception?
One possible workaround would be to utilize FileReader
to convert img
, or other html
element to a data URI
of html
, instead of only img
src
; i.e.g., try opening console
at jsfiddle http://jsfiddle.net/guest271314/9mg5sf7o/ , "right-click" on data URI
at console
, select "Open link in new tab"
var elem = $("div")[0].outerHTML;
var blob = new Blob([elem], {
"type": "text/html"
});
var reader = new FileReader();
reader.onload = function (evt) {
if (evt.target.readyState === 2) {
console.log(
// full data-uri
evt.target.result
// base64 portion of data-uri
, evt.target.result.slice(22, evt.target.result.length));
// window.open(evt.target.result)
};
};
reader.readAsDataURL(blob);
Another possible workaround approach would be to open image in new tab , window
; alternatively an embed
, or iframe
element ; utilize XMLHttpRequest to return a Blob
, convert Blob
to data URI
var xhr = new XMLHttpRequest();
// load `document` from `cache`
xhr.open("GET", "", true);
xhr.responseType = "blob";
xhr.onload = function (e) {
if (this.status === 200) {
// `blob` response
console.log(this.response);
var reader = new FileReader();
reader.onload = function(e) {
// `data-uri`
console.log(e.target.result);
};
reader.readAsDataURL(this.response);
};
};
xhr.send();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With