找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 234|回复: 0

通过ucenter给discuz批量自动注册马甲带头像

[复制链接]
发表于 2018-8-10 20:11:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
通过ucenter给discuz批量注册马甲带头像,因为要做个测试。需要大量的马甲。上网找了几个discuz的注册马甲软件,发现都不好用,太麻烦了。没办法,只好自己写个脚本来搞10W多的马甲自己玩!

代码在如下

  1. <?php
  2. //头像放置文件avatar.txt,用户名放置文件users.txt.一行一个有效数据
  3. //定义马甲的相关信息!1.ip注册IP,2.用户密码,3.用户email,UCD.ucenter的头像文件夹。要求可读写权限,
  4. //4.NUMBER是添加数量。如果文件中用户名少于定义的数量。将以实际为准
  5. /*
  6. * ps.如果数据库已经存在用户,则跳过
  7. * */
  8. define ( "IP", '127.0.0.1' );
  9. define ( "PASSWORD", 'password' );
  10. define ( "EMAIL", 'admin@admin.com' );
  11. define ( 'UPRE', 'uc_' );
  12. define ( "DPRE", 'pre_' );
  13. define("UCD",'/home/wwwroot/ucenter/data/avatar/');
  14. define('NUMBER', 15000);
  15. /**
  16. * 暂时用于shell中运行*
  17. */
  18. //ucenter的数据库配置
  19. $udbserver = 'localhost'; // 此处改成数据库服务器地址
  20. $udbuser = 'root'; // 此处写数据库用户名
  21. $udbpwd = 'root'; // 数据库密码
  22. $udbname = 'ucenter'; // 数据库名称
  23. $ucharset = 'utf8'; // 此处写字符集gbk或者utf8

  24. //discuz的数据库配置
  25. $ddbserver = 'localhost'; // 此处改成数据库服务器地址
  26. $ddbuser = 'root'; // 此处写数据库用户名
  27. $ddbpwd = 'root'; // 数据库密码
  28. $ddbname = 'ultrax'; // 数据库名称
  29. $dcharset = 'utf8'; // 此处写字符集gbk或者utf8
  30.                   
  31. // 连接ucenter
  32. $udb = new PDO ( 'mysql:host=' . $udbserver . ';dbname=' . $udbname, $udbuser, $udbpwd );
  33. if (! $udb)
  34.     exit ( "ucenter can't connet db" );
  35. $udb->exec ( 'set NAMES ' . $ucharset );
  36. // 连接discuz
  37. $ddb = new PDO ( 'mysql:host=' . $ddbserver . ';dbname=' . $ddbname, $ddbuser, $ddbpwd );
  38. if (! $udb)
  39.     exit ( "discuz can't connet udb" );
  40. $ddb->exec ( 'set NAMES ' . $dcharset );
  41. if (! $ddb)
  42.     exit ( "can't connet ddb" );
  43.    
  44.     // 实例化图片类
  45. $image = new Image ();

  46. $_ENV ['udb'] = $udb;
  47. $_ENV ['ddb'] = $ddb;
  48. $_ENV ['image'] = $image;

  49. global $avatdata;

  50. //生产注册时间
  51. for($i = 0; $i < 100010; $i ++) {
  52.     $times [] = $time = strtotime ( '2012-02-01 00:00:00' ) + mt_rand ( 10000, 30000000 );
  53. }
  54. sort($times);
  55. //获取用户名列表
  56. $filedata = file_get_contents ( 'users.txt' );
  57. //用户列表数据
  58. $datas = explode ( "\r\n", $filedata );

  59. //开始添加
  60. uc_adduser ( $datas, $times);



  61. // UCENTER添加用户
  62. function uc_adduser($data, $times) {
  63.     $dnumber = count($data);
  64.     if ($dnumber<NUMBER){
  65.         define('NUMBER', $dnumber);
  66.     }
  67.     $rsth = $_ENV ['udb']->prepare ( "INSERT INTO " . UPRE . "members SET username=?, password=?, email='" . EMAIL . "', regip='" . IP . "', regdate=?, salt=?" );
  68.     $rfth = $_ENV ['udb']->prepare ( "INSERT INTO " . UPRE . "memberfields SET uid=?" );
  69.     foreach ( $data as $key => $name ) {
  70.         $salt = substr ( uniqid ( rand () ), - 6 );
  71.         $password = md5 ( md5 ( PASSWORD ) . $salt );
  72.          $result =$rsth->execute ( array (
  73.                 addslashes(trim($name)),
  74.                 $password,
  75.                 $times [$key],
  76.                 $salt
  77.         ) );
  78.          
  79.         $uid = $_ENV ['udb']->lastInsertId ();
  80.         if ($uid){
  81.             $rfth->execute ( array (
  82.                     $uid
  83.             ) );
  84.             //激活论坛
  85.             ati_dz($uid,$name,$times [$key]);
  86.             //添加头像
  87.             uc_avatar($uid);
  88.             echo $name."添加成功\n";
  89.         }else{
  90.             continue;
  91.         }
  92.         
  93.         //添加数量
  94.         if ($key > NUMBER) {
  95.             echo "用户导入完成!";
  96.             break;
  97.         }
  98.     }
  99. }

  100. // 添加头像
  101. function uc_avatar($uid) {
  102.     //头像数据
  103.     global $avatdata;
  104.     $url = "http://image.xxx.com/img/";
  105.     if (empty($avatdata)){
  106.         $avart = file_get_contents ( 'avatar.txt' );
  107.         $avatdata = explode ( "\r\n", $avart );
  108.     }
  109.     $pic = array_pop($avatdata);
  110.     $bigavatarfile = UCD.get_avatar($uid, 'big');
  111.     $middleavatarfile = UCD.get_avatar($uid, 'middle');
  112.     $smallavatarfile = UCD.get_avatar($uid, 'small');
  113.    
  114.     $path = dirname($bigavatarfile);
  115.     if (! file_exists ( $path )) {
  116.         mkdir ( $path, 0700, true );
  117.     }
  118.     $originpath = UCD.$pic;
  119.     if(downImg($url.$pic, $originpath)){
  120.         $_ENV['image']->bthumb($originpath,200,200,1,true,$bigavatarfile);
  121.         $_ENV['image']->bthumb($originpath,120,120,1,true,$middleavatarfile);
  122.         $_ENV['image']->bthumb($originpath,48,48,1,true,$smallavatarfile);
  123.     }
  124. }

  125. // 激活论坛
  126. function ati_dz($uid,$username,$time) {
  127.     $password = md5(random(10));
  128.     $_ENV['ddb']->exec( "INSERT INTO `" . DPRE . "common_member` (uid,username,password,adminid,groupid,regdate,email,emailstatus,avatarstatus) VALUES ('$uid','$username', '$password','0','10','$time','".EMAIL."',1,1)");
  129.     $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_field_forum` (uid) VALUES ('$uid')");
  130.     $_ENV['ddb']->exec(" replace INTO    `" . DPRE . "common_member_field_home` (uid) VALUES ('$uid')");
  131.     $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_count` (uid) VALUES ('$uid')");
  132.     $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_profile` (uid) VALUES ('$uid')");
  133.     $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_status` (uid,regip,lastvisit,lastactivity) VALUES ('$uid','127.0.0.1','$time','$time')");
  134. }

  135. /**
  136. * 自动创建目录并返回文件名
  137. *   
  138. */
  139. function mkfilename($img, $time) {
  140.     $info = pathinfo ( $img );
  141.     $type = strtolower ( $info ['extension'] );
  142.     $path = './datas/uploadimage/images/';
  143.     $time = strtotime ( $time );
  144.     $filepath = $path . date ( "Y-m", $time ) . '/' . date ( "d", $time ) . '/' . time () . mt_rand ( 10000, 99999 ) . "." . $type;
  145.     $path = dirname ( $filepath );
  146.     if (! file_exists ( $path )) {
  147.         mkdir ( $path, 0777, true );
  148.     }
  149.     return $filepath;
  150. }

  151. /**
  152. * 下载文件
  153. *   
  154. */
  155. function downImg($url, $filename) {
  156.     $ch = curl_init ( $url );
  157.     $fp = fopen ( $filename, 'wb+' );
  158.     curl_setopt ( $ch, CURLOPT_FILE, $fp );
  159.     curl_setopt ( $ch, CURLOPT_HEADER, 0 );
  160.     curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
  161.     curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
  162.     curl_exec ( $ch );
  163.     $status = curl_getinfo ( $ch );
  164.     curl_close ( $ch );
  165.     fclose ( $fp );
  166.     if ($status ['http_code'] != 200) {
  167.         @unlink ( $filename );
  168.         return false;
  169.     } else {
  170.         return true;
  171.     }
  172. }

  173. function get_avatar($uid, $size = 'big', $type = '') {
  174.     $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'big';
  175.     $uid = abs(intval($uid));
  176.     $uid = sprintf("%09d", $uid);
  177.     $dir1 = substr($uid, 0, 3);
  178.     $dir2 = substr($uid, 3, 2);
  179.     $dir3 = substr($uid, 5, 2);
  180.     $typeadd = $type == 'real' ? '_real' : '';
  181.     return  $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
  182. }


  183. function random($length) {
  184.     $hash = '';
  185.     $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  186.     $max = strlen($chars) - 1;
  187.     PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  188.     for($i = 0; $i < $length; $i++) {
  189.         $hash .= $chars[mt_rand(0, $max)];
  190.     }
  191.     return $hash;
  192. }

  193. /**
  194. * 日志记录
  195. *   
  196. */
  197. function mlog($msg) {
  198.     $file = "userIDs.text";
  199.     $handle = fopen ( $file, 'a+' );
  200.     fwrite ( $handle, $msg . "\n" );
  201.     fclose ( $handle );
  202. }


  203. class Image {
  204.     /**
  205.      * 取得图像信息
  206.      *
  207.      * @static
  208.      *
  209.      *
  210.      * @access public
  211.      * @param string $image
  212.      *            图像文件名
  213.      * @return mixed
  214.      */
  215.     static function getImageInfo($img) {
  216.         $imageInfo = getimagesize ( $img );
  217.         if ($imageInfo !== false) {
  218.             $imageType = strtolower ( substr ( image_type_to_extension ( $imageInfo [2] ), 1 ) );
  219.             $imageSize = filesize ( $img );
  220.             $info = array (
  221.                     "width" => $imageInfo [0],
  222.                     "height" => $imageInfo [1],
  223.                     "type" => $imageType,
  224.                     "size" => $imageSize,
  225.                     "mime" => $imageInfo ['mime']
  226.             );
  227.             return $info;
  228.         } else {
  229.             return false;
  230.         }
  231.     }
  232.    
  233.     /**
  234.      * 为图片添加水印
  235.      *
  236.      * @static public
  237.      * @param string $source
  238.      *            原文件名
  239.      * @param string $water
  240.      *            水印图片
  241.      * @param string $savename
  242.      *            添加水印后的图片名
  243.      * @param string $alpha
  244.      *            水印的透明度
  245.      * @return void
  246.      */
  247.     static public function water($source, $water, $savename = null, $alpha = 80) {
  248.         // 检查文件是否存在
  249.         if (! file_exists ( $source ) || ! file_exists ( $water ))
  250.             return false;
  251.             
  252.             // 图片信息
  253.         $sInfo = self::getImageInfo ( $source );
  254.         $wInfo = self::getImageInfo ( $water );
  255.         
  256.         // 如果图片小于水印图片,不生成图片
  257.         if ($sInfo ["width"] < $wInfo ["width"] || $sInfo ['height'] < $wInfo ['height'])
  258.             return false;
  259.             
  260.             // 建立图像
  261.         $sCreateFun = "imagecreatefrom" . $sInfo ['type'];
  262.         if (! function_exists ( $sCreateFun ))
  263.             $sCreateFun = 'imagecreatefromjpeg';
  264.         $sImage = $sCreateFun ( $source );
  265.         $wCreateFun = "imagecreatefrom" . $wInfo ['type'];
  266.         if (! function_exists ( $wCreateFun ))
  267.             $wCreateFun = 'imagecreatefromjpeg';
  268.         $wImage = $wCreateFun ( $water );
  269.         
  270.         // 设定图像的混色模式
  271.         imagealphablending ( $wImage, true );
  272.         
  273.         // 图像位置,默认为右下角右对齐
  274.         $posY = $sInfo ["height"] - $wInfo ["height"] - 4;
  275.         $posX = $sInfo ["width"] - $wInfo ["width"] - 4;
  276.         
  277.         // 生成混合图像
  278.         imagecopymerge ( $sImage, $wImage, $posX, $posY, 0, 0, $wInfo ['width'], $wInfo ['height'], $alpha );
  279.         
  280.         // 输出图像
  281.         $ImageFun = 'Image' . $sInfo ['type'];
  282.         if (! function_exists ( $ImageFun ))
  283.             $ImageFun = 'imagecreatefromjpeg';
  284.             // 如果没有给出保存文件名,默认为原图像名
  285.         if (! $savename) {
  286.             $savename = $source;
  287.             @unlink ( $source );
  288.         }
  289.         // 保存图像
  290.         $ImageFun ( $sImage, $savename );
  291.         imagedestroy ( $sImage );
  292.     }
  293.     /**
  294.      * 为图片添加水印
  295.      *
  296.      * @static public
  297.      * @param string $source
  298.      *            原文件名
  299.      * @param string $water
  300.      *            水印图片
  301.      * @param string $savename
  302.      *            添加水印后的图片名
  303.      * @param string $alpha
  304.      *            水印的透明度
  305.      * @return void
  306.      */
  307.     static public function water2($source, $water, $savename = null, $alpha = 75) {
  308.         // 检查文件是否存在
  309.         if (! file_exists ( $source ) || ! file_exists ( $water ))
  310.             return false;
  311.             
  312.             // 图片信息
  313.         $sInfo = self::getImageInfo ( $source );
  314.         $wInfo = self::getImageInfo ( $water );
  315.         
  316.         // 如果图片小于水印图片,不生成图片
  317.         if ($sInfo ["width"] < $wInfo ["width"] || $sInfo ['height'] < $wInfo ['height'])
  318.             return false;
  319.             
  320.             // 建立图像
  321.         $sCreateFun = "imagecreatefrom" . $sInfo ['type'];
  322.         if (! function_exists ( $sCreateFun ))
  323.             $sCreateFun = 'imagecreatefromjpeg';
  324.         $sImage = $sCreateFun ( $source );
  325.         $wCreateFun = "imagecreatefrom" . $wInfo ['type'];
  326.         if (! function_exists ( $wCreateFun ))
  327.             $wCreateFun = 'imagecreatefromjpeg';
  328.         $wImage = $wCreateFun ( $water );
  329.         
  330.         // 设定图像的混色模式
  331.         imagealphablending ( $wImage, true );
  332.         
  333.         // 图像位置,默认为右下角右对齐
  334.         $posY = $sInfo ["height"] - $wInfo ["height"] - 18;
  335.         $posX = $sInfo ["width"] - $wInfo ["width"] - 18;
  336.         
  337.         // 生成混合图像
  338.         imagecopymerge ( $sImage, $wImage, $posX, $posY, 0, 0, $wInfo ['width'], $wInfo ['height'], $alpha );
  339.         
  340.         // 输出图像
  341.         $ImageFun = 'Image' . $sInfo ['type'];
  342.         if (! function_exists ( $ImageFun ))
  343.             $ImageFun = 'imagecreatefromjpeg';
  344.             // 如果没有给出保存文件名,默认为原图像名
  345.         if (! $savename) {
  346.             $savename = $source;
  347.             @unlink ( $source );
  348.         }
  349.         // 保存图像
  350.         $ImageFun ( $sImage, $savename );
  351.         imagedestroy ( $sImage );
  352.     }
  353.     function showImg($imgFile, $text = '', $x = '10', $y = '10', $alpha = '50') {
  354.         // 获取图像文件信息
  355.         // 2007/6/26 增加图片水印输出,$text为图片的完整路径即可
  356.         $info = Image::getImageInfo ( $imgFile );
  357.         if ($info !== false) {
  358.             $createFun = str_replace ( '/', 'createfrom', $info ['mime'] );
  359.             $im = $createFun ( $imgFile );
  360.             if ($im) {
  361.                 $ImageFun = str_replace ( '/', '', $info ['mime'] );
  362.                 // 水印开始
  363.                 if (! empty ( $text )) {
  364.                     $tc = imagecolorallocate ( $im, 0, 0, 0 );
  365.                     if (is_file ( $text ) && file_exists ( $text )) { // 判断$text是否是图片路径
  366.                                                                       // 取得水印信息
  367.                         $textInfo = Image::getImageInfo ( $text );
  368.                         $createFun2 = str_replace ( '/', 'createfrom', $textInfo ['mime'] );
  369.                         $waterMark = $createFun2 ( $text );
  370.                         // $waterMark=imagecolorallocatealpha($text,255,255,0,50);
  371.                         $imgW = $info ["width"];
  372.                         $imgH = $info ["width"] * $textInfo ["height"] / $textInfo ["width"];
  373.                         // $y = ($info["height"]-$textInfo["height"])/2;
  374.                         // 设置水印的显示位置和透明度支持各种图片格式
  375.                         imagecopymerge ( $im, $waterMark, $x, $y, 0, 0, $textInfo ['width'], $textInfo ['height'], $alpha );
  376.                     } else {
  377.                         imagestring ( $im, 80, $x, $y, $text, $tc );
  378.                     }
  379.                     // ImageDestroy($tc);
  380.                 }
  381.                 // 水印结束
  382.                 if ($info ['type'] == 'png' || $info ['type'] == 'gif') {
  383.                     imagealphablending ( $im, FALSE ); // 取消默认的混色模式
  384.                     imagesavealpha ( $im, TRUE ); // 设定保存完整的 alpha 通道信息
  385.                 }
  386.                 Header ( "Content-type: " . $info ['mime'] );
  387.                 $ImageFun ( $im );
  388.                 @ImageDestroy ( $im );
  389.                 return;
  390.             }
  391.             
  392.             // 保存图像
  393.             $ImageFun ( $sImage, $savename );
  394.             imagedestroy ( $sImage );
  395.             // 获取或者创建图像文件失败则生成空白PNG图片
  396.             $im = imagecreatetruecolor ( 80, 30 );
  397.             $bgc = imagecolorallocate ( $im, 255, 255, 255 );
  398.             $tc = imagecolorallocate ( $im, 0, 0, 0 );
  399.             imagefilledrectangle ( $im, 0, 0, 150, 30, $bgc );
  400.             imagestring ( $im, 4, 5, 5, "no pic", $tc );
  401.             Image::output ( $im );
  402.             return;
  403.         }
  404.     }
  405.     public function bthumb($image, $maxWidth = 200, $maxHeight = 50, $gen = 0, $interlace = true, $filepath = '') {
  406.         $info = Image::getImageInfo ( $image );
  407.         if ($info !== false) {
  408.             $srcWidth = $info ['width'];
  409.             $srcHeight = $info ['height'];
  410.             $type = $info ['type'];
  411.             
  412.             $interlace = $interlace ? 1 : 0;
  413.             unset ( $info );
  414.             
  415.             if ($maxWidth > 0 && $maxHeight > 0) {
  416.                 // $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); //
  417.                 // 计算缩放比例
  418.                 // 改为以宽度缩放
  419.                 $scale = $maxWidth / $srcWidth;
  420.             } elseif ($maxWidth == 0)
  421.                 $scale = $maxHeight / $srcHeight;
  422.             elseif ($maxHeight == 0)
  423.                 $scale = $maxWidth / $srcWidth;
  424.             
  425.             if ($scale >= 1) {
  426.                 // 超过原图大小不再缩略
  427.                 $width = $srcWidth;
  428.                 $height = $srcHeight;
  429.             } else {
  430.                 // 缩略图尺寸
  431.                 $width = ( int ) ($srcWidth * $scale);
  432.                 $height = ( int ) ($srcHeight * $scale);
  433.             }
  434.             
  435.             if ($gen == 1) {
  436.                 $width = $maxWidth;
  437.                 $height = $maxHeight;
  438.             }
  439.             
  440.             $paths = pathinfo ( $image );
  441.             $ext = Image::fileExt ( $image );
  442.             
  443.             if (empty ( $filepath ))
  444.                 $thumbname = str_replace ( '.' . $paths ['extension'], '', $image ) . '_' . $maxWidth . 'x' . $maxHeight . '.' . $ext;
  445.             else
  446.                 $thumbname = $filepath;
  447.                
  448.                 // 载入原图
  449.             $createFun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
  450.             if (! function_exists ( $createFun ))
  451.                 $createFun = 'imagecreatefromjpeg';
  452.             
  453.             $srcImg = $createFun ( $image );
  454.             
  455.             // 创建缩略图
  456.             if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' ))
  457.                 $thumbImg = imagecreatetruecolor ( $width, $height );
  458.             else
  459.                 $thumbImg = imagecreate ( $width, $height );
  460.             
  461.             $x = 0;
  462.             $y = 0;
  463.             
  464.             if ($gen == 1 && $maxWidth > 0 && $maxHeight > 0) {
  465.                 $resize_ratio = $maxWidth / $maxHeight;
  466.                 $src_ratio = $srcWidth / $srcHeight;
  467.                 if ($src_ratio >= $resize_ratio) {
  468.                     $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
  469.                     $width = ($height * $srcWidth) / $srcHeight;
  470.                 } else {
  471.                     $y = ($srcHeight - ((1 / $resize_ratio) * $srcWidth)) / 2;
  472.                     $height = ($width * $srcHeight) / $srcWidth;
  473.                 }
  474.             }
  475.             $x = 0;
  476.             $y = 0;
  477.             
  478.             // 复制图片
  479.             if (function_exists ( "imagecopyresampled" ))
  480.                 imagecopyresampled ( $thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight );
  481.             else
  482.                 imagecopyresized ( $thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight );
  483.             
  484.             if ('gif' == $type || 'png' == $type) {
  485.                 $background_color = imagecolorallocate ( $thumbImg, 0, 0, 0 ); // 指派一个绿色
  486.                 imagecolortransparent ( $thumbImg, $background_color ); // 设置为透明色,若注释掉该行则输出绿色的图
  487.             }
  488.             
  489.             // 对jpeg图形设置隔行扫描
  490.             if ('jpg' == $type || 'jpeg' == $type)
  491.                 imageinterlace ( $thumbImg, $interlace );
  492.                
  493.                 // 生成图片
  494.             if (function_exists ( 'imagefilter' ))
  495.                 imagefilter ( $thumbImg, IMG_FILTER_CONTRAST, - 1 );
  496.                
  497.                 // 保存图片
  498.             imagejpeg ( $thumbImg, $thumbname, 100 );
  499.             imagedestroy ( $thumbImg );
  500.             imagedestroy ( $srcImg );
  501.             return $thumbname;
  502.         }
  503.         return false;
  504.     }
  505.    
  506.     /**
  507.      * 生成缩略图
  508.      *
  509.      * @static
  510.      *
  511.      *
  512.      *
  513.      *
  514.      *
  515.      *
  516.      *
  517.      *
  518.      *
  519.      * @access public
  520.      * @param string $image
  521.      *            原图
  522.      * @param string $type
  523.      *            图像格式
  524.      * @param string $thumbname
  525.      *            缩略图文件名
  526.      * @param string $maxWidth
  527.      *            宽度
  528.      * @param string $maxHeight
  529.      *            高度
  530.      * @param string $position
  531.      *            缩略图保存目录
  532.      * @param boolean $interlace
  533.      *            启用隔行扫描
  534.      * @return void
  535.      */
  536.     static function thumb($image, $thumbname, $type = '', $maxWidth = 200, $maxHeight = 50, $interlace = true) {
  537.         // 获取原图信息
  538.         $info = Image::getImageInfo ( $image );
  539.         if ($info !== false) {
  540.             $srcWidth = $info ['width'];
  541.             $srcHeight = $info ['height'];
  542.             $type = empty ( $type ) ? $info ['type'] : $type;
  543.             $type = strtolower ( $type );
  544.             $interlace = $interlace ? 1 : 0;
  545.             unset ( $info );
  546.             $scale = min ( $maxWidth / $srcWidth, $maxHeight / $srcHeight ); // 计算缩放比例
  547.             if ($scale >= 1) {
  548.                 // 超过原图大小不再缩略
  549.                 $width = $srcWidth;
  550.                 $height = $srcHeight;
  551.             } else {
  552.                 // 缩略图尺寸
  553.                 $width = ( int ) ($srcWidth * $scale);
  554.                 $height = ( int ) ($srcHeight * $scale);
  555.             }
  556.             
  557.             // 载入原图
  558.             $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  559.             if (! function_exists ( $createFun )) {
  560.                 return false;
  561.             }
  562.             $srcImg = $createFun ( $image );
  563.             
  564.             // 创建缩略图
  565.             if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' ))
  566.                 $thumbImg = imagecreatetruecolor ( $width, $height );
  567.             else
  568.                 $thumbImg = imagecreate ( $width, $height );
  569.                 // png和gif的透明处理 by luofei614
  570.             if ('png' == $type) {
  571.                 imagealphablending ( $thumbImg, false ); // 取消默认的混色模式(为解决阴影为绿色的问题)
  572.                 imagesavealpha ( $thumbImg, true ); // 设定保存完整的 alpha
  573.                                                         // 通道信息(为解决阴影为绿色的问题)
  574.             } elseif ('gif' == $type) {
  575.                 $trnprt_indx = imagecolortransparent ( $srcImg );
  576.                 if ($trnprt_indx >= 0) {
  577.                     // its transparent
  578.                     $trnprt_color = imagecolorsforindex ( $srcImg, $trnprt_indx );
  579.                     $trnprt_indx = imagecolorallocate ( $thumbImg, $trnprt_color ['red'], $trnprt_color ['green'], $trnprt_color ['blue'] );
  580.                     imagefill ( $thumbImg, 0, 0, $trnprt_indx );
  581.                     imagecolortransparent ( $thumbImg, $trnprt_indx );
  582.                 }
  583.             }
  584.             // 复制图片
  585.             if (function_exists ( "ImageCopyResampled" ))
  586.                 imagecopyresampled ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
  587.             else
  588.                 imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
  589.                
  590.                 // 对jpeg图形设置隔行扫描
  591.             if ('jpg' == $type || 'jpeg' == $type)
  592.                 imageinterlace ( $thumbImg, $interlace );
  593.                
  594.                 // 生成图片
  595.             $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  596.             $imageFun ( $thumbImg, $thumbname );
  597.             imagedestroy ( $thumbImg );
  598.             imagedestroy ( $srcImg );
  599.             return $thumbname;
  600.         }
  601.         return false;
  602.     }
  603.     static function output($im, $type = 'png', $filename = '') {
  604.         header ( "Content-type: image/" . $type );
  605.         $ImageFun = 'image' . $type;
  606.         if (empty ( $filename )) {
  607.             $ImageFun ( $im );
  608.         } else {
  609.             $ImageFun ( $im, $filename );
  610.         }
  611.         imagedestroy ( $im );
  612.     }
  613.    
  614.     /**
  615.      * 获取文件扩展名
  616.      *
  617.      * @return string
  618.      */
  619.     function fileExt($file_name) {
  620.         return addslashes ( strtolower ( substr ( strrchr ( $file_name, '.' ), 1, 10 ) ) );
  621.     }
  622. }
  623. ?>
复制代码
代码只是初级代码,本地测试已经可以使用。里面可能有bug,欢迎提出或自行修改。

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

本版积分规则

返回顶部快速回复上一主题下一主题返回列表找客服手机访问

QQ|申请友链|Archiver|小黑屋|手机版|网站地图|谷姐论坛 谷姐提供 ( 渝ICP备2021009247号-1 )

GMT+8, 2024-4-27 12:02

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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