PHP:来自图像字节的图像mime类型 [英] PHP: image mime type from image bytes
问题描述
我有一个从数据库检索到的base64图像字符串:$imageBase64Str
I have a base64 image string retrieved from a database: $imageBase64Str
我需要从此内容中检索哑剧并显示图像.这是以下代码的作用:
I need to retrieve the mime from this content and display the image. This is what the following code does:
function imgMime($imgBytes){
if(is_null($imgBytes)){
return(false);
}
if(strlen($imgBytes)<12){
return(false);
}
$file = tmpfile();
if(!fwrite($file,$imgBytes,12)){
fclose($file);
return(false);
}
$path = stream_get_meta_data($file)['uri'];
$mimeCode=exif_imagetype($path);
fclose($file);
if(!$mimeCode){
return(false);
}
return(image_type_to_mime_type($mimeCode));
}
$imageBytes=base64_decode($imageBase64Str,true);
if(!$imageBytes){
throw new Exception("cannot decode image base64");
}
$imageMime=imgMime($imageBytes);
if(!$imageMime){
throw new Exception("cannot recognize image mime");
}
header('Content-type: '.$imageMime);
echo($imageBytes);
问题
此解决方案的问题在于,它要求我将内容的前12个字节写入一个临时文件.我想知道是否有一种简单的方法可以避免这种情况,而不必手动维护一组哑剧.另外,我想避免调用外部程序(例如,通过exec
),以使我的代码保持可移植性.
Question
The issue I have with this solution is that it requires me to write the 12 first bytes of the content to a temporary file. I am wondering whether there could be a simple way to avoid this without having to maintain a set of mimes manually. Also, I would like to avoid calling an external program (through exec
for example) so that my code remains portable.
我希望有一个像exif_imagetype_from_bytes
这样的php函数.我的imgMime
函数会更简单:
I wish there was a php function like exif_imagetype_from_bytes
. My imgMime
function would be simpler:
function imgMime($imgBytes){
if(is_null($imgBytes)){
return(false);
}
if(strlen($imgBytes)<12){
return(false);
}
$mimeCode=exif_imagetype($imgBytes);
if(!$mimeCode){
return(false);
}
return(image_type_to_mime_type($mimeCode));
}
$imageBytes=base64_decode($imageBase64Str,true);
if(!$imageBytes){
throw new Exception("cannot decode image base64");
}
$imageMime=imgMime($imageBytes);
if(!$imageMime){
throw new Exception("cannot recognize image mime");
}
header('Content-type: '.$imageMime);
echo($imageBytes);
基于所选答案的解决方案
非常感谢@Kunal Raut提供的答案,我可以提出以下解决方案:
Solution based on selected answer
Thanks a lot to @Kunal Raut for the answer that allowed me to come up with the following solution:
function imgMime($imgBytes){
if(is_null($imgBytes)){
return(false);
}
if(strlen($imgBytes)<12){
return(false);
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime=$finfo->buffer($imgBytes);
if(strncmp($mime, "image/", 6) != 0){
return(false);
}
return($mime);
}
$imageBytes=base64_decode($imageBase64Str,true);
if(!$imageBytes){
throw new Exception("cannot decode image base64");
}
$imageMime=imgMime($imageBytes);
if(!$imageMime){
throw new Exception("cannot recognize image mime");
}
header('Content-type: '.$imageMime);
echo($imageBytes);
此解决方案非常优雅,恕我直言.
This solution is a lot more elegant IMHO.
推荐答案
此解决方案的问题在于,它要求我将内容的前12个字节写入一个临时文件.我想知道是否有一种简单的方法可以避免这种情况,而不必手动维护一组哑剧.
The issue I have with this solution is that it requires me to write the 12 first bytes of the content to a temporary file. I am wondering whether there could be a simple way to avoid this without having to maintain a set of mimes manually.
这是因为您的代码的这一部分
This is because of this part of your code
if(!fwrite($file,$imgBytes,12)){
fclose($file);
return(false);
}
它使您可以在文件中写入至少12个字节的数据,然后继续执行.您可以跳过此if()
并解决第一个问题.
It makes you write minimum 12 bytes of data in the file and then lets the execution move forward.You can skip this if()
and solve your first problem.
我希望有一个像exif_imagetype_from_bytes这样的php函数.我的imgMime函数会更简单
I wish there was a php function like exif_imagetype_from_bytes. My imgMime function would be simpler
是的,该函数可以为您返回 base64_decoded 字符串的类型.
Yes there is such function which returns you the type of the base64_decoded string.
有关此功能的更多详细信息,请单击此处.
For more details regarding this function Click Here.
功能使用
这篇关于PHP:来自图像字节的图像mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!