[discuz建站技术]
discuz x3.2 提取帖子第一张图片生成缩略图代码
[复制链接]
在已获得帖子图片附件aid的情况下可以直接使用 * ]9 N2 `0 j" N C" J1 F
<!--{eval $imagelistkey = getforumimg($thecover[aid], 0, 225, 0); }--> ) H. t9 d% s R6 Y
这个是生成到data/attachment目录。 9 ~2 H; I: z4 o1 @- T
! q, m0 t1 J8 _ u
或者另一种,在只有帖子tid的情况下获得帖子缩略图,单独创建aidpic.php文件放到根目录,在使用时缩略图地址为aidpic.php?aid=帖子tid&宽x高,如<im g sr c="aidpic.php?aid=24575&size=150x100"/> * S" K* k' q9 B3 f1 t1 p4 k
生成到自动创建data/aidpic。 9 ]1 v7 }3 S1 ?2 V; Y" L3 J) M
<?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; } /* $w=100;//宽度 $h=75;//高度 $m=0;//缩略图模式 //mode=0为固定宽高,画质裁切不变形 //mode=1为固定宽高,画质会拉伸变形 //mode=2为可变宽高,宽高不超过指定大小 //mode=3为固定宽度,高度随比例变化 */ $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; } ?> 复制代码
4 O9 P; c7 @' Z' J
魔趣吧版权声明 1,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2,本文内容仅代表作者本人观点,不代表本网站立场,作者文责自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3,本文内容,包括但不限于源码、文字、图片等,仅供参考使用,本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容
4,如您认为本文内容侵犯了您的权益,请与我们联系 !我们将在5个工作日内做出处理!本站保留全部修改、解释、更新本声明的权利魔趣吧