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

qimen

<?php
/**
 * 检测文件的真实类型
 * @param  string $srcPath 文件路径
 * @return string $realType 文件真实类型
*/

$imgurl = 'http://i1.fuimg.com/711685/cb9cf07a45838f2f.png';
echo file_type_detect($imgurl);
function file_type_detect($srcPath){
    $types = array(
        6677    => 'bmp',
        7173    => 'gif',
        7368    => 'mp3',
        13780   => 'png',
        255216  => 'jpg',
    );  

    $binary = curl_get($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;
}
function curl_get($url){
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $content=curl_exec($ch);
    curl_close($ch);
    return($content);
}

版权协议须知!

本篇文章来源于 网络转载 ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

79 0 2020-03-05


分享:
PHP对HTML进行代码压缩处理

压缩HTML的起因 如何提高网页加载速度,需要怎么对HT...

阅读全文
开启CDN后获取用户真实IP方法 PHP|ASP|JAVA常见代码

接入 CDN 后,为了方便获取客户端真实 IP,部分CDN...

阅读全文
PHP在处理checkbox复选框表单提交

PHP表单提交页面复选框的名称后要加[],这样在接收页面才...

阅读全文
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
  • 默认
  • 护眼
  • 夜晚
  • 壁纸
  • 默认