[API]PHP判断图片真实类型源码

例:https://api.ayfre.com/tools/img.php?url=https://www.baidu.com/img/bd_logo1.png

 

api:https://api.ayfre.com/tools/img.php?url=

参数:url 图片文件链接

返回:图片文件真实类型

 

源码:

 

<?php
/**
* 检测文件的真实类型
* @url 文件路径
* @echo 文件类型
* 諦覠
* AYFRE.COM
* 2019/12/2
*/
if(!isset($_GET['url'])||empty($_GET['url'])||$_GET['url']==''){
echo json_encode(array('code'=>'201','msg'=>'Please fill in the request parameters'));
exit();
}
$imgurl = $_GET['url'];
echo file_type_detect($imgurl);
function file_type_detect($srcPath){
$types = array(
6677 => json_encode(array('code'=>'200','type'=>'bmp')),
7173 => json_encode(array('code'=>'200','type'=>'gif')),
7368 => json_encode(array('code'=>'200','type'=>'mp3')),
13780 => json_encode(array('code'=>'200','type'=>'png')),
255216 => json_encode(array('code'=>'200','type'=>'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);
}

温馨提示: 本文最后更新于2019-12-02,至今已有1770天,某些文章具有时效性,若有错误或已失效,请在下方留言
© 版权声明
THE END
喜欢就支持一下吧❀
点赞1投币 分享
评论 共2条

    请登录后查看评论内容