通过图片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];
}


本文链接:php-curl根据图片url下载图片到指定位置 - https://myags.cn/index.php/252.html

版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 拾穗儿's 所有,未经允许不得转载!
最后修改:2021 年 06 月 18 日 03 : 28 PM
如果觉得我的文章对你有用,请随意赞赏