简体   繁体   English

PHP:来自图像字节的图像 mime 类型

[英]PHP: image mime type from image bytes

Introduction介绍

I have a base64 image string retrieved from a database: $imageBase64Str我有一个从数据库中检索到的 base64 图像字符串: $imageBase64Str

I need to retrieve the mime from this content and display the image.我需要从此内容中检索 mime 并显示图像。 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);

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.我对这个解决方案的问题是它需要我将内容的前 12 个字节写入临时文件。 I am wondering whether there could be a simple way to avoid this without having to maintain a set of mimes manually.我想知道是否有一种简单的方法可以避免这种情况,而无需手动维护一组 mime。 Also, I would like to avoid calling an external program (through exec for example) so that my code remains portable.另外,我想避免调用外部程序(例如通过exec ),以便我的代码保持可移植性。

Ideally理想情况下

I wish there was a php function like exif_imagetype_from_bytes .我希望有一个 php function 像exif_imagetype_from_bytes My imgMime function would be simpler:我的imgMime function 会更简单:

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);

Edit: Solution based on selected answer编辑:基于所选答案的解决方案

Thanks a lot to @Kunal Raut for the answer that allowed me to come up with the following solution:非常感谢@Kunal Raut 的回答让我想出了以下解决方案:

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.恕我直言,这个解决方案要优雅得多。

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.我对这个解决方案的问题是它需要我将内容的前 12 个字节写入临时文件。 I am wondering whether there could be a simple way to avoid this without having to maintain a set of mimes manually.我想知道是否有一种简单的方法可以避免这种情况,而无需手动维护一组 mime。

This is because of this part of your code这是因为您的这部分代码

if(!fwrite($file,$imgBytes,12)){
        fclose($file);
        return(false);
    }

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.它使您在文件中写入至少 12 个字节的数据,然后让执行继续前进。您可以跳过此if()并解决您的第一个问题。

I wish there was a php function like exif_imagetype_from_bytes.我希望有一个 php function 像 exif_imagetype_from_bytes。 My imgMime function would be simpler我的 imgMime function 会更简单

Yes there is such function which returns you the type of the base64_decoded string.是的,有这样的 function 会返回base64_decoded字符串的类型。

finfo_buffer()

For more details regarding this function Click Here .有关此 function 的更多详细信息,请单击此处

Use of function function的使用

Check out this 看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM