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两个文件。
到此结束,祝您好运