Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - get extension from base64 image

I have a base64 encoded image returned from a service and it looks like this:

/9j/4AAQSkZJRgABAQEASABIAAD/4Yp2aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/Pgo8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA0LjEtYzAzNiA0Ni4yNzcwOTIsIEZyaSBGZWIgMjMgMjAwNyAxNDoxNjoxOCAgICAgICAgIj4KICAgPHJkZjpSREYgeG1.... etc 

How can i detect / check the image extension?

like image 697
itsme Avatar asked Jan 11 '15 11:01

itsme


People also ask

How can I get MIME type from base64?

'function guessImageMime(data){ if(data. charAt(0)=='/'){ return "image/jpeg"; }else if(data. charAt(0)=='R'){ return "image/gif"; }else if(data. charAt(0)=='i'){ return "image/png"; } }' Thanks for your answer.

What is a b64 file?

Binary file encoded using base64 encoding; often used to convert text data into base64 data; enables more reliable transmission of e-mail attachments and other information; similar to a . MIME attachment.


2 Answers

A bit late but it seem the question was misunderstood. He just only had the base64 content of the image, not the full data URI.

I wrote here for anyone who encounters with this quest, you can read the first character of content content.charAt(0). By base64 image content if the first char is:

'/' : jpg

'i' : png

'R' : gif

'U' : webp

So for your case, it is 'jpg'.

like image 158
Up209d Avatar answered Sep 20 '22 15:09

Up209d


Was just tweaking with the string. May be this could help.

base64Data.substring("data:image/".length, base64Data.indexOf(";base64")) 
like image 32
Saptarshi Avatar answered Sep 24 '22 15:09

Saptarshi