// 本地化文章中的图片
function download_images($post_content,$path)
{
$dom = new DOMDocument();
@$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' . $post_content);
$imgs = $dom->getElementsByTagName('img');
foreach ($imgs as $img) {
$image_url = $img->getAttribute('src');
// 获取图片的文件名称
$file_name = basename($image_url);
$path_info = pathinfo($file_name);
$newpath = '/wp-content/uploads/'.date('Y/m').'/';
$new_file = $newpath.date('YmdHis').floor(rand(10000,99999)).'.'.$path_info['extension'];
$oldfile = get_curl($image_url);
if($oldfile){
file_put_contents($path.$new_file, $oldfile);
}
$img->setAttribute('src', $new_file);
}
$post_contents = $dom->saveHTML();
return strip_tags($post_contents, '<p><img><br>');
}
提取html节点中的图片地址并替换,下载到本地
于 2024-07-23 18:48:09 首次发布