Skip to content

Instantly share code, notes, and snippets.

@davidicus
Created November 11, 2020 04:47
Show Gist options
  • Save davidicus/f09c4dd6828e998e07412cc000cea12c to your computer and use it in GitHub Desktop.
Save davidicus/f09c4dd6828e998e07412cc000cea12c to your computer and use it in GitHub Desktop.
Get an image from a URL and covert it into a data url
const fetchDataURL = (url) =>
fetch(url)
.then((res) => res.arrayBuffer())
.then((ab) =>
btoa(
new Uint8Array(ab).reduce(
(data, byte) => data + String.fromCharCode(byte),
''
)
)
);
@davidicus
Copy link
Author

fetchImgFromURL('http://placekitten.com/200/300').then((uri) =>
      console.dir(`data:image/png;base64,${uri}`)
 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment