php将远程图片保存到本地服务器的实现代码

有些时候我们想保存一些漂亮图片,但又不想手动去保存下来,尤其是大批量的存储,这个时候我们需要写一段程序来帮助我们完成这个工作,本文介绍了php如何将远程图片本地化,需要的朋友可以参考下

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-600"; } else { $itype = '.jpg-600'; } } $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); 

以上就是实现远程图片本地化的全部代码,希望对大家的学习有所帮助。

以上就是php将远程图片保存到本地服务器的实现代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » PHP编程