Skip to content

Instantly share code, notes, and snippets.

@mytholog
Forked from zzarcon/fetch_blob.js
Created November 29, 2016 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mytholog/e752a62dd5337450916744f750562180 to your computer and use it in GitHub Desktop.
Save mytholog/e752a62dd5337450916744f750562180 to your computer and use it in GitHub Desktop.
var url = 'https://ronreiter-meme-generator.p.mashape.com/meme?meme=Baby+Godfather&font_size=50&font=Impact&top=Thanks+m&bottom=Later';
var headers = new Headers({'X-Mashape-Key': 'API_KEY'});
var options = {
method: 'GET',
headers: headers,
mode: 'cors',
cache: 'default'
};
var request = new Request(url);
fetch(request, options).then((response) => {
response.arrayBuffer().then((buffer) => {
var base64Flag = 'data:image/jpeg;base64,';
var imageStr = arrayBufferToBase64(buffer);
document.querySelector('img').src = base64Flag + imageStr;
});
});
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = [].slice.call(new Uint8Array(buffer));
bytes.forEach((b) => binary += String.fromCharCode(b));
return window.btoa(binary);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment