Get extension and body from base64 encoded image

Base 64 images start with data:image/. After this comes the image type. Then ;base64,. And finally the base64 encoded data.

We can use that information to find the file extension between data:image/ and ;base64,.

function base64encoded_image_extension_and_body(data) {
  var base64_part =;base64,/)
  var ext = data.substring("data:image/".length, base64_part)
  var body = data.substr(base64_part + 8) // ;base64, is 8 chars
  return { body, ext }

Edit on github
comments powered by Disqus
Click me