返回列表 发帖

遠程圖片本地化(discuz7.0無限擴展版)

功能及特色:
遠程圖片本地化.
與discu附件系統相結合.
刪除主題或者編輯主題附件將有效支持.
可配置水印及discu後台支持(原版控制)
權限控制(uid精準控制);
小圖片智能替換(再也不會把細米小的圖片也來本地化了)
單次減壓設置. (考慮到虛擬主機及連接超時問題, 所以縮小遠程個數)
插件安裝非常簡易, 僅一個文件解決

安裝方法
打開:
viewthread.php文件(根目錄)
查找:
複製內容到剪貼板代碼:
while($post = $sdb->fetch_array($query)) {


在它的下一行增加如下代碼:複製內容到剪貼板代碼:
//配置開始:
                $imgon = 1;                                //開啟水印.
                $minwidth = 100;             //最小圖片尺寸.
                $minimg = '--';         //小圖片替換成;
                $img5 = 5;              //幾張圖片遠程一次?,如果服務器不穩定,或者其它問題請設置一張圖片遠程一次, 然後多刷新幾次主題..
                $ruid = array($post['first'] && $post['uid'],1);          //使用者ID,第一個主題作者, 第二個是管理員,依次在後面加
                //判斷: 主題層/內容中函遠程圖片/用戶權限        
                if($post['first']==='1' &&strpos($post['message'],'[/img]')!==false &&in_array($discuz_uid,$ruid)===true){
                        set_time_limit(0);
                        //當前附件目錄
                        $mot = 'month_'.strtr(date('Ym',time()),array('20'=>''));
                        //絕對附件目錄
                        $attpath = strtr($_DCACHE['settings']['attachdir'],array('./'=>'')).'/';
                        //判斷附件目錄是否存在
                        if(is_dir($attpath.$mot)===false)
                                @mkdir($attpath.$mot,0777);
                        //初始化
                        $imgarray = $imgarr = array();
                        $inid = null;
                        //正則出遠程圖片地址塊,非記錄地址,一定要塊.
                        preg_match_all('/\[img(?:=.+)?\].+\[\/img\]/isU',$post['message'],$imgarray);
                        require_once DISCUZ_ROOT.'./include/image.class.php';
                        foreach ($imgarray[0] as $val){
                                //過濾掉前置.
                                $imgname = preg_replace(array('/\[img(?:=.+)?\]/isU','/\[\/img\]/isU'),'',$val);
                                //隨機化圖片名稱: $filenamea 附件字段filename需要;
                                $filenamea = time().random(16).'.gif';
                                $filename = $mot.'/'.strtr($filenamea,array('//'=>'/'));
                                //寫入目錄,>=PHP5;
                                $file = @file_get_contents($imgname);
                                if(file_put_contents($attpath.$filename,$file)){ // 如果寫入成功
                                        //取得附件圖片信息;
                                        $info_r = @getimagesize($attpath.$filename);
                                        //取得附件字節   
                                        $filesize = filesize($attpath.$filename);
                                        //取得當前時間
                                        $time = time();
                                        //如果圖片太小,就把那塊段替換成其它字符.並且中斷一次.
                                        if($info_r[0] < $minwidth){
                                                $post['message'] = &strtr($post['message'],array($val=>"$minimg"));
                                                unlink($attpath.$filename);
                                                continue;
                                        }
                                        //寫入附件數據表.
                                        $db->query("INSERT INTO `{$tablepre}attachments`
                                        (tid,pid,width,dateline,filename,filetype,filesize,attachment,isimage,uid) VALUES
                                       ('{$post['tid']}','{$post['pid']}','{$info_r[0]}','$time','$filenamea','{$info_r['mime']}','$filesize','$filename','1','{$post['uid']}')");
                                        //取得當前寫入ID值.
                                        $inid = $db->insert_id();
                                        $imgarr[] = $inid;
                                        //引用方式替換內容中的圖片
                                        $post['message'] =&strtr($post['message'],array($val=>"[attach]{$inid}[/attach]"));
                                        //判斷:水印開關/附件插入ID
                                        if($imgon === 1 && $inid){  //水印
                                                $image = new Image($attpath.$filename);
                               $image->Watermark();
                                        }
                                        if(count($imgarr)===$img5)
                                        break;                                       
                                }
                                //開始處理清除變量;
                        unset($info_r,$filesize);                        
                        }
                        //循環之外,如果有插入附件ID值,就將內容數據表更新, 並且設置附件attachment為1.
                        if(isset($inid)){
                                $db->query("UPDATE`{$tablepre}posts` SET `message` = '{$post['message']}',`attachment` ='1' WHERE `pid` ={$post['pid']} LIMIT 1 ;");
                        }

                }
                //結束

返回列表