在工作中,合作方调用接口会向我们传递一些数据,中间会有图片,但是可能合作方传递的数据不符合要求而又不能强制要求甲方爸爸来改动这些参数,所以只能将图片下载下来,来获取图片的宽高等属性。
/**
* 下载图片
* @param $picUrl
* @return array|bool
*/
public static function downloadPic($picUrl){
if(empty($picUrl)){
return false;
}
$picUrlInfo = parse_url($picUrl);
if(!isset($picUrlInfo['host']) || empty($picUrlInfo['host'])){
return false;
}
$headers = get_headers($picUrl);
$isWebP = false;
foreach ($headers as $header) {
if(stripos($header,'image/webp') !== false){
$isWebP = true;
break;
}
}
try{
$picFile = CommonModel::saveRemoteFile($picUrl);
} catch (\Exception $e) {
CommonLog::info([
'msg' => '下载图片失败',
'pic_url' => $picUrl,
]);
return false;
}
if(!$picFile) {
return false;
}
if($isWebP) {
return [
'type' => 'webp',
'file_path' => $picFile,
'file_md5' => md5_file($picFile)
];
}
if(@imagecreatefromstring(file_get_contents($picFile)) === false){
CommonLog::info([
'msg' => '下载图片失败-下载的文件不是图片',
'pic_url' => $picUrl,
'filesize' => filesize($picFile)
]);
@unlink($picFile);
$picFile = self::downloadPicWithHeader($picUrl);
if(!$picFile) return false;
}
list($width, $height, $type, $attr) = getimagesize($picFile);
$imageTypeMap = [
1 => 'gif',
2 => 'jpg',
3 => 'png',
4 => 'swf',
5 => 'psd',
6 => 'bmp',
7 => 'tiff',
8 => 'tiff',
9 => 'jpc',
10 => 'jp2',
11 => 'jpx',
12 => 'jb2',
13 => 'swc',
14 => 'iff',
15 => 'wbmp',
16 => 'xbm',
];
return [
'width' => $width,
'height' => $height,
'type' => $imageTypeMap[$type],
'attr' => $attr,
'file_path' => $picFile,
'file_md5' => md5_file($picFile)
];
}
/**
* 下载远程图片
* @param $url
* @param int $connectTimeout //单位秒
* @param int $queryTimeout //单位秒
* @return bool|string
*/
public static function saveRemoteFile($url, $connectTimeout = 10, $queryTimeout = 1800)
{
Yii::$app->params['connect_timeout'] = $connectTimeout * 1000;
Yii::$app->params['query_timeout'] = $queryTimeout * 1000;
$content = CurlServiceDao::get($url, [], 'curl_download', false);
if (empty($content)) {
return false;
}
$pathInfo = pathinfo($url);
$savePath = sys_get_temp_dir() . '/' . md5(microtime(true) . $url) . '.' . $pathInfo['extension'];
file_put_contents($savePath, $content);
return $savePath;
}
/**
* 使用Curl获取Curl头来下载图片
* @param $picUrl
* @return bool|string
*/
public static function downloadPicWithHeader($picUrl){
try{
$curl = new CurlCommon();
$curl->setOpt(CURLOPT_HEADER, 1);
$curl->setOpt(CURLOPT_NOSIGNAL, true);
$curl->setOpt(CURLOPT_CONNECTTIMEOUT , 10);
$curl->setOpt(CURLOPT_TIMEOUT, 1800);
$curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
$result = $curl->get($picUrl);
$pathInfo = pathinfo($picUrl);
$picFile = sys_get_temp_dir() . '/' . md5(microtime(true).$picUrl). '.' . $pathInfo['extension'];
file_put_contents($picFile,$result->response);
unset($result);
if(@imagecreatefromstring(file_get_contents($picFile)) === false){
CommonLog::info([
'msg' => '下载图片失败-下载的文件不是图片',
'pic_url' => $picUrl,
'filesize' => filesize($picFile)
]);
@unlink($picFile);
return false;
}
return $picFile;
} catch (\Exception $e) {
CommonLog::info([
'msg' => '下载图片失败',
'pic_url' => $picUrl,
]);
return false;
}
}