从PHP的base64字符串检测图像类型 [英] Detecting image type from base64 string in PHP
问题描述
是否有可能找出EN codeD作为PHP一个base64字符串图像的类型?
Is it possible to find out the type of an image encoded as a base64 String in PHP?
我没有访问原始图像文件,只需连接codeD字符串的方法。从我所看到的, imagecreatefromstring()
可以创建一个字符串再presentation图像资源(后它已经从的base64德codeD),但它会自动检测图像类型和图像资源本身是一个特殊的PHP重新presentation。如果我想再次将图像保存为一个文件,我不知道如果我将其保存为类型对应于从该字符串重新presentation创建的原始类型。
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.
推荐答案
的FileInfo 可以做为你:
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
这篇关于从PHP的base64字符串检测图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!