public static function transImgLocal($html_contents,$a_baseSavePath,$a_localPathFlag){
$result=$html_contents;
$html_contents = stripslashes($html_contents);//返回已剥离反斜杠的字符串
//echo $html_contents; exit;
$img_array = array();
//处理远程图片url
preg_match_all("/(src|SRC)=[\"|'| ]{0,}((http|https):\/\/(.*).(gif|jpg|jpeg|bmp|png))/isU", $html_contents, $img_array );
// 匹配出来的不重复图片
if(!empty($img_array)&& count($img_array)>=2){
$img_array = array_unique ( $img_array [2] );
//var_dump('img_array=',$img_array); exit;
//echo $img_array[0];
foreach($img_array as $item){
if(strpos($item,$a_localPathFlag) !== false){ continue; }
$filename=self::grabImage($item,$a_baseSavePath);
//echo $filename;
$html_contents=str_replace($item,'/'.$filename,$html_contents);
}
}
//处理base64图片格式
preg_match_all("/(src|SRC)=[\"|'| ]{0,}(data:image\/(\w+);base64,(.*))(\")/isU", $html_contents, $img_array2);
if(!empty($img_array2)){
$img_array2 = array_unique ( $img_array2 [2] );
//var_dump('img_array2=',$img_array2); exit;
foreach($img_array2 as $item){
if(strpos($item,$a_localPathFlag) !== false){ continue; }
$filename=self::base64_image($item,$a_baseSavePath);
//echo $filename; exit;
if(!empty($filename)){ $html_contents=str_replace($item,'/'.$filename,$html_contents); }
}
}
$result=$html_contents;
return $result;
}