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 字符串检测图像类型
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
