【即将关站通知●已关闭注册功能】,倒计时剩余:计算中...

[discuz建站技术] discuz x3.2 提取帖子第一张图片生成缩略图代码

[复制链接]
魔趣吧站长魔趣吧官方成员 实名认证 发表于 2019-6-10 11:51:52 | 显示全部楼层 |阅读模式
在已获得帖子图片附件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
  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. ?>
复制代码

4 O9 P; c7 @' Z' J

魔趣吧版权声明1,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2,本文内容仅代表作者本人观点,不代表本网站立场,作者文责自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3,本文内容,包括但不限于源码、文字、图片等,仅供参考使用,本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容
4,如您认为本文内容侵犯了您的权益,请与我们联系!我们将在5个工作日内做出处理!本站保留全部修改、解释、更新本声明的权利魔趣吧

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线支持
关闭
欢迎来到魔趣吧
如果你有建站技术问题、仿站需求、模板插件修改需求,请联系我们的技术支持!高效率,低价格的帮你搞定!
请认准魔趣吧唯一官网:www.moqu8.com,谨防假冒网站!

一般问题请到【求助区】发帖咨询(免费)!

邮箱咨询:
winkill2012@qq.com

邮箱咨询需注明:资源地址+本站用户名+具体问题,三者缺一不做处理。非本站资源不处理!
在线支持
快速回复 返回列表

关于魔趣吧|魔趣建站

魔趣吧,志在打造全国最大的建站资源共享平台。 本站提供海量免费的建站资源,包括WordPress模板、Discuz精仿模板、PHPWind模板、phpcms模板、Discuz精仿插件、帝国cms模板、织梦模板等上万精品模板!同时,提供各种建站素材、JS特效、整站程序源码!
服务中文站长,传播分享精神!建站,就来魔趣吧!!
2015-2024 魔趣吧 ( 豫ICP备2021024354号 )   豫公网安备41030402000182号 百度统计
HTTPS安全认证
|网站地图 | 已运行:天 
温馨提示:本站所有资源、教程、源码,不得用于非法站点及用途,否则,本站将终止提供任何服务! Powered by Discuz! Licensed