硬汉工作室

搜索
热搜: 活动 交友 discuz

[开发技术] discuz支持gif作为缩略图

[复制链接]
发表于 2019-9-1 22:02:35 | 显示全部楼层 |阅读模式
我用的是discuz 3.4 gbk,其他版本应该也差不多。

discuz默认情况下,gif是不能作为缩略图的,有位站长希望让gif也能作为缩略图,就是某个板块开启图片列表功能后,发新帖,gif的会自动设置为封面,看了看代码,插件估计无法实现,只能改源码,一共需要改两个文件:
1,function_foum.php
列表里的缩略图是通过这个文件里的getthreadcover来返回的,默认情况下,缩略图是jpg后缀的,并且根据tid设置路径规则,缩略图存放在'data\attachment\forum\threadcover'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.jpg';,其实gif的也是这个,只是后缀是gif,因此我们必须动态获取封面的后缀了。封面图片放在forum_threadiamge表里,我们可以根据图片后缀来判断图片类型。修改后的函数如下:
function getthreadcover($tid, $cover = 0, $getfilename = 0) {
        global $_G;
        if(empty($tid)) {
                return '';
        }
        $coverpath = '';
        $filename = DB::result_first('select attachment from %t where tid = %d', array('forum_threadimage', $tid));
        $ext = fileext($filename);
        $covername = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.'.$ext;
        //$covername = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.jpg'; // 原来的。

    if($getfilename) {
                return $covername;
        }
        if($cover) {
                $coverpath = ($cover < 0 ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/'.$covername;
        }
        return $coverpath;
}


2,function_post.php
这个文件里的setthreadcover函数是用来设置帖子封面的,无论是新发帖,手动设置封面以及后来重建封面,都会调用这个函数。我们要改的是判断图片是否是gif,如果是gif就不走系统设置封面的流程,而是直接把这个gif图片复制到封面路径里,直接附上代码吧。
function setthreadcover($pid, $tid = 0, $aid = 0, $countimg = 0, $imgurl = '') {

        global $_G;
        $cover = 0;
        if(empty($_G['uid']) || !intval($_G['setting']['forumpicstyle']['thumbheight']) || !intval($_G['setting']['forumpicstyle']['thumbwidth'])) {
                return false;
        }

        if(($pid || $aid) && empty($countimg)) {
                if(empty($imgurl)) {
                        if($aid) {
                                $attachtable = 'aid:'.$aid;
                                $attach = C::t('forum_attachment_n')->fetch('aid:'.$aid, $aid, array(1, -1));
                        } else {
                                $attachtable = 'pid:'.$pid;
                                $attach = C::t('forum_attachment_n')->fetch_max_image('pid:'.$pid, 'pid', $pid);
                        }
                        if(!$attach) {
                                return false;
                        }
                        if(empty($_G['forum']['ismoderator']) && $_G['uid'] != $attach['uid']) {
                                return false;
                        }
                        $pid = empty($pid) ? $attach['pid'] : $pid;
                        $tid = empty($tid) ? $attach['tid'] : $tid;
                        $picsource = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/'.$attach['attachment'];
                } else {
                        $attachtable = 'pid:'.$pid;
                        $picsource = $imgurl;
                }


                $basedir = !$_G['setting']['attachdir'] ? (DISCUZ_ROOT.'./data/attachment/') : $_G['setting']['attachdir'];
                $coverdir = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/';
                dmkdir($basedir.'./forum/'.$coverdir);

        if(fileext($picsource) == 'gif') { // 判断是否是gif
            copy($picsource, 'data/attachment/forum/'.$coverdir.$tid.'.gif'); // 直接把gif图片复制到缩略图目录,并改名为tid.gif
            $cover = C::t('forum_attachment_n')->count_image_by_id($attachtable, 'pid', $pid);
            if($imgurl && empty($cover)) {
                $cover = 1;
            }

        } else {
            require_once libfile('class/image');
            $image = new image();
            if($image->Thumb($picsource, 'forum/'.$coverdir.$tid.'.jpg', $_G['setting']['forumpicstyle']['thumbwidth'], $_G['setting']['forumpicstyle']['thumbheight'], 2)) {
                $remote = '';
                if(getglobal('setting/ftp/on')) {
                    if(ftpcmd('upload', 'forum/'.$coverdir.$tid.'.jpg')) {
                        $remote = '-';
                    }
                }
                $cover = C::t('forum_attachment_n')->count_image_by_id($attachtable, 'pid', $pid);
                if($imgurl && empty($cover)) {
                    $cover = 1;
                }
                $cover = $remote.$cover;
            } else {
                return false;
            }
        }

        }
        if($countimg) {
                if(empty($cover)) {
                        $thread = C::t('forum_thread')->fetch($tid);
                        $oldcover = $thread['cover'];

                        $cover = C::t('forum_attachment_n')->count_image_by_id('tid:'.$tid, 'pid', $pid);
                        if($cover) {
                                $cover = $oldcover < 0 ? '-'.$cover : $cover;
                        }
                }
        }
        if($cover) {
                C::t('forum_thread')->update($tid, array('cover' => $cover));
                return true;
        }
}


完事了。
有关PHP系统、Discuz或网站等各种问题,可以联系QQ1069971363寻求付费支持
Discuz插件商店:http://addon.dismall.com/?@56030.developer
回复

使用道具 举报

QQ|Archiver|手机版|小黑屋|硬汉工作室 ( 冀ICP备13021567号-9 )

GMT+8, 2024-4-20 19:57 , Processed in 0.052191 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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