discuz x3.2 提取帖子第一张图片生成缩略图代码-Discuz教程下载

discuz x3.2 提取帖子第一张图片生成缩略图代码

开通本站Svip会员,全站资源免费下
在已获得帖子图片附件aid的情况下可以直接使用
  1. <!--{eval $imagelistkey = getforumimg($thecover[aid], 0, 225, 0); }-->
复制代码
这个是生成到data/attachment目录。

或者另一种,在只有帖子tid的情况下获得帖子缩略图,单独创建aidpic.php文件放到根目录,在使用时缩略图地址为aidpic.php?aid=帖子tid&宽x高,如
  1. <img src="aidpic.php?aid=24575&size=150x100"/>
复制代码
生成到自动创建data/aidpic。
  1. <?php
  2. require_once './source/class/class_core.php';
  3. $Discuz = & discuz_core::instance();
  4. $discuz->init();

  5. list($w,$h)=explode("x",$_G['gp_size']);
  6. $m=0;
  7. if($w==0&&$h==0){
  8.         $m=5;}elseif ($h==0){
  9.         $m=3;}elseif ($w==0){
  10.         $m=4;}
  11.         /*$w=100;//宽度$h=75;//高度$m=0;//缩略图模式
  12.         //mode=0为固定宽高,画质裁切不变形
  13.         //mode=1为固定宽高,画质会拉伸变形
  14.         //mode=2为可变宽高,宽高不超过指定大小
  15.         //mode=3为固定宽度,高度随比例变化*/
  16. $nopic='./static/image/common/nophotosmall.gif';
  17. //缺省图片$aid=intval($_G['gp_aid']);
  18. $dir="data/aidpic/";
  19. $subdir=$dir."/{$w}x{$h}x{$m}/";
  20. $thumbfile=$subdir."/".$aid.".jpg";
  21. if(file_exists($thumbfile)){
  22.         header("location:{$thumbfile}");
  23.         die();
  24. }
  25. $tableid=substr($aid,-1,1);
  26. $attach=DB::fetch_first("SELECT a.tid,a.attachment,a.remoteFROM ".DB::table("forum_attachment_{$tableid}")." aWHERE a.`tid` ='$aid'AND a.`isimage`<>0order by a.aid asclimit 0,1");
  27. if($attach){
  28.         $attachurl=$attach['remote']?$_G['setting']['ftp']['attachurl']:$_G['setting']['attachurl'];
  29.         $attachfile=$attachurl."/forum/".$attach['attachment'];
  30.         if(!is_dir($dir)) @mkdir($dir);
  31.         if(!is_dir($subdir)) @mkdir($subdir);
  32.         dzthumb($attachfile,$thumbfile,$w,$h,$m);
  33.         header("location:{$thumbfile}");
  34.         die();}else{
  35.         header("location:$nopic");
  36.         die();}function dzthumb($srcfile,$dstfile,$dstw,$dsth=0,$mode=0,$data=''){
  37.         $data=$data==''?@GetImageSize($srcfile):$data;
  38.         if(!$data) return false;
  39.         if($data[2]==2) $im=@ImageCreateFromJPEG($srcfile);
  40.         elseif ($data[2]==1) $im=@ImageCreateFromGIF($srcfile);
  41.         elseif($data[2]==3) $im=@ImageCreateFromPNG($srcfile);
  42.         list($img_w, $img_h) = $data;
  43.         if($dsth==0) $mode=3;
  44.         if($mode==0){
  45.                 $imgratio = $img_w / $img_h;
  46.                 $thumbratio = $dstw / $dsth;
  47.                 if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
  48.                         $cuty = $img_h;
  49.                         $cutx = $cuty * $thumbratio;
  50.                 } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
  51.                         $cutx = $img_w;
  52.                         $cuty = $cutx / $thumbratio;
  53.                 }
  54.                 $cx = $cutx;
  55.                 $cy = $cuty;
  56.         }elseif($mode==1){
  57.                 $cx = $img_w;
  58.                 $cy = $img_h;
  59.         }elseif ($mode==2){
  60.                 $cx = $img_w;
  61.                 $cy = $img_h;
  62.                 $bit=$img_w/$img_h;
  63.                 if($dstw/$dsth>$bit){
  64.                         $dstw=($img_w/$img_h)*$dsth;
  65.                 }else{
  66.                         $dsth=($img_h/$img_w)*$dstw;
  67.                 }
  68.         }
  69.         elseif($mode==3){
  70.                 $cx = $img_w;
  71.                 $cy = $img_h;
  72.                 $dsth=$dstw * $img_h / $img_w;
  73.         }
  74.         elseif ($mode==4){
  75.                 $cx = $img_w;
  76.                 $cy = $img_h;
  77.                 $dstw=$dsth * $img_w / $img_h;
  78.         }
  79.         $ni=imagecreatetruecolor($dstw,$dsth);
  80.         ImageCopyResampled($ni,$im,0,0,0,0,$dstw,$dsth, $cx, $cy);
  81.         clearstatcache();
  82.         if($data[2]==2) ImageJPEG($ni,$dstfile,100);
  83.         elseif($data[2]==1) ImageGif($ni,$dstfile);
  84.         elseif($data[2]==3) ImagePNG($ni,$dstfile);
  85.         return true;}
  86. ?>
复制代码

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部