Discuz! 7.0下Wap访问显示图片的研究

2009-06-02 17:16:41 0 2260
由于管理自己的论坛,对论坛的一些功能有缺陷的话,会想办法进行完善,于是有了这篇文章。玩黑思路很重要,写程序思路一样很重要。由于网上对相关Discuz论坛wap显示图片的技术文章非常少,而官方迟迟不更新wap版本,所以只能靠自己了。

官方论坛搜了很久,也查看了别人对于5.5版本与6.0版本的处理方法,经过修改,我得到了相应的Discuz!7.0下wap显示图片的方法,利用wapcode这个函数做一些字符串替换,具体代码如下(修改global.func.php中的wapcode函数即可):
function wapcode($string) {
        global $lang;
        $string = str_replace(array('[attach]', '[/attach]'), array('<img src1="', '" alt="p" />'), $string);
        $string = str_replace(array('[img]', '[/img]'), array('<img src2="', '" alt="p" />'), $string);
              $string = preg_replace("/\[url=(http:\/\/.*?)\](.*?)\[\/url\]/is", "<a href=\"\\1\">\\2</a>", $string);
        $string = preg_replace("/\[url\](.*?)\[\/url\]/is", "<a href=\"\\1\">\\1</a>", $string);
            $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '&lt;', '&gt;'), $string);
        $string = str_replace(array('&lt;img src1="','&lt;img src2="', '" alt="p" /&gt;'), array('<img width="100" src="wapattachment.php?aid=','<img width="100" src="', '" alt="download..." />
'), $string);
        $string = str_replace(array('&lt;a href="', '"&gt;','&lt;/a&gt;'), array('<a href="', '">', '</a>'), $string);
        $string = preg_replace("/\[hide\](.+?)\[\/hide\]/is", $lang['post_hide_reply_hidden'], $string);
        $string = preg_replace("/\[hide=(\d+)\]\s*(.+?)\s*\[\/hide\]/ies", $lang['post_hide_reply_hidden'], $string);
                 for($i = 0; $i < 1; $i++) {
                $string = preg_replace("/\[(\w+)[^\]]*?\](.*?)\[\/\\1\]/is", "\\2", $string);
        }
               
        return $string;
}
测试了下,蛮成功的,图片能正常显示。一个两个图片也没有发现异常,不过图片多的时候突然手机里报错了,仔细看了下代码才发现:这个方法只改了wapcode($string)函数,但没有对wapcutstr($string, &$length)方法做相应调整,所以在前后翻页的时候可能会遇到因img标签被截断而导致页面无法正常显示,这样的话不仅是img标签了,如果不处理wapcutstr想用html标签都没可能性,因为字符串截断的时候不管是不是标签都当成字符串处理,于是我想改wapcutstr这个函数,网上也找到了相应的修改版,但我没继续跟下去,因为后来发现图文并茂的时候在手机里看起来不爽,于是我有了第二种显示图片的思路。

帖子里的图片无非两种:一是远程图片,用[img][/img]调用;而是本地图片,一般情况下可以直接从数据库中读取。既然这样,何必那么麻烦的替换字符串,直接把所有图片都显示出来放在帖子的第一页不就可以了。实现代码(将如下代码添加至thread.inc.php中的$post['author'] = !$post['anonymous'] ? $post['author'] : $lang['anonymous'];前面):
if(!$offset || $offset==-1){
                                if (preg_match_all("/\[img(.*?)\](.*?)\[\/img\]/is",$strpost,$imgout,PREG_PATTERN_ORDER)) {
                                        $imgnum=count($imgout[2]);
                                        $img="";
                                        for ($i=0;$i<$imgnum;$i++){
                                                if($imgout[1][$i] && strpos($imgout[1][$i],",")){
                                                $imgwidth=explode(",",str_replace("=","",$imgout[1][$i]));
                                                $newwidth=$imgwidth[0]>100?100:$imgwidth[0];
                                                }else{
                                                $newwidth=100;
                                                }
                                                $img.='<img width="'.$newwidth.'" src="'.$imgout[2][$i].'" alt="download..." />
';
                                        }
                                        $threadposts .= $img;
                                }
                                if($post['attachment']) {
                                        $attquery = $db->query("SELECT * FROM {$tablepre}attachments WHERE pid='$post[pid]'");
                                        while($attach = $db->fetch_array($attquery)) {
                                                if($attach['isimage']) {
                                                        $attachimg.='<img width="100" src="../attachments/'.$attach['attachment'].'" alt="download..." />
';
                                                }
                                        }
                                        $threadposts .= $attachimg;
                                }
                        }
至此,完美解决图片的基本显示问题,至于图片大小缩略图那种问题,没有继续深究;另外阅读权限问题,也没有考虑(想考虑的话稍微改点代码就可以了)

关于作者

oldjun132篇文章575篇回复

评论0次

要评论?请先  登录  或  注册