Detecting image type from base64 string in PHP(从 PHP 中的 base64 字符串检测图像类型)
问题描述
是否可以在 PHP 中找出编码为 base64 字符串的图像的类型?
Is it possible to find out the type of an image encoded as a base64 String in PHP?
我没有访问原始图像文件的方法,只有编码字符串.据我所见, imagecreatefromstring()
可以从字符串表示创建图像资源(在从 base64 解码之后),但它会自动检测图像类型并且图像资源本身是特殊的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.
推荐答案
FileInfo可以做给你的:
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
这篇关于从 PHP 中的 base64 字符串检测图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 PHP 中的 base64 字符串检测图像类型


- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01