全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
楼主: guoaibing

大佬推荐个wp远程图片 本地化的插件

[复制链接]
发表于 2019-3-23 09:12:51 | 显示全部楼层

链接:https://pan.baidu.com/s/1mC9axzarM6AOFCfcPOUKNg  密码:sx7z
 楼主| 发表于 2019-3-23 09:48:52 | 显示全部楼层
yumijie 发表于 2019-3-23 07:02
不要用插件,有纯代码

用了这段代码  图片确实上传到本地了  内容替换有问题
只有图片路径的后面,域名/wp-content/uploads没加上去

  1. //自动本地化外链图片
  2. add_filter('content_save_pre', 'auto_save_image');
  3. function auto_save_image($content) {
  4. $upload_path = '';
  5. $upload_url_path = get_bloginfo('url');
  6. //上传目录
  7. if (($var = get_option('upload_path')) != '') {
  8. $upload_path = $var;
  9. } else {
  10. $upload_path = 'wp-content/uploads';
  11. }
  12. if (get_option('uploads_use_yearmonth_folders')) {
  13. $upload_path.= '/' . date("Y", time()) . '/' . date("m", time());
  14. }
  15. //文件地址
  16. if (($var = get_option('upload_url_path')) != '') {
  17. $upload_url_path = $var;
  18. } else {
  19. $upload_url_path = get_bloginfo('url');
  20. }
  21. if (get_option('uploads_use_yearmonth_folders')) {
  22. $upload_url_path.= '/wp-content/uploads/' . date("Y", time()) . '/' . date("m", time());
  23. }
  24. require_once ("../wp-includes/class-snoopy.php");
  25. $snoopy_Auto_Save_Image = new Snoopy;
  26. $img = array();
  27. //以文章的标题作为图片的标题
  28. if (!empty($_REQUEST['post_title'])) $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) $fileext = "jpg";
  40. $savefiletype = array('jpg', 'gif', 'png', 'bmp');
  41. if (in_array($fileext, $savefiletype)) {
  42. if ($snoopy_Auto_Save_Image->fetch($value)) {
  43. $get_file = $snoopy_Auto_Save_Image->results;
  44. } else {
  45. echo "error fetching file: " . $snoopy_Auto_Save_Image->error . "<br>";
  46. echo "error url: " . $value;
  47. die();
  48. }
  49. $filetime = time();
  50. $filepath = "/" . $upload_path; //图片保存的路径目录
  51. !is_dir(".." . $filepath) ? mkdirs(".." . $filepath) : null;
  52. //$filename = date("His",$filetime).random(3);
  53. $filename = substr($value, strrpos($value, '/'), strrpos($value, '.') - strrpos($value, '/'));
  54. //$e = '../'.$filepath.$filename.'.'.$fileext;
  55. //if(!is_file($e)) {
  56. // copy(htmlspecialchars_decode($value),$e);
  57. //}
  58. $fp = @fopen(".." . $filepath . $filename . "." . $fileext, "w");
  59. @fwrite($fp, $get_file);
  60. fclose($fp);
  61. $wp_filetype = wp_check_filetype($filename . "." . $fileext, false);
  62. $type = $wp_filetype['type'];
  63. $post_id = (int)$_POST['temp_ID2'];
  64. $title = $post_title;
  65. $url = $upload_url_path . $filename . "." . $fileext;
  66. $file = $_SERVER['DOCUMENT_ROOT'] . $filepath . $filename . "." . $fileext;
  67. //添加数据库记录
  68. $attachment = array('post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => '',);
  69. $id = wp_insert_attachment($attachment, $file, $post_parent);
  70. $text = str_replace($value, $url, $text); //替换文章里面的图片地址
  71. }
  72. }
  73. }
  74. $content = AddSlashes($text);
  75. remove_filter('content_save_pre', 'auto_save_image');
  76. return $content;
  77. }
  78. function mkdirs($dir) {
  79. if (!is_dir($dir)) {
  80. mkdirs(dirname($dir));
  81. mkdir($dir);
  82. }
  83. return;
  84. }
  85. function dhtmlspecialchars($string) {
  86. if (is_array($string)) {
  87. foreach ($string as $key => $val) {
  88. $string[$key] = dhtmlspecialchars($val);
  89. }
  90. } else {
  91. $string = str_replace('&', '&', $string);
  92. $string = str_replace('"', '"', $string);
  93. $string = str_replace('<', '<', $string);
  94. $string = str_replace('>', '>', $string);
  95. $string = preg_replace('/&(#\d;)/', '&\1', $string);
  96. }
  97. return $string;
  98. }
复制代码
发表于 2019-3-23 09:51:12 | 显示全部楼层
guoaibing 发表于 2019-3-23 09:48
用了这段代码  图片确实上传到本地了  内容替换有问题
只有图片路径的后面,域名/wp-content/uploads没加 ...

你是说图片没有保存到本地?还是什么?你说的我看不懂
 楼主| 发表于 2019-3-23 09:51:28 | 显示全部楼层
siyi 发表于 2019-3-23 02:32
我用autopost pro采的,功能没火车头那么强大,关键简单好上手,直接支持远程图片保存到媒体库。

多谢各位大佬  问题是用点skycaiji  只能入库后  在wp里面实现图片本地化啊  真没啥插件了吗
保存到cdn的也行啊
 楼主| 发表于 2019-3-23 09:54:18 | 显示全部楼层
yumijie 发表于 2019-3-23 09:51
你是说图片没有保存到本地?还是什么?你说的我看不懂

图片能保存到本地  就是文章里面  图片地址  不完整  

只有图片地址的后面部分  我改改试试吧 谢大佬了
发表于 2019-3-23 10:57:10 | 显示全部楼层
easy-copy-paste   好用的不得鸟。
 楼主| 发表于 2019-3-23 11:21:02 | 显示全部楼层
viccy520 发表于 2019-3-23 10:57
easy-copy-paste   好用的不得鸟。

试过了   远程发布文章不会保存图片。。。
发表于 2019-3-23 11:32:40 | 显示全部楼层
guoaibing 发表于 2019-3-23 11:21
试过了   远程发布文章不会保存图片。。。

我用的没问题啊
 楼主| 发表于 2019-3-23 12:42:30 | 显示全部楼层

你是编辑器里发布文章应该   

已经解决了skycaiji里面要设置两次才能下载图片。。
 楼主| 发表于 2019-3-23 12:43:34 | 显示全部楼层
感谢各位回复的大佬
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-20 14:14 , Processed in 0.074546 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表