通过图片url下载图片到指定位置,并获取图片后缀
// 根据图片url 下载图片到指定位置
function upload($url, $path, $file_name)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$resource = fopen($path . DIRECTORY_SEPARATOR . $file_name, 'a');
fwrite($resource, $file);
fclose($resource);
return $resource;
}
// 获取图片url后缀 curl请求获取
function getImgExt($file)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
// \Log::info('content_type=>>' . $content_type);
curl_close($ch);
$content_type_map = [
'image/bmp' => 'bmp',
'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/x-icon' => 'ico'
];
// \Log::info('ext=>>' . $content_type_map[$content_type]);
return $content_type_map[$content_type];
}