Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine image type from base 64 content

I have different types of image content in base64 format in javascript, like:

a png file: "iVBORw0KGgoAAAANSUhEUgAABQAAAAL4CAYAAAAkgloNAAAgA......."

a jpeg file: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA......"

similarly, there can be other images like GIF, BMP, etc.

I need a way to determine the image type based on the content. I understand there are headers for each file type and I can refer it to determine the image content type. Example Reference: http://en.wikipedia.org/wiki/Portable_Network_Graphics#Technical_details.

But does anyone know of a library or other technique that will do the trick in a more smarter way? I can use any of the HTML5 features if required.

The main intent is to render the image on the screen. For that I use dataURI pattern, . I have image content, all I need is to put the right value in the "data:image/png" part of the url.

like image 313
sbr Avatar asked Apr 24 '12 17:04

sbr


1 Answers

There's lots of javascript base64 decoders, (related question), just decode the first 4 or 5 letters and they should contain the filetype.

like image 148
hobberwickey Avatar answered Oct 19 '22 23:10

hobberwickey