- <?php
- @set_magic_quotes_runtime(0);
- @set_time_limit(0);
- define('UCENTER_URL', 'http://youdomian/Ucenter'); //UCenter URL
- define('UCENTER_ROOT', getcwd().'/Ucenter/'); //UCenter 路径
- define('DISCUZ_ROOT', getcwd().'/');
- define('IN_DISCUZ', TRUE);
- if(!file_exists(UCENTER_ROOT.'./data/avatar')) {
- echo 'UCenter 路径不存在,请修改本工具设置正确....';
- exit;
- }
- require DISCUZ_ROOT.'./include/db_mysql.class.php';
- @include DISCUZ_ROOT.'./config.inc.php';
- $db = new dbstuff();
- $db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, FALSE, $dbcharset);
- $dbuser = $dbpw = $dbname = $pconnect = NULL;
- $start = intval($_GET['start']);
- $total = intval($_GET['total']);
- $limit = 100;
- $maxuid = @file(DISCUZ_ROOT.'forumdata/upgrademaxuid.log');
- $maxuid = $maxuid[0];
- //$maxuid = 1;
- instheader();
-
- $query = $db->query("SELECT uid, avatar FROM {$tablepre}memberfields WHERE avatar!='' LIMIT $start, $limit");
- if(!$db->num_rows($query)) {
- echo "<br /><br /><br />导入完毕,共导入 $total 个头像!";
- } else {
- while($data = $db->fetch_array($query)) {
- if(preg_match_all('/^customavatars\/(\d+)\.(.+?)$/', $data['avatar'], $a)) {
- set_home($data['uid'], UCENTER_ROOT.'./data/avatar');
- $data['uid'] += $maxuid;
- $avatar = DISCUZ_ROOT.'customavatars/'.$a[1][0].'.'.$a[2][0];
- $ucavatar = UCENTER_ROOT.'./data/avatar/'.get_avatar($data['uid'], 'middle');
- if(!file_exists($ucavatar)) {
- $create = FALSE;
- $img = new Image_Lite($avatar, $ucavatar);
- if($img->imagecreatefromfunc && $img->imagefunc) {
- if($img->Thumb(120, 120)) {
- $create = TRUE;
- $total++;
- }
- }
- if($create) {
- $ucavatar = UCENTER_ROOT.'./data/avatar/'.get_avatar($data['uid'], 'small');
- $img = new Image_Lite($avatar, $ucavatar);
- if($img->imagecreatefromfunc && $img->imagefunc) {
- $img->Thumb(48, 48);
- }
- $ucavatar = UCENTER_ROOT.'./data/avatar/'.get_avatar($data['uid'], 'big');
- @copy($avatar, $ucavatar);
- echo '<img src="'.UCENTER_URL.'data/avatar/'.get_avatar($data['uid'], 'small').'" />';
- }
- }
- }
- }
- $end = $start + $limit;
- $url_forward = "upgradeavatar.php?start=$end&total=$total";
- echo "<br /><br /><br /><a href="$url_forward">已导入 $total 个头像,请等待 ...</a><script>setTimeout("redirect('$url_forward');", 1250);</script>";
- }
- instfooter();
- function instheader() {
- global $charset, $version;
- echo "<html><head>".
- "<meta http-equiv="Content-Type" content="text/html; charset=$charset">".
- "<title>Discuz! 6.0.0 > UCenter 头像导入工具</title>".
- "<style type="text/css">
- a {
- color: #3A4273;
- text-decoration: none
- }
- a:hover {
- color: #3A4273;
- text-decoration: underline
- }
- body, table, td {
- color: #3A4273;
- font-family: Tahoma, Verdana, Arial;
- font-size: 12px;
- line-height: 20px;
- scrollbar-base-color: #E3E3EA;
- scrollbar-arrow-color: #5C5C8D
- }
- input {
- color: #085878;
- font-family: Tahoma, Verdana, Arial;
- font-size: 12px;
- background-color: #3A4273;
- color: #FFFFFF;
- scrollbar-base-color: #E3E3EA;
- scrollbar-arrow-color: #5C5C8D
- }
- .install {
- font-family: Arial, Verdana;
- font-size: 20px;
- font-weight: bold;
- color: #000000
- }
- .message {
- background: #E3E3EA;
- padding: 20px;
- }
- .altbg1 {
- background: #E3E3EA;
- }
- .altbg2 {
- background: #EEEEF6;
- }
- .header td {
- color: #FFFFFF;
- background-color: #3A4273;
- text-align: center;
- }
- .option td {
- text-align: center;
- }
- .redfont {
- color: #FF0000;
- }
- </style>
- <script type="text/javascript">
- function redirect(url) {
- window.location=url;
- }
- function $(id) {
- return document.getElementById(id);
- }
- </script>
- </head>".
- "<body bgcolor="#3A4273" text="#000000"><div id="append_parent"></div>".
- "<table width="95%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center"><tr><td>".
- "<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center"><tr>".
- "<td class="install" height="30" valign="bottom"><font color="#FF0000">>></font> Discuz! 6.0.0 > UCenter 头像导入工具".
- "</td></tr><tr><td><hr noshade align="center" width="100%" size="1"></td></tr><tr><td colspan="2">";
- }
- function instfooter() {
- echo "</td></tr><tr><td><hr noshade align="center" width="100%" size="1"></td></tr>".
- "<tr><td align="center">".
- "<b style="font-size: 11px">Powered by <a href="http://discuz.net" target="_blank">Discuz!".
- "</a> Copyright © <a href="http://www.comsenz.com" target="_blank">Comsenz Inc.</a> 2001-2008</b><br /><br />".
- "</td></tr></table></td></tr></table>".
- "</body></html>";
- }
- function set_home($uid, $dir = '.') {
- $uid = abs(intval($uid));
- $uid = sprintf("%09d", $uid);
- $dir1 = substr($uid, 0, 3);
- $dir2 = substr($uid, 3, 2);
- $dir3 = substr($uid, 5, 2);
- !is_dir($dir.'/'.$dir1) && mkdir($dir.'/'.$dir1, 0777);
- !is_dir($dir.'/'.$dir1.'/'.$dir2) && mkdir($dir.'/'.$dir1.'/'.$dir2, 0777);
- !is_dir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3) && mkdir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3, 0777);
- return $dir1.'/'.$dir2.'/'.$dir3;
- }
- /**
- * 根据用户的 uid 得到用户的 home 目录
- *
- * @param int $uid
- * @return string
- */
- function get_home($uid) {
- $uid = abs(intval($uid));
- $uid = sprintf("%09d", $uid);
- $dir1 = substr($uid, 0, 3);
- $dir2 = substr($uid, 3, 2);
- $dir3 = substr($uid, 5, 2);
- return $dir1.'/'.$dir2.'/'.$dir3;
- }
- function get_avatar($uid, $size = 'big') {
- $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'big';
- $uid = abs(intval($uid));
- $uid = sprintf("%09d", $uid);
- $dir1 = substr($uid, 0, 3);
- $dir2 = substr($uid, 3, 2);
- $dir3 = substr($uid, 5, 2);
- return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2)."_avatar_$size.jpg";
- }
- class Image_Lite {
- var $attachinfo = '';
- var $srcfile = '';
- var $targetfile = '';
- var $imagecreatefromfunc = '';
- var $imagefunc = '';
- var $attach = array();
- var $animatedgif = 0;
- function Image_Lite($srcfile, $targetfile) {
- $this->srcfile = $srcfile;
- $this->targetfile = $targetfile;
- $this->attachinfo = @getimagesize($srcfile);
- switch($this->attachinfo['mime']) {
- case 'image/jpeg':
- $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
- $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
- break;
- case 'image/gif':
- $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
- $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
- break;
- case 'image/png':
- $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
- $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
- break;
- }
- $this->attach['size'] = @filesize($srcfile);
- if($this->attachinfo['mime'] == 'image/gif') {
- $fp = fopen($srcfile, 'rb');
- $targetfilecontent = fread($fp, $this->attach['size']);
- fclose($fp);
- $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === FALSE ? 0 : 1;
- }
- }
- function Thumb($thumbwidth, $thumbheight, $preview = 0) {
- $return = $this->Thumb_GD($thumbwidth, $thumbheight);
- $this->attach['size'] = @filesize($this->targetfile);
- return $return;
- }
- function Thumb_GD($thumbwidth, $thumbheight) {
- if(function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
- $imagecreatefromfunc = $this->imagecreatefromfunc;
- $imagefunc = $this->imagefunc;
- list($img_w, $img_h) = $this->attachinfo;
- if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
- $attach_photo = $imagecreatefromfunc($this->srcfile);
- $imgratio = $img_w / $img_h;
- $thumbratio = $thumbwidth / $thumbheight;
- 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;
- }
- $dst_photo = imagecreatetruecolor($cutx, $cuty);
- imageCopyMerge($dst_photo, $attach_photo, 0, 0, 0, 0, $cutx, $cuty, 100);
- $thumb['width'] = $thumbwidth;
- $thumb['height'] = $thumbheight;
- $targetfile = $this->targetfile;
- $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
- imageCopyreSampled($thumb_photo, $dst_photo ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $cutx, $cuty);
- clearstatcache();
- if($this->attachinfo['mime'] == 'image/jpeg') {
- $imagefunc($thumb_photo, $targetfile, 100);
- } else {
- $imagefunc($thumb_photo, $targetfile);
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- }
- ?>
复制代码 保存成PHP文件,注意修改//UCenter 路径//UCenter URL。在论坛淘的,我正在转换,管用不管用一会告诉你们。 |