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

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

WBOY
WBOYOriginal
2016-06-06 19:48:511087browse

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

php如何将远程图片本地化,,本文分享了实现代码

]*)\.(gif|jpg|png))/isU",$body,$img_array); $img_array = array_unique($img_array[1]); $imgUrl = $cfg_uploaddir.'http://www.jb51.net/'.date("ymd", time()); $imgPath = $cfg_basedir.$imgUrl; if(!is_dir($imgPath.'http://www.jb51.net/')) { 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.'http://www.jb51.net/'.$milliSecondN.'-'.$key.$itype; $fileurl = $imgUrl.'http://www.jb51.net/'.$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);

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn