wordpress远程图片自动本地化,找不到插件请用代码实现

2020年2月28日22:38:53 发表评论 73 views

wordpress 主题中加入远程图片文件自动本地化代码,经常转载文章的朋友应该会用到这个功能。把外链图片自动保存到网站本地,防止外链图片失效造成图片不显示。把下面的代码添加到主题的 functions.php 文件或者是 functions.php 的引入文件中即可,以后每当在 wordpress 发布文章时如果文章中含有外链图片就会自动本地化了,无需任何设置操作非常方便。

  1.     add_filter('content_save_pre', 'auto_save_image');
  2.     function auto_save_image($content) {
  3.      $upload_path = '';
  4.      $upload_url_path = get_bloginfo('url');
  5.      //上传目录
  6.      if (($var = get_option('upload_path')) !=''){
  7.      $upload_path = $var;
  8.      } else {
  9.      $upload_path = 'wp-content/uploads';
  10.      }
  11.      if(get_option('uploads_use_yearmonth_folders')) {
  12.      $upload_path .= '/'.date("Y",time()).'/'.date("m",time());
  13.      }
  14.      //文件地址
  15.      if(($var = get_option('upload_url_path')) != '') {
  16.      $upload_url_path = $var;
  17.      } else {
  18.      $upload_url_path = bloginfo('url');
  19.      }
  20.      if(get_option('uploads_use_yearmonth_folders')) {
  21.      $upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
  22.      }
  23.      require_once ("../wp-includes/class-snoopy.php");
  24.      $snoopy_Auto_Save_Image = new Snoopy;
  25.      $img = array();
  26.      //以文章的标题作为图片的标题
  27.      if ( !empty( $_REQUEST['post_title'] ) )
  28.      $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
  29.      $text = stripslashes($content);
  30.      if (get_magic_quotes_gpc()) $text = stripslashes($text);
  31.      preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);
  32.      $img = array_unique(dhtmlspecialchars($img[2]));
  33.      foreach ($img as $key => $value){
  34.      set_time_limit(180); //每个图片最长允许下载时间,秒
  35.      if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
  36.      //判断是否是本地图片,如果不是,则保存到服务器
  37.      $fileext = substr(strrchr($value,'.'),1);
  38.      $fileext = strtolower($fileext);
  39.      if($fileext==""||strlen($fileext)>4)
  40.      $fileext = "jpg";
  41.      $savefiletype = array('jpg','gif','png','bmp');
  42.      if (in_array($fileext, $savefiletype)){
  43.      if($snoopy_Auto_Save_Image->fetch($value)){
  44.      $get_file = $snoopy_Auto_Save_Image->results;
  45.      }else{
  46.      echo "error fetching file: ".$snoopy_Auto_Save_Image->error."<br>";
  47.      echo "error url: ".$value;
  48.      die();
  49.      }
  50.      $filetime = time();
  51.      $filepath = "/".$upload_path;//图片保存的路径目录
  52.      !is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
  53.      //$filename = date("His",$filetime).random(3);
  54.      $filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
  55.      //$e = '../'.$filepath.$filename.'.'.$fileext;
  56.      //if(!is_file($e)) {
  57.      // copy(htmlspecialchars_decode($value),$e);
  58.      //}
  59.      $fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
  60.      @fwrite($fp,$get_file);
  61.      fclose($fp);
  62.      $wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
  63.      $type = $wp_filetype['type'];
  64.      $post_id = (int)$_POST['temp_ID2'];
  65.      $title = $post_title;
  66.      $url = $upload_url_path.$filename.".".$fileext;
  67.      $file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
  68.      //添加数据库记录
  69.      $attachment = array(
  70.      'post_type' => 'attachment',
  71.      'post_mime_type' => $type,
  72.      'guid' => $url,
  73.      'post_parent' => $post_id,
  74.      'post_title' => $title,
  75.      'post_content' => '',
  76.      );
  77.      $id = wp_insert_attachment($attachment, $file, $post_parent);
  78.      $text = str_replace($value,$url,$text); //替换文章里面的图片地址
  79.      }
  80.      }
  81.      }
  82.      $content = AddSlashes($text);
  83.      remove_filter('content_save_pre', 'auto_save_image');
  84.      return $content;
  85.     }
  86.     function mkdirs($dir)
  87.     {
  88.      if(!is_dir($dir))
  89.      {
  90.      mkdirs(dirname($dir));
  91.      mkdir($dir);
  92.      }
  93.      return ;
  94.     }
  95.     function dhtmlspecialchars($string) {
  96.      if(is_array($string)) {
  97.      foreach($string as $key => $val) {
  98.      $string[$key] = dhtmlspecialchars($val);
  99.      }
  100.      }else{
  101.      $string = str_replace('&', '&', $string);
  102.      $string = str_replace('"', '"', $string);
  103.      $string = str_replace('<', '<', $string);
  104.      $string = str_replace('>', '>', $string);
  105.      $string = preg_replace('/&(#\d;)/', '&\1', $string);
  106.      }
  107.      return $string;
  108.     }

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: