WordPress自动保存远程图片(更新2.5)

  支持wordpress2.3.3和2.5版
  下载地址:http://www.01on.com/auto-save-image.rar
  原文链接:Wordpress写日志时自动保存远程图片的插件

网络上比较流行的是老鬼的做法.我在其基础上针对最新版做了些修改.

自动保存远程图片修改方法如下:

1、在wp-admin/edit-form-advanced.php 第167行

HTML 代码:


<span id=”autosave”></span>


后面加增加HTML 代码:


Save pic:<input type=”checkbox” name=”savepic” value=”checked” />


2、修改 wp-admin/post.php,将第7行的PHP 代码:


wp_reset_vars(array(’action’, ’safe_mode’, ’withcomments’, ’posts’,

’content’, ’edited_post_title’, ’comment_error’, ’profile’, ’trackback_url’,

excerpt’, ’showcomments’, ’commentstart’, ’commentend’, ’commentorder’));


改为

PHP 代码:


wp_reset_vars(array(’action’, ’safe_mode’, ’withcomments’, ’posts’, ’content’,

edited_post_title’, ’comment_error’, ’profile’, ’trackback_url’, ’excerpt’, ’showcomments’,

’commentstart’, ’commentend’, ’commentorder’, ’savepic’));



3、在wp-admin/includes/post.php的开头增加PHP 代码:


function open($file,$type=”){
global $fromurl,$referer;
$cachename=$file;
if($type){
$file=$fromurl.’/’.$type.’/’.$file;
}else{
$file=$fromurl.$file;
}
if($open=file($file)){
$count=count($open);
for($i=0;$i<$count;$i++){
$theget.=$open[$i];
}
}else{
die(’请求过多,超时,请刷新’);
}
return $theget;
}

function cut($file,$from,$end){

$message=explode($from,$file);
$message=explode($end,$message[1]);
return        $message[0];
}
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;
}
function random($length) {
$hash = ”;
$chars = ’ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz’;
$max = strlen($chars) – 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}

function mkdirs($dir)
{
if(!is_dir($dir))
{
mkdirs(dirname($dir));
mkdir($dir);
}
return ;
}


再在PHP 代码:


$_POST[’post_content’] = $_POST[’content’];


的后面添加以下代码:
注意,上面的代码有两个地方有,第一个是修改的,最后一个是添加的.你如果只加添加的功能就在最后那个加,如果修改的也要加,就在前面的也加上.PHP 代码:


if($_POST[’savepic’]==”checked”){
// begin to save pic;
$img_array = array();
$content1 = stripslashes($_POST[’post_content’]);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);
//echo $content1;//文章内容嘿嘿开始处理了
preg_match_all(”/(src|SRC)=”(http://(.+).(gif|jpg|jpeg|bmp|png))/isU”,$content1,$img_array);//正则开始匹配所有的图片并放入数据
$img_array = array_unique(dhtmlspecialchars($img_array[2]));

print_r($img_array);
set_time_limit(0);

foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);//开始获取图片了哦
$filetime = time();
$filepath = ”../wp-content/uploads/pic2/”.date(”Y”,$filetime).”/”.date(”m”,$filetime).”/”;//图片保存的路径目录
!is_dir($filepath) ? mkdirs($filepath) : null;
$filename = date(”YmdHis”,$filetime).random(1).’.’.substr($value,-3,3);
$fp = @fopen($filepath.$filename,”w”);
@fwrite($fp,$get_file);
fclose($fp);//完工,哈
$content1 = preg_replace(”/”.addcslashes($value,”/”).”/isU”, ”/wp-content/uploads/pic2/”. date(”Y”,$filetime).”/”.date(”m”,$filetime).”/”.$filename, $content1);   // 顺便替换一下文章里面的图片地址

}

$_POST[’post_content’] = AddSlashes($content1); //老鬼的旧版本没有这个转义过程所以会出错

// end save pic;
}


完毕。现在,你再发表文章时,提交按钮旁边多了个选项选中“save pic”,再点“发表”,即可保存远程图片了。总结一下,要修改的文件有:wp-admin/edit-form-advanced.php
wp-admin/post.php
wp-admin/includes/post.php

注:要打开了PHP.INI的output_buffering 和allow_url_fopen

出自:http://www.rongdede.com/archives/16.html

4 Comments

Add a Comment
  1. 这东西不就是转贴利器吗!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

猫言猫语 © 2007-2014 Frontier Theme