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

复制帖子链接

发贴时间:2021-05-18 16:25 

来自版块:茶馆

·

[技术交流]

人气1283 评论1 点赞0

在已获得帖子图片附件aid的情况下可以直接使用 <!--{eval $imagelistkey = getforumimg($thecover[aid], 0, 225, 0); }-->


这个是生成到data/attachment目录。


或者另一种,在只有帖子tid的情况下获得帖子缩略图,单独创建aidpic.php文件放到根目录


在使用时缩略图地址为aidpic.php?aid=帖子tid&宽x高


如<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;
  9. }elseif ($h==0){
  10.         $m=3;
  11. }elseif ($w==0){
  12.         $m=4;
  13. }
  14. /*
  15. $w=100;//宽度
  16. $h=75;//高度
  17. $m=0;//缩略图模式
  18.         //mode=0为固定宽高,画质裁切不变形
  19.         //mode=1为固定宽高,画质会拉伸变形
  20.         //mode=2为可变宽高,宽高不超过指定大小
  21.         //mode=3为固定宽度,高度随比例变化
  22. */

  23. $nopic='./static/image/common/nophotosmall.gif';//缺省图片

  24. $aid=intval($_G['gp_aid']);
  25. $dir="data/aidpic/";
  26. $subdir=$dir."/{$w}x{$h}x{$m}/";
  27. $thumbfile=$subdir."/".$aid.".jpg";
  28. if(file_exists($thumbfile)){
  29.         header("location:{$thumbfile}");
  30.         die();
  31. }
  32. $tableid=substr($aid,-1,1);
  33. $attach=DB::fetch_first("SELECT a.tid,a.attachment,a.remote
  34. FROM ".DB::table("forum_attachment_{$tableid}")." a
  35. WHERE a.`tid` ='$aid'
  36. AND a.`isimage`<>0
  37. order by a.aid asc
  38. limit 0,1");

  39. if($attach){
  40.         $attachurl=$attach['remote']?$_G['setting']['ftp']['attachurl']:$_G['setting']['attachurl'];
  41.         $attachfile=$attachurl."/forum/".$attach['attachment'];
  42.         if(!is_dir($dir)) @mkdir($dir);
  43.         if(!is_dir($subdir)) @mkdir($subdir);
  44.         dzthumb($attachfile,$thumbfile,$w,$h,$m);
  45.         header("location:{$thumbfile}");
  46.         die();
  47. }else{
  48.         header("location:$nopic");
  49.         die();
  50. }

  51. function dzthumb($srcfile,$dstfile,$dstw,$dsth=0,$mode=0,$data=''){
  52.         $data=$data==''?@GetImageSize($srcfile):$data;
  53.         if(!$data) return false;
  54.         if($data[2]==2) $im=@ImageCreateFromJPEG($srcfile);
  55.         elseif ($data[2]==1) $im=@ImageCreateFromGIF($srcfile);
  56.         elseif($data[2]==3) $im=@ImageCreateFromPNG($srcfile);
  57.         list($img_w, $img_h) = $data;
  58.         if($dsth==0) $mode=3;
  59.         if($mode==0){
  60.                 $imgratio = $img_w / $img_h;
  61.                 $thumbratio = $dstw / $dsth;
  62.                 if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
  63.                         $cuty = $img_h;
  64.                         $cutx = $cuty * $thumbratio;
  65.                 } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
  66.                         $cutx = $img_w;
  67.                         $cuty = $cutx / $thumbratio;
  68.                 }
  69.                 $cx = $cutx;
  70.                 $cy = $cuty;
  71.         }elseif($mode==1){
  72.                 $cx = $img_w;
  73.                 $cy = $img_h;
  74.         }elseif ($mode==2){
  75.                 $cx = $img_w;
  76.                 $cy = $img_h;
  77.                 $bit=$img_w/$img_h;
  78.                 if($dstw/$dsth>$bit){
  79.                         $dstw=($img_w/$img_h)*$dsth;
  80.                 }else{
  81.                         $dsth=($img_h/$img_w)*$dstw;
  82.                 }
  83.         }
  84.         elseif($mode==3){
  85.                 $cx = $img_w;
  86.                 $cy = $img_h;
  87.                 $dsth=$dstw * $img_h / $img_w;
  88.         }
  89.         elseif ($mode==4){
  90.                 $cx = $img_w;
  91.                 $cy = $img_h;
  92.                 $dstw=$dsth * $img_w / $img_h;
  93.         }
  94.         $ni=imagecreatetruecolor($dstw,$dsth);
  95.         ImageCopyResampled($ni,$im,0,0,0,0,$dstw,$dsth, $cx, $cy);
  96.         clearstatcache();
  97.         if($data[2]==2) ImageJPEG($ni,$dstfile,100);
  98.         elseif($data[2]==1) ImageGif($ni,$dstfile);
  99.         elseif($data[2]==3) ImagePNG($ni,$dstfile);
  100.         return true;
  101. }

  102. ?>
复制代码



复制以下链接分享到 QQ群 QQ空间 贴吧 或其他CG网站上,每进来一个人你将获得: 2微豆
B Color Smilies

全部评论1


客服
快速回复 返回顶部 返回列表