/*
* @function:将微信头像保存到自己的服务器中
* @param url图片链接地址
* @param 服务器根目录
* @date:2019-10-21
* */
static function download_remote_pic($url,$dir){
$header = [
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}
$img_content=$imgBase64Code;//图片内容
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
$type = $result[2];//得到图片类型png?jpg?gif?
$new_dir="/tmp/upload/avator/".date("Ymd",time());
if(!is_dir($dir.$new_dir)){
mkdir($dir.$new_dir,0777,true);
}
$new_file = $new_dir.'/'.time().rand(1,10000).".{$type}";
$imgs = base64_decode(str_replace($result[1], '', $img_content));
$fp2 = fopen($dir.$new_file , "a");
fwrite($fp2, $imgs);
fclose($fp2);
return $new_file;
}
}
将远程图片保存到自己的服务器
于 2021-10-15 14:24:04 首次发布

被折叠的 条评论
为什么被折叠?



