欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2024-01-05 16:30:34
...

有些时候我们想保存一些漂亮图片,但又不想手动去保存下来,尤其是大批量的存储,这个时候我们需要写一段程序来帮助我们完成这个工作,本文介绍了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);

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

上一篇:

下一篇: