需要手动创建目录Uploads
需要传入的参数$body 嗯,HTML代码
$url 传入一个源网站的地址,我没写检测功能,这个会手动检查时http还是https协议
function SaveRomoteImg($body,$url){
//以下为保存图片到本地的代码段落
$img_array = explode('&',$body);
$img_array = array();
$scheme = parse_url($url);
$matchRule = '/(src)=[\"|\'| ]{0,}('.$scheme[scheme].':\/\/(.*)\.(gif|jpg|jpeg|bmp|png|JPEG|GIF|PNG))[\"|\'| ]{0,}/isU';
preg_match_all($matchRule, $body, $img_array);
$img_array = array_unique($img_array[2]);
$imgPath = "Uploads/article/".date("Y-m-d")."/";
if(!is_dir($imgPath)) @mkdir($imgPath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$fileName = pathinfo($value,PATHINFO_BASENAME);
$rndFileName = $imgPath."/".$fileName;
if($get_file)
{
$fp = @fopen($rndFileName,"w");
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = @ereg_replace($value, 'http://localhost/typecho/'.$rndFileName, $body);
$body=str_replace('_z.jpg','',$body); //替换里面有问题的内容
}
return $body;
}