Discuz教程网

[二次开发] 仿discuz 上传头像

[复制链接]
authicon dly 发表于 2012-8-3 20:24:32 | 显示全部楼层 |阅读模式
仿discuz 上传头像
27105246_YnAH.jpg
  1. <?php
  2. class AvatarUploader
  3. {
  4. // ? url
  5. private function getThisUrl()
  6. {
  7. $thisUrl = $_SERVER['SCRIPT_NAME'];
  8. $thisUrl = "http://{$_SERVER['HTTP_HOST']}{$thisUrl}";
  9. return $thisUrl;
  10. }

  11. // ? base-url /
  12. private function getBaseUrl()
  13. {
  14. $baseUrl = $this->getThisUrl();
  15. $baseUrl = substr( $baseUrl, 0, strrpos( $baseUrl, '/' ) + 1 );
  16. return $baseUrl;
  17. }

  18. // ???? DIRECTORY_SEPARATOR
  19. private function getBasePath()
  20. {
  21. $basePath = $_SERVER['SCRIPT_FILENAME'];
  22. $basePath = substr( $basePath, 0, strrpos($basePath, '/' ) + 1 );
  23. $basePath = str_replace( '/', DIRECTORY_SEPARATOR, $basePath );
  24. return $basePath;
  25. }

  26. // ???????
  27. private function uploadAvatar( $uid )
  28. {
  29. // ??
  30. if ( empty($_FILES['Filedata']) ) {
  31. return -3; // No photograph be upload!
  32. }

  33. // ?
  34. $tmpPath = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}";

  35. // ????
  36. $dir = dirname( $tmpPath );
  37. if ( !file_exists( $dir ) ) {
  38. @mkdir( $dir, 0777, true );
  39. }

  40. // ??????
  41. if ( file_exists($tmpPath) ) {
  42. @unlink($tmpPath);
  43. }

  44. // ?????
  45. if ( @copy($_FILES['Filedata']['tmp_name'], $tmpPath) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpPath)) {
  46. @unlink($_FILES['Filedata']['tmp_name']);
  47. list($width, $height, $type, $attr) = getimagesize($tmpPath);
  48. if ( $width < 10 || $height < 10 || $width > 3000 || $height > 3000 || $type == 4 ) {
  49. @unlink($tmpPath);
  50. return -2; // Invalid photograph!
  51. }
  52. } else {
  53. @unlink($_FILES['Filedata']['tmp_name']);
  54. return -4; // Can not write to the data/tmp folder!
  55. }

  56. // ????? url
  57. $tmpUrl = $this->getBaseUrl() . "data/{$uid}";
  58. return $tmpUrl;
  59. }

  60. private function flashdata_decode($s) {
  61. $r = '';
  62. $l = strlen($s);
  63. for($i=0; $i<$l; $i=$i+2) {
  64. $k1 = ord($s[$i]) - 48;
  65. $k1 -= $k1 > 9 ? 7 : 0;
  66. $k2 = ord($s[$i+1]) - 48;
  67. $k2 -= $k2 > 9 ? 7 : 0;
  68. $r .= chr($k1 << 4 | $k2);
  69. }
  70. return $r;
  71. }

  72. // ?????
  73. private function rectAvatar( $uid )
  74. {
  75. // $_POST ???
  76. $bigavatar = $this->flashdata_decode( $_POST['avatar1'] );
  77. $middleavatar = $this->flashdata_decode( $_POST['avatar2'] );
  78. $smallavatar = $this->flashdata_decode( $_POST['avatar3'] );
  79. if ( !$bigavatar || !$middleavatar || !$smallavatar ) {
  80. return '<root><message type="error" value="-2" /></root>';
  81. }

  82. // ????
  83. $bigavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_big.jpg";
  84. $middleavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_middle.jpg";
  85. $smallavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_small.jpg";

  86. $success = 1;
  87. $fp = @fopen($bigavatarfile, 'wb');
  88. @fwrite($fp, $bigavatar);
  89. @fclose($fp);

  90. $fp = @fopen($middleavatarfile, 'wb');
  91. @fwrite($fp, $middleavatar);
  92. @fclose($fp);

  93. $fp = @fopen($smallavatarfile, 'wb');
  94. @fwrite($fp, $smallavatar);
  95. @fclose($fp);

  96. // ?????
  97. $biginfo = @getimagesize($bigavatarfile);
  98. $middleinfo = @getimagesize($middleavatarfile);
  99. $smallinfo = @getimagesize($smallavatarfile);
  100. if ( !$biginfo || !$middleinfo || !$smallinfo || $biginfo[2] == 4 || $middleinfo[2] == 4 || $smallinfo[2] == 4 ) {
  101. file_exists($bigavatarfile) && unlink($bigavatarfile);
  102. file_exists($middleavatarfile) && unlink($middleavatarfile);
  103. file_exists($smallavatarfile) && unlink($smallavatarfile);
  104. $success = 0;
  105. }
  106. // ????
  107. $tmpPath = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}";
  108. @unlink($tmpPath);

  109. return '<?xml version="1.0" ?><root><face success="' . $success . '"/></root>';
  110. }

  111. // ?????? url
  112. public function getAvatarUrl( $uid, $size='middle' )
  113. {
  114. return $this->getBaseUrl() . "data/{$uid}_{$size}.jpg";
  115. }

  116. // HTTP Request
  117. // ??? request?? true?? false
  118. public function processRequest()
  119. {
  120. // input ?
  121. $arr = array();
  122. parse_str( $_GET['input'], $arr );
  123. $uid = intval($arr['uid']);

  124. if ( $_GET['a'] == 'uploadavatar') {

  125. // ???????
  126. echo $this->uploadAvatar( $uid );
  127. return true;

  128. } else if ( $_GET['a'] == 'rectavatar') {

  129. // ?????
  130. echo $this->rectAvatar( $uid );
  131. return true;
  132. }

  133. return false;
  134. }

  135. // ?? camera.swf HTML
  136. public function renderHtml( $uid )
  137. {
  138. // ???? input
  139. $input = urlencode( "uid={$uid}" );

  140. $baseUrl = $this->getBaseUrl();
  141. $uc_api = urlencode( $this->getThisUrl() );
  142. $urlCameraFlash = "{$baseUrl}camera.swf?ucapi={$uc_api}&input={$input}";
  143. $urlCameraFlash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="447" height="477" id="mycamera" align="middle">
  144. <param name="allowScriptAccess" value="always" />
  145. <param name="scale" value="exactfit" />
  146. <param name="wmode" value="transparent" />
  147. <param name="quality" value="high" />
  148. <param name="bgcolor" value="#ffffff" />
  149. <param name="movie" value="'.$urlCameraFlash.'" />
  150. <param name="menu" value="false" />
  151. <embed src="'.$urlCameraFlash.'" quality="high" bgcolor="#ffffff" width="447" height="477" name="mycamera" align="middle" allowScriptAccess="always" allowFullScreen="false" scale="exactfit" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
  152. </object>';
  153. return $urlCameraFlash;
  154. }
  155. }

  156. header("Expires: 0");
  157. header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  158. header("Pragma: no-cache");
  159. header("Cache-Control:no-cache");

  160. $au = new AvatarUploader();
  161. if ( $au->processRequest() ) {
  162. exit();
  163. }

  164. // ???? camera.swf
  165. $uid = intval($_GET['uid']);
  166. $urlAvatarBig = $au->getAvatarUrl( $uid, 'big' );
  167. $urlAvatarMiddle = $au->getAvatarUrl( $uid, 'middle' );
  168. $urlAvatarSmall = $au->getAvatarUrl( $uid, 'small' );
  169. $urlCameraFlash = $au->renderHtml( $uid );
  170. ?>
  171. <script type="text/javascript">
  172. function updateavatar() {
  173. window.location.reload();
  174. }
  175. </script>
  176. <img src="<?php echo $urlAvatarBig ?>">
  177. <img src="<?php echo $urlAvatarMiddle ?>">
  178. <img src="<?php echo $urlAvatarSmall ?>">
  179. <hr>
  180. <?php echo $urlCameraFlash ?>
  181. 还有附件,我没传,如果大家需要这种效果可以联系 我qq
复制代码






上一篇:discuz的image类使用
下一篇:discuz x1.5 计划任务改为系统层执行,加强计划任务执行、降低用户端执行压力
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 01:59

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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