使用 PHP 从 URI 中提取 MIME 类型
Pull a mime type out of a URI using PHP
因此,在搜索了多个文档源之后,我仍然无法弄清楚如何从已经处理并存储在数据库中的数据 URI 中提取 Mime 类型。

这是我必须处理的确切数据的快速快照。我只想要一种动态方式来始终获取 "image/png" 部分,该部分可能随数据库中的每个图像而变化。
我正在使用 PHP。
这不是一个优雅的解决方案,但您可以这样做:
// assume you've set $image_uri to be the URI from the database
$image_parts = explode(";", $image_uri); // split on the ; after the mime type
$mime_type = substr($image_parts[0], 5); // get the information after the data: text
这可以用正则表达式来完成,但我不太擅长想出它。
这是使用 mime_content_type
函数的优雅解决方案。
return mime_content_type($data_uri);
只需将 URI 传递给函数,它就会起作用 (source)。
因此,在搜索了多个文档源之后,我仍然无法弄清楚如何从已经处理并存储在数据库中的数据 URI 中提取 Mime 类型。
这是我必须处理的确切数据的快速快照。我只想要一种动态方式来始终获取 "image/png" 部分,该部分可能随数据库中的每个图像而变化。
我正在使用 PHP。
这不是一个优雅的解决方案,但您可以这样做:
// assume you've set $image_uri to be the URI from the database
$image_parts = explode(";", $image_uri); // split on the ; after the mime type
$mime_type = substr($image_parts[0], 5); // get the information after the data: text
这可以用正则表达式来完成,但我不太擅长想出它。
这是使用 mime_content_type
函数的优雅解决方案。
return mime_content_type($data_uri);
只需将 URI 传递给函数,它就会起作用 (source)。