dedecms的圖集功能可以實現圖集的單頁、多頁和多行多列的顯示方法,我在前面的文章中,已經實現了“圖集內容自定義分頁”全文請參考:http://blog.csdn.net/expendable/archive/2007/06/07/1643152.aspx,但是對於多行多列的顯示方式,默認爲直接把圖集的圖片通過width限制寬度顯示在頁面上,導致當圖集內容很多的時候,頁面打開速度慢,並且圖片也被擠壓變形,效果並不理想。解決的方法是讓圖集也生成縮略圖,並且縮略圖的功能只對多行多列顯示方法有效。
下面是具體實現步驟(對dedecms 4.0有效,不保證對其他版本有效):
第一步:修改/dede/album_add.php文件,新增多行多列參數“生成縮略圖”,意思爲當選擇這個選項,生成縮略圖,不選擇,則不生成。參考代碼如下:
<tr>
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80"> 多列式參數:</td>
<td>行
<input name="row" type="text" id="row" value="3" size="6">
列
<input name="col" type="text" id="col" value="3" size="6">
圖片寬度限制: <input name="ismaxwidth" type="checkbox" id="ismaxwidth" value="1" class="np">
<input name="ddmaxwidth" type="text" id="ddmaxwidth" value="150" size="6">
像素
<input name="islit" type="checkbox" id="islit" value="1" class="np">
生成縮略圖
</td>
</tr>
<td height="24" colspan="4" class="bline"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80"> 多列式參數:</td>
<td>行
<input name="row" type="text" id="row" value="3" size="6">
列
<input name="col" type="text" id="col" value="3" size="6">
圖片寬度限制: <input name="ismaxwidth" type="checkbox" id="ismaxwidth" value="1" class="np">
<input name="ddmaxwidth" type="text" id="ddmaxwidth" value="150" size="6">
像素
<input name="islit" type="checkbox" id="islit" value="1" class="np">
生成縮略圖
</td>
</tr>
第二步:修改/dede/album_add_action.php文件,響應請求,並判斷如果需要生成縮略圖,則生成。相關代碼如下:
//處理並保存所指定的圖片
//------------------------------
$imgurls = "{dede:pagestyle maxwidth='$maxwidth' ddmaxwidth='$ddmaxwidth' row='$row' col='$col' value='$pagestyle' islit='$islit' ismaxwidth='$ismaxwidth' /} ";
for($i=1;$i<=120;$i++){
if(isset(${'imgurl'.$i})||(isset($_FILES['imgfile'.$i]['tmp_name']) && is_uploaded_file($_FILES['imgfile'.$i]['tmp_name']))){
$iinfo = str_replace("'","`",stripslashes(${'imgmsg'.$i}));
//非上傳圖片
if(!is_uploaded_file($_FILES['imgfile'.$i]['tmp_name'])){
$iurl = stripslashes(${'imgurl'.$i});
if(trim($iurl)=="") continue;
$iurl = trim(str_replace($cfg_basehost,"",$iurl));
if((eregi("^http://",$iurl) && !eregi($cfg_basehost,$iurl)) && $isUrlOpen)
//遠程圖片
{
$reimgs = "";
if($isUrlOpen && $isrm==1)
{
$reimgs = GetRemoteImage($iurl,$adminID);
if(is_array($reimgs)){
$imgurls .= "{dede:img text='$iinfo' width='".$reimgs[1]."' height='".$reimgs[2]."'} ".$reimgs[0]." {/dede:img} ";
}else{
echo "下載:".$iurl." 失敗,可能圖片有反採集功能或http頭不正確!<br /> ";
}
}else{
$imgurls .= "{dede:img text='$iinfo' width='' height=''} ".$iurl." {/dede:img} ";
}
//站內圖片
}else if($iurl!=""){
$imgfile = $cfg_basedir.$iurl;
if(is_file($imgfile)){
$info = "";
//生成縮略圖
if($islit==1) GetDDImage('ddfirst',$iurl,$ddisremote);
$imginfos = GetImageSize($imgfile,$info);
$imgurls .= "{dede:img text='$iinfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img} ";
}
}
//直接上傳的圖片
}else{
$sparr = Array("image/pjpeg","image/jpeg","image/gif","image/png","image/x-png","image/wbmp");
if(!in_array($_FILES['imgfile'.$i]['type'],$sparr)){
continue;
}
$uptime = mytime();
$imgPath = $cfg_image_dir."/".strftime("%y%m%d",$uptime);
MkdirAll($cfg_basedir.$imgPath,777);
CloseFtp();
$filename = $imgPath."/".dd2char($cuserLogin->getUserID().strftime("%H%M%S",$uptime).mt_rand(100,999).$i);
$fs = explode(".",$_FILES['imgfile'.$i]['name']);
$filename = $filename.".".$fs[count($fs)-1];
@move_uploaded_file($_FILES['imgfile'.$i]['tmp_name'],$cfg_basedir.$filename);
@WaterImg($cfg_basedir.$filename,'up');
$imgfile = $cfg_basedir.$filename;
if(is_file($imgfile)){
$iurl = $filename;
$info = "";
//生成縮略圖
if($islit==1) GetDDImage('ddfirst',$iurl,$ddisremote);
$imginfos = GetImageSize($imgfile,$info);
$imgurls .= "{dede:img text='$iinfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img} ";
//把新上傳的圖片信息保存到媒體文檔管理檔案中
$inquery = "
INSERT INTO #@__uploads(title,url,mediatype,width,height,playtime,filesize,uptime,adminid,memberid)
VALUES ('$title".$i."','$filename','1','".$imginfos[0]."','".$imginfos[1]."','0','".filesize($imgfile)."','".mytime()."','$adminID','0');
";
$dsql->SetQuery($inquery);
$dsql->ExecuteNoneQuery();
}
}
}//含有圖片的條件
}//循環結束
$imgurls = addslashes($imgurls);
//加入附加表
//----------------------------------
//------------------------------
$imgurls = "{dede:pagestyle maxwidth='$maxwidth' ddmaxwidth='$ddmaxwidth' row='$row' col='$col' value='$pagestyle' islit='$islit' ismaxwidth='$ismaxwidth' /} ";
for($i=1;$i<=120;$i++){
if(isset(${'imgurl'.$i})||(isset($_FILES['imgfile'.$i]['tmp_name']) && is_uploaded_file($_FILES['imgfile'.$i]['tmp_name']))){
$iinfo = str_replace("'","`",stripslashes(${'imgmsg'.$i}));
//非上傳圖片
if(!is_uploaded_file($_FILES['imgfile'.$i]['tmp_name'])){
$iurl = stripslashes(${'imgurl'.$i});
if(trim($iurl)=="") continue;
$iurl = trim(str_replace($cfg_basehost,"",$iurl));
if((eregi("^http://",$iurl) && !eregi($cfg_basehost,$iurl)) && $isUrlOpen)
//遠程圖片
{
$reimgs = "";
if($isUrlOpen && $isrm==1)
{
$reimgs = GetRemoteImage($iurl,$adminID);
if(is_array($reimgs)){
$imgurls .= "{dede:img text='$iinfo' width='".$reimgs[1]."' height='".$reimgs[2]."'} ".$reimgs[0]." {/dede:img} ";
}else{
echo "下載:".$iurl." 失敗,可能圖片有反採集功能或http頭不正確!<br /> ";
}
}else{
$imgurls .= "{dede:img text='$iinfo' width='' height=''} ".$iurl." {/dede:img} ";
}
//站內圖片
}else if($iurl!=""){
$imgfile = $cfg_basedir.$iurl;
if(is_file($imgfile)){
$info = "";
//生成縮略圖
if($islit==1) GetDDImage('ddfirst',$iurl,$ddisremote);
$imginfos = GetImageSize($imgfile,$info);
$imgurls .= "{dede:img text='$iinfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img} ";
}
}
//直接上傳的圖片
}else{
$sparr = Array("image/pjpeg","image/jpeg","image/gif","image/png","image/x-png","image/wbmp");
if(!in_array($_FILES['imgfile'.$i]['type'],$sparr)){
continue;
}
$uptime = mytime();
$imgPath = $cfg_image_dir."/".strftime("%y%m%d",$uptime);
MkdirAll($cfg_basedir.$imgPath,777);
CloseFtp();
$filename = $imgPath."/".dd2char($cuserLogin->getUserID().strftime("%H%M%S",$uptime).mt_rand(100,999).$i);
$fs = explode(".",$_FILES['imgfile'.$i]['name']);
$filename = $filename.".".$fs[count($fs)-1];
@move_uploaded_file($_FILES['imgfile'.$i]['tmp_name'],$cfg_basedir.$filename);
@WaterImg($cfg_basedir.$filename,'up');
$imgfile = $cfg_basedir.$filename;
if(is_file($imgfile)){
$iurl = $filename;
$info = "";
//生成縮略圖
if($islit==1) GetDDImage('ddfirst',$iurl,$ddisremote);
$imginfos = GetImageSize($imgfile,$info);
$imgurls .= "{dede:img text='$iinfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img} ";
//把新上傳的圖片信息保存到媒體文檔管理檔案中
$inquery = "
INSERT INTO #@__uploads(title,url,mediatype,width,height,playtime,filesize,uptime,adminid,memberid)
VALUES ('$title".$i."','$filename','1','".$imginfos[0]."','".$imginfos[1]."','0','".filesize($imgfile)."','".mytime()."','$adminID','0');
";
$dsql->SetQuery($inquery);
$dsql->ExecuteNoneQuery();
}
}
}//含有圖片的條件
}//循環結束
$imgurls = addslashes($imgurls);
//加入附加表
//----------------------------------
$imgurls = "{dede:pagestyle maxwidth='$maxwidth' ddmaxwidth='$ddmaxwidth' row='$row' col='$col' value='$pagestyle' islit='$islit' ismaxwidth='$ismaxwidth' /}/r/n";
爲保存ismaxwidth和islit兩個參數,以方便頁面顯示的時候判斷
if($islit==1) GetDDImage('ddfirst',$iurl,$ddisremote);
爲當勾選“生成縮略圖”後,調用GetDDImage直接生成縮略圖,縮略圖生成的方法爲:原圖片名稱+_lit+文件後綴。
第三步:修改顯示方法,修改/include/inc_channel_unit.php文件,修改GetImgLinks函數,修改後爲:
//獲得圖片的展示頁面
//---------------------------
function GetImgLinks($fvalue)
{
$revalue = "";
$dtp = new DedeTagParse();
$dtp->LoadSource($fvalue);
if(!is_array($dtp->CTags)){
$dtp->Clear();
return "無圖片信息!";
}
$ptag = $dtp->GetTag("pagestyle");
if(is_object($ptag)){
$pagestyle = $ptag->GetAtt('value');
$maxwidth = $ptag->GetAtt('maxwidth');
$ddmaxwidth = $ptag->GetAtt('ddmaxwidth');
$irow = $ptag->GetAtt('row');
$icol = $ptag->GetAtt('col');
$islit = $ptag->GetAtt('islit');
$ismaxwidth = $ptag->GetAtt('ismaxwidth');
if(empty($maxwidth)) $maxwidth = $GLOBALS['cfg_album_width'];
}else{
$pagestyle = 2;
$maxwidth = $GLOBALS['cfg_album_width'];
$ddmaxwidth = 200;
}
if($pagestyle == 3){
if(empty($irow)) $irow = 4;
if(empty($icol)) $icol = 4;
}
//遍歷圖片信息
$mrow = 0;
$mcol = 0;
$photoid = 0;
$images = array();
$i=0;
foreach($dtp->CTags as $ctag){
if($ctag->GetName()=="img"){
$iw = $ctag->GetAtt('width');
$ih = $ctag->GetAtt('heigth');
$alt = str_replace("'","",$ctag->GetAtt('text'));
$src = trim($ctag->GetInnerText());
if($iw > $maxwidth) $iw = $maxwidth;
$iw = (empty($iw) ? "" : "width='$iw'");
//全部列出式或分頁式圖集
if($pagestyle<3){
if($revalue==""){
$revalue = "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}else{
//分頁顯示
if($pagestyle==2) {
if(($i % $GLOBALS['cfg_img_pagesize'])==0)
$revalue .= "#p#分頁標題#e#<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
else $revalue .= "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}
//單頁顯示
if($pagestyle==1) $revalue .= "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}
$i++;
//多列式圖集
}else if($pagestyle==3){
$images[$photoid][0] = $src;
$images[$photoid][1] = $alt;
$photoid++;
}
}
}
//重新運算多列式圖集
if($pagestyle==3){
if(empty($ddmaxwidth)) $ddmaxwidth = 200;
$picnum = count($images);
$sPos = 0;
if($icol==0) $icol = 1;
$tdwidth = ceil(100 / $icol);
while($sPos < $picnum){
$revalue .= "<table width='90%' border='0' cellpadding='5' cellspacing='1'> ";
$revalue .= "<tr height='0'> ";
for($j=0;$j < $icol;$j++){ $revalue .= "<td width='{$tdwidth}%'></td> "; }
$revalue .= "</tr>";
for($i=0;$i < $irow;$i++){
$revalue .= "<tr align='center'> ";
for($j=0;$j < $icol;$j++){
if(!isset($images[$sPos])){ $revalue .= "<td></td> "; }
else{
$src = $images[$sPos][0];
//如果使用縮略圖
if($islit==1) $srcs = str_replace('.','_lit.',$src);
if($ismaxwidth==1) {
$max_width = "width=$ddmaxwidth";
}else{
$max_width = "";
}
$alt = $images[$sPos][1];
$revalue .= "<td valign='top'><a href='{$GLOBALS['cfg_phpurl']}/showphoto.php?aid={$this->ArcID}&src=".urlencode($src)."&npos=$sPos' target='_blank'><img src='$srcs' $max_width alt='$alt' border='0'/></a><br/>$alt </td> ";
$sPos++;
}
}
$revalue .= "</tr> ";
if(!isset($images[$sPos])) break;
}
if(!isset($images[$sPos])){
$revalue .= "</table>";
break;
}else{
$revalue .= "</table>#p#分頁標題#e#";
}
}
}
unset($dtp);
unset($images);
return $revalue;
}
//---------------------------
function GetImgLinks($fvalue)
{
$revalue = "";
$dtp = new DedeTagParse();
$dtp->LoadSource($fvalue);
if(!is_array($dtp->CTags)){
$dtp->Clear();
return "無圖片信息!";
}
$ptag = $dtp->GetTag("pagestyle");
if(is_object($ptag)){
$pagestyle = $ptag->GetAtt('value');
$maxwidth = $ptag->GetAtt('maxwidth');
$ddmaxwidth = $ptag->GetAtt('ddmaxwidth');
$irow = $ptag->GetAtt('row');
$icol = $ptag->GetAtt('col');
$islit = $ptag->GetAtt('islit');
$ismaxwidth = $ptag->GetAtt('ismaxwidth');
if(empty($maxwidth)) $maxwidth = $GLOBALS['cfg_album_width'];
}else{
$pagestyle = 2;
$maxwidth = $GLOBALS['cfg_album_width'];
$ddmaxwidth = 200;
}
if($pagestyle == 3){
if(empty($irow)) $irow = 4;
if(empty($icol)) $icol = 4;
}
//遍歷圖片信息
$mrow = 0;
$mcol = 0;
$photoid = 0;
$images = array();
$i=0;
foreach($dtp->CTags as $ctag){
if($ctag->GetName()=="img"){
$iw = $ctag->GetAtt('width');
$ih = $ctag->GetAtt('heigth');
$alt = str_replace("'","",$ctag->GetAtt('text'));
$src = trim($ctag->GetInnerText());
if($iw > $maxwidth) $iw = $maxwidth;
$iw = (empty($iw) ? "" : "width='$iw'");
//全部列出式或分頁式圖集
if($pagestyle<3){
if($revalue==""){
$revalue = "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}else{
//分頁顯示
if($pagestyle==2) {
if(($i % $GLOBALS['cfg_img_pagesize'])==0)
$revalue .= "#p#分頁標題#e#<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
else $revalue .= "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}
//單頁顯示
if($pagestyle==1) $revalue .= "<center><a href='$src' target='_blank'><img src='$src' alt='$alt' $iw border='0'/></a><br/>$alt<br/></center> ";
}
$i++;
//多列式圖集
}else if($pagestyle==3){
$images[$photoid][0] = $src;
$images[$photoid][1] = $alt;
$photoid++;
}
}
}
//重新運算多列式圖集
if($pagestyle==3){
if(empty($ddmaxwidth)) $ddmaxwidth = 200;
$picnum = count($images);
$sPos = 0;
if($icol==0) $icol = 1;
$tdwidth = ceil(100 / $icol);
while($sPos < $picnum){
$revalue .= "<table width='90%' border='0' cellpadding='5' cellspacing='1'> ";
$revalue .= "<tr height='0'> ";
for($j=0;$j < $icol;$j++){ $revalue .= "<td width='{$tdwidth}%'></td> "; }
$revalue .= "</tr>";
for($i=0;$i < $irow;$i++){
$revalue .= "<tr align='center'> ";
for($j=0;$j < $icol;$j++){
if(!isset($images[$sPos])){ $revalue .= "<td></td> "; }
else{
$src = $images[$sPos][0];
//如果使用縮略圖
if($islit==1) $srcs = str_replace('.','_lit.',$src);
if($ismaxwidth==1) {
$max_width = "width=$ddmaxwidth";
}else{
$max_width = "";
}
$alt = $images[$sPos][1];
$revalue .= "<td valign='top'><a href='{$GLOBALS['cfg_phpurl']}/showphoto.php?aid={$this->ArcID}&src=".urlencode($src)."&npos=$sPos' target='_blank'><img src='$srcs' $max_width alt='$alt' border='0'/></a><br/>$alt </td> ";
$sPos++;
}
}
$revalue .= "</tr> ";
if(!isset($images[$sPos])) break;
}
if(!isset($images[$sPos])){
$revalue .= "</table>";
break;
}else{
$revalue .= "</table>#p#分頁標題#e#";
}
}
}
unset($dtp);
unset($images);
return $revalue;
}
關鍵點1:
$islit = $ptag->GetAtt('islit');
$ismaxwidth = $ptag->GetAtt('ismaxwidth');
獲得$islit和$ismaxwidth兩個參數
關鍵點2:
if($islit==1) $srcs = str_replace('.','_lit.',$src);
if($ismaxwidth==1) {
$max_width = "width=$ddmaxwidth";
}else{
$max_width = "";
}
當參數$islit爲1,說明有縮略圖,直接調用縮略圖顯示。當$ismaxwidth無值時,直接使用縮略圖本身的長寬大小設置。
第四步:參照第一、二步的方法,修改/dede/album_edit.php和/dede/album_edit_action.php兩個文件。
到此結束,祝您好運