ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` /* * 远程图片下载 */ public function downpic($content){ $content= preg_replace('/alt=[\'|\"](.*?)[\'|\"]/','',$content); $content = str_replace(' ','',$content); //获取图片 preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?)[\'|\"]/',$content, $matches); $ok = $chongwu = 0; if(isset($matches[1])&&count($matches[1])>0){ $xiugaiqian = $matches[1]; $xiugaihou = array(); foreach($xiugaiqian as $ik=>$iv){ $houv = getTrueImage($iv); $xiugaihou[$ik] = $houv; if($iv==$houv){ $chongwu++; }else{ $ok++; } } $content = str_replace($xiugaiqian,$xiugaihou,$content); } $data['ok'] = $ok; $data['chongwu'] = $chongwu; $data['content'] = $content; return json_encode($data); } //下载图片文件名不变 function getTrueImage($url,$save_dir='uploadimg/image/'){ $url_path = parse_url($url); //获取图片信息 $img_info = pathinfo($url_path['path']); $save_dir = $img_info['dirname'].'/';//图片保存路径 $save_dir = substr($save_dir,strpos($save_dir,"/")+1); if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){ return $url; } //文件名 if($img_info['basename']){ $filename = $img_info['basename']; }else{ $filename = time().rand(100,999).'.'.$img_info['extension']; } if(!file_exists($save_dir.$filename)){ //获取远程文件所采用的方法 $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt ($ch, CURLOPT_REFERER, $url_path['scheme'].$url_path['host']);//这里为空也行,为他的主域名也可 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($ch, CURLOPT_ENCODING,'gzip'); $img=curl_exec($ch); curl_close($ch); //$size=strlen($img); //文件大小 if(($fp2=@fopen(trim($save_dir.$filename),'a')) !== false){ fwrite($fp2, $img); fclose($fp2); } unset($img,$url); } return '/'.$save_dir.$filename; } ```