PHP 判断图片文件的真实类型

远昔 代码记录 2021-09-03 3573 0

PHP 判断图片文件的真实类型代码:

<?
$imgurl = 'https://www.yuanxiapi.cn/api/ipimg/';
echo file_type_detect($imgurl);

function file_type_detect($srcPath){
    $types = array(
        6677    => 'bmp',
        7173    => 'gif',
        7368    => 'mp3',
        13780   => 'png',
        255216  => 'jpg',
    );  

$binary=file_get_contents($srcPath);

    $bytes = substr($binary, 0, 2); 
    $head = @unpack('C2char', $bytes);
    $typeCode = intval($head['char1'].$head['char2']);
    $realType = isset($types[$typeCode]) ? $types[$typeCode] : $typeCode;
    return $realType;
} ?>

返回结果:png

评论

发表评论:

挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论