discuz论坛外部调用图片代码整理

作者: 坎肩儿 分类: DISCUZ论坛 发布时间: 2009-12-01 23:57

最近的一个项目需要外部调用论坛的图片,网上搜集了一段,不过有弊端,还需进一步改进,先看代码:

<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<script language=”javascript”>
<!–
var focus_width=260
var focus_height=200
var text_height=0
var swf_height = focus_height+text_height

<?php
/*
design by Clement
change by xuon
*/
header(‘Content-Tyep:text/html; charset=utf-8′);
//—设置—start
$shownums = 5;//演示个数 6以内
$searchnums = 50;//搜索总数.
$cachelife = 1800;//缓存时间.
$orderby = ‘dateline';//搜索顺序:按日期dateline.按浏览次数downloads.随机rand.
$cachename = ‘pichome';//缓存名.若与何插件冲突请修改,只要不冲突不会影响使用.
$searchfid = ”;//搜索fid范围.不限制留空.请用逗号相隔,如’1,2,3′.
//—设置—end
if((@!include(‘forumdata/cache/cache_’.$cachename.’.php’)) || $expiration < time() || $cacheshownums != $shownums || $cachesearchnums != $searchnums || $cachecachelife != $cachelife || $cacheorderby != $orderby || $cachecachename != $cachename || $cachesearchfid != $searchfid) {
require_once ‘./include/common.inc.php';
$orderby = $orderby != ‘rand’ ? ‘attach.’.$orderby : ‘rand()';
$fids = $searchfid ? ‘AND ‘.”t.fid IN ($searchfid)” : ”;
$data = array();
$query = $db->query(“SELECT attach.aid, attach.attachment, t.tid, t.fid, t.subject FROM {$tablepre}attachments attach LEFT JOIN {$tablepre}threads t ON t.tid=attach.tid WHERE attach.readperm=’0′ AND displayorder>=’0′ AND filetype=’image/pjpeg’ $fids GROUP BY attach.tid ORDER BY $orderby DESC LIMIT $searchnums”);
while ($pic = $db->fetch_array($query)) {
  $data[‘pic’][$pic[aid]][‘showpicpics’] = $attachurl.’/’.$pic[‘attachment’];
  $data[‘pic’][$pic[aid]][‘showpiclinks’] = “viewthread.php?tid=$pic[tid]”;
  $data[‘pic’][$pic[aid]][‘showpictexts’] = str_replace(‘\”, ‘&nbsp;’,$pic[‘subject’]);
}
$data[‘expiration’] = $cachelife + time();
$data[‘cacheshownums’] = $shownums;
$data[‘cachesearchnums’] = $searchnums;
$data[‘cachecachelife’] = $cachelife;
$data[‘cacheorderby’] = $orderby;
$data[‘cachecachename’] = $cachename;
$data[‘cachesearchfid’] = $searchfid;
require_once DISCUZ_ROOT.’./include/cache.func.php';
writetocache($cachename, ”, getcachevars($data));
include(DISCUZ_ROOT.’./forumdata/cache/cache_’.$cachename.’.php’);
}
shuffle($pic);
$i = 0;
$j = 0;
while ($j < $shownums && isset($pic[$i])) {
if(is_readable($pic[$i][‘showpicpics’])) {
  $showpicpics .= $comma.$pic[$i][‘showpicpics’];
  $showpiclinks .= $comma.$pic[$i][‘showpiclinks’];
  $showpictexts .= $comma.$pic[$i][‘showpictexts’];
  $comma = ‘|';
  $j++;
}
$i++;
}
?>
var pics='<?=$showpicpics?>’
var links='<?=$showpiclinks?>’
var texts='<?= $showpictexts?>’

document.write(‘<object classid=”clsid:d27cdb6e-ae6d-11cf-96b8-444553540000″ codebase=”http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0” width=”‘+ focus_width +'” height=”‘+ swf_height +'”>’);
document.write(‘<param name=”allowScriptAccess” value=”sameDomain”><param name=”movie” value=”http://www.impn.net/images/player.swf”;><param name=”quality” value=”high”><param name=”bgcolor” value=”#DADADA”>’);
document.write(‘<param name=”menu” value=”false”><param name=wmode value=”opaque”>’);
document.write(‘<param name=”FlashVars” value=”pics=’+pics+’&links=’+links+’&texts=’+texts+’&borderwidth=’+focus_width+’&borderheight=’+focus_height+’&textheight=’+text_height+'”>’);
document.write(‘<embed src=”http://www.impn.net/images/player.swf“; wmode=”opaque” FlashVars=”pics=’+pics+’&links=’+links+’&texts=’+texts+’&borderwidth=’+focus_width+’&borderheight=’+focus_height+’&textheight=’+text_height+'” menu=”false” bgcolor=”#DADADA” quality=”high” width=”‘+ focus_width +'” height=”‘+ swf_height +'” allowScriptAccess=”sameDomain” type=”application/x-shockwave-flash” pluginspage=”http://www.macromedia.com/go/getflashplayer“; />’);            document.write(‘</object>’);
//–>
</script>

复制上面的代码,另存api-js.php,保存到论坛根目录,即可进行调用。如果是外部调用,可以用iframe解决。

经测试这段代码的弊端:

  1. 只能调用jpg格式图片,gif不能调用
  2. 只调用每篇帖子的第一张图片

博客中还整理了一片首页四格的代码,点击查看详情