-
Discuz X3.2 提取帖子首张图片生成缩略图代码
- 时间:2025-03-29 15:01:53 来源: 人气:0
在开发和优化网站内容时,确保图片能够快速加载并正确显示是至关重要的。以下是一篇关于如何通过代码实现帖子图片附件生成及缩略图功能的SEO优化文章。 在已获得帖子图片附件aid的情况下,可以直接使用以下代码来生成图片:
<!--{eval $imagelistkey = getforumimg($thecover[aid], 0, 225, 0); }-->
或者,另一种方法是在只有帖子tid的情况下获取帖子缩略图。这需要单独创建一个名为aidpic.php的文件,并将其放置在根目录下。在使用时,缩略图地址为aidpic.php?aid=帖子tid&宽x高,例如:
这些缩略图会被生成到自动创建的data/aidpic目录中。
以下是aidpic.php的具体代码实现:
<?php
require_once './source/class/class_core.php';
$discuz = & discuz_core::instance();
$discuz->init();
list($w,$h)=explode("x",$_G['gp_size']);
$m=0;
if($w==0&&$h==0){
$m=5;
}elseif ($h==0){
$m=3;
}elseif ($w==0){
$m=4;
}
$nopic='./static/image/common/nophotosmall.gif';//缺省图片
$aid=intval($_G['gp_aid']);
$dir="data/aidpic/";
$subdir=$dir."/{$w}x{$h}x{$m}/";
$thumbfile=$subdir."/".$aid.".jpg";
if(file_exists($thumbfile)){
header("location:{$thumbfile}");
die();
}
$tableid=substr($aid,-1,1);
$attach=DB::fetch_first("SELECT a.tid,a.attachment,a.remote
FROM ".DB::table("forum_attachment_{$tableid}")." a
WHERE a.`tid` ='$aid'
AND a.`isimage`<>0
order by a.aid asc
limit 0,1");
if($attach){
$attachurl=$attach['remote']?$_G['setting']['ftp']['attachurl']:$_G['setting']['attachurl'];
$attachfile=$attachurl."/forum/".$attach['attachment'];
if(!is_dir($dir)) @mkdir($dir);
if(!is_dir($subdir)) @mkdir($subdir);
dzthumb($attachfile,$thumbfile,$w,$h,$m);
header("location:{$thumbfile}");
die();
}else{
header("location:$nopic");
die();
}
function dzthumb($srcfile,$dstfile,$dstw,$dsth=0,$mode=0,$data=''){
$data=$data==''?@GetImageSize($srcfile):$data;
if(!$data) return false;
if($data[2]==2) $im=@ImageCreateFromJPEG($srcfile);
elseif ($data[2]==1) $im=@ImageCreateFromGIF($srcfile);
elseif($data[2]==3) $im=@ImageCreateFromPNG($srcfile);
list($img_w, $img_h) = $data;
if($dsth==0) $mode=3;
if($mode==0){
$imgratio = $img_w / $img_h;
$thumbratio = $dstw / $dsth;
if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
$cuty = $img_h;
$cutx = $cuty * $thumbratio;
} elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
$cutx = $img_w;
$cuty = $cutx / $thumbratio;
}
$cx = $cutx;
$cy = $cuty;
}elseif($mode==1){
$cx = $img_w;
$cy = $img_h;
}elseif ($mode==2){
$cx = $img_w;
$cy = $img_h;
$bit=$img_w/$img_h;
if($dstw/$dsth>$bit){
$dstw=($img_w/$img_h)*$dsth;
}else{
$dsth=($img_h/$img_w)*$dstw;
}
}
elseif($mode==3){
$cx = $img_w;
$cy = $img_h;
$dsth=$dstw * $img_h / $img_w;
}
elseif ($mode==4){
$cx = $img_w;
$cy = $img_h;
$dstw=$dsth * $img_w / $img_h;
}
$ni=imagecreatetruecolor($dstw,$dsth);
ImageCopyResampled($ni,$im,0,0,0,0,$dstw,$dsth, $cx, $cy);
clearstatcache();
if($data[2]==2) ImageJPEG($ni,$dstfile,100);
elseif($data[2]==1) ImageGif($ni,$dstfile);
elseif($data[2]==3) ImagePNG($ni,$dstfile);
return true;
}
?>
此外,为了进一步增强网站的功能性和用户交互体验,推荐使用EyouCms,这是一个简单易用的企业网站管理系统,能够帮助您更高效地管理网站内容。
相关文章
-
在Discuz论坛系统中,帖子内容页面的导航功能(上一篇和下一篇)对于提升用户体验至关重要。这些功能可以让用户更方便地浏览相关内容,而无需返回列表页重新选择。以下是实现“上一篇”和“下一篇”功能的具体代码及其优化后的SEO文章。Discuz帖子内容页面中的导航功能可以通过简单的HTML代码实现,...2025-03-29
-
在Discuz论坛系统中,为内容页面添加打印按钮是一项实用的功能,它可以让用户轻松打印出主题内容。以下是一篇关于如何实现这一功能的SEO优化文章。 在构建基于Discuz的主题页面时,增加一个打印按钮可以极大地提升用户体验。这不仅方便了那些希望保存内容以备后用的用户,还可能间接提高网站的访问量和停...2025-03-29
-
Discuz字符串截取函数 `messagecutstr()` 详解
在进行Discuz二次开发时,我们常常需要对内容进行简介处理。此时,可以利用Discuz自带的内容处理函数 `messagecutstr` 来实现这一需求。本文将详细介绍该函数的用法及参数说明,并通过实例演示其具体操作。 ### 函数定义与参数解释 `messagecutstr` 是一个用于...2025-03-29 -
在网站开发和功能扩展的过程中,实现回帖邮件通知功能可以有效提升用户体验。以下是实现该功能的详细步骤,包括代码修改的具体位置和方法。打开目录:sourceincludepost找到文件:post_newreply.php在该文件中,找到以下代码段: 复制代码 if...2025-03-29