从wordpress转到本系统也有一阵子了,速度虽然是刚刚的,但是也少了很多功能。比如远程图片本地化啥的。现在这个系统不比WP的插件多,所以需要的功能还得亲自上阵才行。
经过长达数小时的看BBS源码,终于搞定了。经测试,可以下载远程的有后缀,无后缀,http,https等图片。自动过滤站内图片采集
现在免费贴上代码。hook的文件是model_thread_create_end.php,打开它,添加以下代码。
?><?php
$one_style = setting_get('one_style');
if($one_style['hso_remote_image_enable']){
$user = user_read($uid);
user_login_check();
$post = post__read($pid);//再次重新读出来
$preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($post['message']),$matches);
if($matches && $preg){
//1表示无<img src=.../>标签
foreach($matches[1] as $image_url){
if(empty($image_url)) continue;
if(strpos($image_url,'http') === 0){//是http开头
$site_url = http_url_path();
$pos=strpos($image_url,$site_url);
if($pos===false){//判断是不是本站的图片,不是本站随即采集
$data = null;
if(function_exists('curl_init')){
//可能有https的图片
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_REFERER, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
}else{
$data = file_get_contents($image_url);//下载图片
}
if(!empty($data)){
//检查上传权限
empty($group['allowattach']) AND $gid != 1 AND message(-1, '您无权上传');
empty($data) AND message(-1, lang('data_is_empty'));
$size = strlen($data);
$size > 20480000 AND message(-1, lang('filesize_too_large', array('maxsize'=>'20M', 'size'=>$size)));
//解析图片
$img_data = getimagesize('data://image/jpeg;base64,'. base64_encode($data));
//Array ( [0] => 539 [1] => 316 [2] => 3 [3] => width="539" height="316" [bits] => 8 [mime] => image/png )
$width = $img_data[0];
$height = $img_data[1];
//获取上传目录
$attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');
$day = date($attach_dir_save_rule, time());
$path = $conf['upload_path'].'attach/'.$day;
$url = $conf['upload_url'].'attach/'.$day;
!is_dir($path) AND mkdir($path, 0777, TRUE);
//按规则生成图片名称并上传
$ext = file_ext(file_name($image_url));
if(empty($ext)||strlen($ext) != 3)$ext='png';//给个默认值
$filetypes = include APP_PATH.'conf/attach.conf.php';
!in_array($ext, $filetypes['all']) AND $ext = '_'.$ext;
$filename = $uid.'_'.xn_rand(15).'.'.$ext;
$destfile = $path.'/'.$filename;
$desturl = $url.'/'.$filename;
$filetype = attach_type($filename, $filetypes);
file_put_contents($destfile, $data) OR message(-1, lang('write_to_file_failed'));
//开始写入数据库,包含3张表thread,post,attach
$arr_attach = array(
'tid'=>$tid,
'pid'=>$pid,
'uid'=>$uid,
'filesize'=>$size,
'width'=>$img_data[0],
'height'=>$img_data[1],
'filename'=>"$day/$filename",
'orgfilename'=>'image.png',
'filetype'=>$filetype,
'create_date'=>time(),
'comment'=>'',
'downloads'=>0,
'isimage'=>1
);
attach_create($arr_attach);
//替换原来的图片路径
$post['message'] = str_replace($image_url, $desturl,$post['message']);
}
}
}
}
//更新主题和内容图片数量+1
thread__update($tid,array('images'=>count($matches[1])));
post_message_fmt($post, $gid);//用内置的html格式化函数
post__update($pid,array('images'=>count($matches[1]),'message'=>$post['message'],'message_fmt'=>$post['message_fmt']));
}
}我自己加了开关设置,你们可以自行去掉。最后来一张其它站的图片测试一发!
原图片地址:upload/attach/201709/1_7hh1kso8vioxpmd.jpg

收藏的用户(0) X
正在加载信息~
推荐阅读
站点信息
- 文章2314
- 用户1336
- 访客11813325
每日一句
Let's seek joy in the simple, quiet moments.
让我们在简单宁静的时刻中寻找快乐。
让我们在简单宁静的时刻中寻找快乐。