pc wx

扫码关注“沃之涛科技”安全登录

扫码登录 微信内打开可长按扫码一键登录

登录即表示同意服务协议条款

我已确认并阅读 服务协议条款

如果您丢失了所有信息,可联系管理员QQ:1500351892。

添加域名
确定删除该域名吗?
该操作无法复原
用户头像

用户

ID: 剩余积分:

无赠送
100积分
100
赠100积分
500积分
500
赠400积分
1000积分
1000
赠1300积分
2000积分
2000
赠7000积分
5000积分
5000
注意事项

积分仅限于AI文章写作也可以用于WordPress下的SEO合集插件“智能改写”“词库挖掘”“关键词排名监控”“AI智能DK”功能使用;

充值仅用于消费,不可变更,退款,提现,请慎重选择!

支付宝
微信
购买积分: 100
赠送积分: 0
应付金额: ¥100

用户邮箱

验证码

点此继续访问
邮箱不存在
确定删除吗?
该操作无法复原
分类编辑
序号
分类名称
操作
{{item.index}}

暂无数据

{{item.index}}.
暂无数据
行业资讯
右圆圈
左圆圈
大圆圈
左边大圆圈
圆圈
圆圈

WodrPress自动保存远程图片到本地

浏览次数:2059 发布日期:2022-10-17 18:11:53

wordpress远程图片保存到本地服务器主要用于文章中图片的复制粘贴以及文章的转载。图片本地化后,能够有效的防止图片因远程网址关闭、防盗链等原因造成的图片破损。

需要注意的是,一些带有公司标识的图片请尽量不用,防止造成法律问题。转载请注明转载地址,作者辛苦编辑一篇文章也不容易,尊重别人也是尊重自己。

下面进入正题,图片本地化有两种方式:

一、在主题下functions.php下添加如下代码:

//自动本地化外链图片
add_filter('content_save_pre', 'auto_save_image');
function auto_save_image($content) {
    $upload_path = '';
    $upload_url_path = get_bloginfo('url');
    //上传目录
    if (($var = get_option('upload_path')) !=''){
        $upload_path = $var;
    } else {
        $upload_path = 'wp-content/uploads';
    }
    if(get_option('uploads_use_yearmonth_folders')) {
        $upload_path .= '/'.date("Y",time()).'/'.date("m",time());
    }
    //文件地址
    if(($var = get_option('upload_url_path')) != '') {
        $upload_url_path = $var;
    } else {
        $upload_url_path = bloginfo('url');
    }
    if(get_option('uploads_use_yearmonth_folders')) {
        $upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
    }
    require_once ("../wp-includes/class-snoopy.php");
    $snoopy_Auto_Save_Image = new Snoopy;
    $img = array();
    //以文章的标题作为图片的标题
    if ( !empty( $_REQUEST['post_title'] ) )
        $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
    $text = stripslashes($content);
    if (get_magic_quotes_gpc()) $text = stripslashes($text);
    preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);
    $img = array_unique(dHTMLspecialchars($img[2]));
    foreach ($img as $key => $value){
        set_time_limit(180); //每个图片最长允许下载时间,秒
        if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
            //判断是否是本地图片,如果不是,则保存到服务器
            $fileext = substr(strrchr($value,'.'),1);
            $fileext = strtolower($fileext);
            if($fileext==""||strlen($fileext)>4)
                $fileext = "jpg";
            $savefiletype = array('jpg','gif','png','bmp');
            if (in_array($fileext, $savefiletype)){
                if($snoopy_Auto_Save_Image->fetch($value)){
                    $get_file = $snoopy_Auto_Save_Image->results;
                }else{
                    echo "error fetching file: ".$snoopy_Auto_Save_Image->error."<br>";
                    echo "error url: ".$value;
                    die();
                }
                $filetime = time();
                $filepath = "/".$upload_path;//图片保存的路径目录
                !is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
                //$filename = date("His",$filetime).random(3);
                $filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
                //$e = '../'.$filepath.$filename.'.'.$fileext;
                //if(!is_file($e)) {
                // copy(htmlspecialchars_decode($value),$e);
                //}
                $fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
                @fwrite($fp,$get_file);
                fclose($fp);
                $wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
                $type = $wp_filetype['type'];
                $post_id = (int)$_POST['temp_ID2'];
                $title = $post_title;
                $url = $upload_url_path.$filename.".".$fileext;
                $file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
                //添加数据库记录
                $attachment = array(
                    'post_type' => 'attachment',
                    'post_mime_type' => $type,
                    'guid' => $url,
                    'post_parent' => $post_id,
                    'post_title' => $title,
                    'post_content' => '',
                );
                $id = wp_insert_attachment($attachment, $file, $post_parent);
                $text = str_replace($value,$url,$text); //替换文章里面的图片地址
            }
        }
    }
    $content = AddSlashes($text);
    remove_filter('content_save_pre', 'auto_save_image');
    return $content;
}
function mkdirs($dir){
    if(!is_dir($dir)){
        mkdirs(dirname($dir));
        mkdir($dir);
    }
    return ;
}
function dhtmlspecialchars($string) {
    if(is_array($string)) {
        foreach($string as $key => $val) {
            $string[$key] = dhtmlspecialchars($val);
        }
    }else{
        $string = str_replace('&', '&', $string);
        $string = str_replace('"', '"', $string);
        $string = str_replace('<', '<', $string);
        $string = str_replace('>', '>', $string);
        $string = preg_replace('/&(#\d;)/', '&\1', $string);
    }
    return $string;
}

二、使用插件

1)、QQWorld Auto Save Images插件

虽然可能不在更新了,但是wordpress6依然还可以用。具有是否使用经典编辑器、上传模式、处理时机、设置特色图片等基础功能。还有图片大小过滤、链接自定义、同时还可以处理已发布的文章,功能强大。

2)、WP Githuber MD插件

该插件是把wordpress的编辑器更换为Markdown 编辑器,多数功能都是markdown的功能设置,但也有抓取远程图片的功能,以及代码优化、SEO优化的部分功能,喜欢Markdown编辑器的用户可以尝试使用。该插件也是有一年没更新了。

总结:远程图片本地化实现方式可以根据自己的需要进行选择,这只是一个小功能,甚至很多主题直接就支持该功能。如果不支持推荐在主题里进行修改。因为这样功能的插件确实很多但是大部分都不更新了,不知道您的wordpress什么时候更新着就不支持了。


营业执照
seo合集软著
WordPress积木主题软著
报价
交流
微信二维码
kelerk
图片
复制成功!