javascript 从 base 64 内容确定图像类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10302689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine image type from base 64 content
提问by sbr
Possible Duplicate:
Detecting image type from base64 string in PHP
I have different types of image content in base64 format in javascript, like:
我在 javascript 中有不同类型的 base64 格式的图像内容,例如:
a png file: "iVBORw0KGgoAAAANSUhEUgAABQAAAAL4CAYAAAAkgloNAAAgA......."
一个 png 文件:“iVBORw0KGgoAAAANSUhEUgAABQAAAAL4CAYAAAAkgloNAAAgA ......”
a jpeg file: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA......"
一个 jpeg 文件:“/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA ......”
similarly, there can be other images like GIF, BMP, etc.
同样,也可以有其他图像,如 GIF、BMP 等。
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.
我需要一种方法来根据内容确定图像类型。我知道每种文件类型都有标题,我可以参考它来确定图像内容类型。示例参考: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.
但是有没有人知道一个库或其他技术可以以更聪明的方式来解决这个问题?如果需要,我可以使用任何 HTML5 功能。
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.
主要目的是在屏幕上渲染图像。为此,我使用 dataURI 模式,. 我有图像内容,我只需要在 url 的“data:image/png”部分输入正确的值。
回答by hobberwickey
There's lots of javascript base64 decoders, (related question), just decode the first 4 or 5 letters and they should contain the filetype.
有很多 javascript base64 解码器,(相关问题),只需解码前 4 或 5 个字母,它们应该包含文件类型。