从 PHP 中的 base64 字符串检测图像类型
是否可以找出 PHP 中编码为 base64 字符串的图像的类型?
我无法访问原始图像文件,只能访问编码字符串。据我所知,imagecreatefromstring()
可以从字符串表示形式创建图像资源(在从base64解码之后),但它会自动检测图像类型,并且图像资源本身是一个特殊的PHP 表示。如果我想再次将图像保存为文件,我不知道我保存它的类型是否对应于创建字符串表示形式的原始类型。
Is it possible to find out the type of an image encoded as a base64 String in PHP?
I have no method of accessing the original image file, just the encoded string. From what I've seen, imagecreatefromstring()
can create an image resource from a string representation (after it's been decoded from base64), but it automatically detects the image type and the image resource itself is a special PHP representation. In case I want to save the image as a file again, I would have no idea if the type I am saving it as corresponds to the original type from which the String representation was created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
continue
You can use magic numbers to detect a MIME type (check here the list of file signatures). However, file signatures are not 100% reliable and you can easily encounter false positives. Of course, there are tasks when a such solution is more than enough.
So if you have a Base64 string and want to identify its MIME type using file signatures you don't need to decode the Base64. A much faster way is to store the file signatures as Base64 and just check if input starts with one of them. A simple example:
I got this solution from this Js Que-Answer
Additinally : this one is also works properly
关注 PHP.NET Fileinfo :-
Follow PHP.NET Fileinfo :-
@Marc B 展示的方式是最好的。
如果 FInfo 不可用,我知道的唯一其他方法是将数据存储到文件中,然后对其运行 getimagesize() 。
The way shown by @Marc B is the nicest.
Should
FInfo
not be available, the only other way I know is to store the data into a file, and run agetimagesize()
on it.如果您对文件格式结构有一定的了解,理论上您可以查看文件的顶部字节,直到您可以弄清楚它是什么类型。
例如,GIF 图像始终以以下字节
GIF89a
开头。如果您可以在文件开头找到该字符串,则可以合理确定它是 GIF 图像,并且绝对确定它不是任何其他图像格式。 (它仍然可能是一个文本文件,只是恰好以“GIF89a”开头;您必须解析更多文件才能绝对确定)同样,PNG 文件具有字符串
PNG
相当接近开始(这还不是一开始;同样,您需要研究文件格式的详细信息,以帮助您确定需要了解多少信息才能确定)。JPEG 的标头中还包含可识别的字符串,尽管这些字符串更加多样化和复杂。您可能需要查找字符串
Exif
。获取文件格式定义肯定会给您带来更高的准确性,但根据您需要的准确程度,您只需在二进制编辑器中打开一些图像文件以了解它们的结构,就可以充分了解文件格式。
这些资源可能对您有帮助:
http://www.w3.org/Graphics/JPEG/< /a>
http://www.w3.org/Graphics/PNG/
If you know a minimal amount about the file format structure, you could theoretically look at the top bytes of the file until you could work out what type of file it is.
For example, a GIF image always starts with the following bytes
GIF89a
. If you can find that string at the begining of the file, you can be reasonably sure that it is a GIF image and absolutely certain it isn't any other image format. (it could still be a text file though, that just happens to start with 'GIF89a'; you'd have to parse more of the file to be absolutely certain)Likewise, PNG files have the string
PNG
fairly near the start (it's not quite at the very begining; again, you'd need to research the file format specifics to help you determine how much you'd need to know to be certain).JPEGs also contain recognisable strings in their headers, although these are more varied and complex. You might want to look out for the string
Exif
.Getting the file format definitions would definitely give you more accuracy, but depending on how accurate you need to be, you might learn enough about the files formats just by opening some image files in a binary editor to see how they're structured.
These resources may help you:
http://www.w3.org/Graphics/JPEG/
http://www.w3.org/Graphics/PNG/
@Marc B 给出的解决方案对我来说是最好的(如果我们的 php 版本> 5.3.0,否则我们可以使用 @Aaron Murgatroyd 给出的解决方案)。
我想对这个解决方案做一些补充。
要获取图像类型,您可以这样做:
事实上,(如果您不知道的话)图像的 mime 类型是:image/type 和 type可以是 png 或 gif 或 jpeg 或...
希望可以帮助某人并感谢@Marc B 的解决方案。
有关 MIME 类型的详尽列表,您可以查看此处:
The solution given by @Marc B is the best one for me (if our php version is > 5.3.0 otherwise we can use the solution given by @Aaron Murgatroyd).
I would like to give a little addition to this solution.
To get the image type you can do it like this :
In fact, (if you don't know it) the mime type for images is : image/type and type can be png or gif or jpeg or ...
Hope that can help someone and thanks to @Marc B for his solution.
For an exhaustive list of mime type you can look here :
下面的代码将从其 mime 类型获取图像类型。
Code below will get the image type from its mime type.
如果由于它们的依赖性而不想使用这些函数,则可以使用数据的第一个字节:
引用:- 在此处查看文件签名列表
If you dont want to use these functions because of their dependencies you can use the first bytes of the data:
references :- check here the list of file signatures
FileInfo 可以为您做到这一点:
FileInfo can do that for you: