php如何将远程图片本地化,本文分享了实现代码
]*)\.(gif|jpg|png))/isu,$body,$img_array); $img_array = array_unique($img_array[1]); $imgurl = $cfg_uploaddir.'/'.date(ymd, time()); $imgpath = $cfg_basedir.$imgurl; if(!is_dir($imgpath.'/')) { mkdirall($imgpath, $globals['cfg_dir_purview']); closeftp(); } $millisecond = date('his',time()); foreach($img_array as $key=>$value) { if(preg_match(#.$basehost.#i, $value)) { continue; } if(preg_match(#.$basehost.#i, $value)) { continue; } if(!preg_match(#^http:\/\/#i, $value)) { continue; } $http=pget($value,'$value',true); $itype=($http['head']['content_type']); if(!preg_match(#\.(jpg|gif|png)#i, $itype)) { if($itype=='image/gif') { $itype = .gif; } else if($itype=='image/png') { $itype = .png; } else { $itype = '.jpg'; } } $millisecondn = rand(1000,9999).rand(1000,9999); $value = trim($value); $rndfilename = $imgpath.'/'.$millisecondn.'-'.$key.$itype; $fileurl = $imgurl.'/'.$millisecondn.'-'.$key.$itype; $tp = fopen($rndfilename, 'wb'); fwrite($tp, $http['data']); fclose($tp); if(file_exists($cfg_basedir.$fileurl)) { $info = ''; $imginfos = getimagesize($rndfilename, $info); $fsize = filesize($rndfilename); $body = str_replace($value, $fileurl, $body); } } return $body; } //调用方式 echo getcurcontent($body);
以上就是实现远程图片本地化的全部代码,希望对大家的学习有所帮助。