Codeigniter+flash实现Avatar头像上传的程序

头像上传功能我们用到最多的就简单的flash+php来实现上传前剪切图片然后达到我们想要的效果了,下面我来给各位整理几个基于ci整合Avatar头像上传功能,希望例子能帮助到各位,然后在控制器中调用即可:实现方法,代码如下:

  1. //重新设置session(防止新传头像无法显示)
  2. $this->session->set_userdata('user_info',$user_info);
  3. //项目决对路径
  4. $base_path = rtrim(str_replace('\\','/', FCPATH),'/');
  5. require APPPATH.'libraries/avatar.class.php';
  6. $au = new avatar($base_path.'/uploads/user/'.$user_info['mid'].'/',$base_path.'/uploads/user/temp/','/uploads/user/','/faceapi/');
  7. if(!$user_info['face']){
  8. $uid = 'uface_'.$user_info['mid'];
  9. }else{
  10. $uid = $user_info['face'];
  11. }//开源代码phpfensi.com
  12. $this->data['urlAvatarBig'] = $user_info['face'] ? $au->getAvatarUrl($uid,'big') : (($this->user_info['sex']==0 )? static_url().'/uploads/user/dfgirl.png' : static_url().'/uploads/user/dfboy.png' );
  13. $this->data['urlCameraFlash'] = $au->renderHtml($uid);
  14. $this->data['uid'] = $uid;//头像标示
  15. $this->load->view('center/member/edit_face',$this->data);

视图中的调用方法,代码如下:

  1. <div ><?php echo $urlCameraFlash ?></div>
  2. <script type="text/javascript">
  3. function updateavatar(){
  4. $('#userface').attr('src','/uploads/user/<?php echo $user_info['mid'].'/'.$uid?>_big.jpg?aid='+Math.random());
  5. }
  6. </script>

下面再来补充一下avatar.class.php了,代码如下:

  1. <?php
  2. class avatar{
  3. public $savePath;
  4. public $tempPath;
  5. public $viewPath;
  6. public $urlPath;
  7. public $mid;
  8. public function __construct($savePath,$tempPath,$viewPath,$urlPath,$mid){
  9. $this->savePath=$savePath;
  10. $this->tempPath=$tempPath;
  11. $this->viewPath='http://'.$_SERVER['HTTP_HOST'].$viewPath;
  12. $this->urlPath='http://'.$_SERVER['HTTP_HOST'].$urlPath;
  13. $this->mid = $mid;
  14. }
  15. // 第一步:上传原始图片文件
  16. private function uploadAvatar($uid){
  17. // 检查上传文件的有效性
  18. if(emptyempty($_FILES['Filedata'])){
  19. return -3; // No photograph be upload!
  20. }
  21. // 本地临时存储位置
  22. $tmpPath = $this->tempPath."{$uid}.jpg";
  23. // 如果临时存储的文件夹不存在,先创建它
  24. $dir=dirname($tmpPath);
  25. if(!file_exists($dir)){
  26. @mkdir($dir,0777, true );
  27. }
  28. // 如果同名的临时文件已经存在,先删除它
  29. if(file_exists($tmpPath)){
  30. @unlink($tmpPath);
  31. }
  32. // 把上传的图片文件保存到预定位置
  33. if ( @copy($_FILES['Filedata']['tmp_name'], $tmpPath) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpPath)) {
  34. @unlink($_FILES['Filedata']['tmp_name']);
  35. list($width, $height, $type, $attr) = getimagesize($tmpPath);
  36. if ( $width < 10 || $height < 10 || $width > 3000 || $height > 3000 || $type == 4 ) {
  37. @unlink($tmpPath);
  38. return -2; // Invalid photograph!
  39. }
  40. } else {
  41. @unlink($_FILES['Filedata']['tmp_name']);
  42. return -4; // Can not write to the data/tmp folder!
  43. }
  44. // 用于访问临时图片文件的 url
  45. $tmpUrl =$this->viewPath."temp/{$uid}.jpg";
  46. return $tmpUrl;
  47. }
  48. private function flashdata_decode($s) {
  49. $r = '';
  50. $l = strlen($s);
  51. for($i=0; $i<$l; $i=$i+2) {
  52. $k1 = ord($s[$i]) - 48;
  53. $k1 -= $k1 > 9 ? 7 : 0;
  54. $k2 = ord($s[$i+1]) - 48;
  55. $k2 -= $k2 > 9 ? 7 : 0;
  56. $r .= chr($k1 << 4 | $k2);
  57. }
  58. return $r;
  59. }
  60. // 第二步:上传分割后的三个图片数据流
  61. private function rectAvatar( $uid ){
  62. // 从 $_POST 中提取出三个图片数据流
  63. $bigavatar = $this->flashdata_decode( $_POST['avatar1'] );
  64. $middleavatar = $this->flashdata_decode( $_POST['avatar2'] );
  65. if ( !$bigavatar || !$middleavatar) {
  66. return '<root><message type="error" value="-2" /></root>';
  67. }
  68. //不存在目录,则创建
  69. if(!file_exists($this->savePath)){
  70. @mkdir($this->savePath,0777, true );
  71. }
  72. // 保存为图片文件
  73. $bigavatarfile = $this->savePath."{$uid}_big.jpg";
  74. $middleavatarfile = $this->savePath."{$uid}_middle.jpg";
  75. $success = 1;
  76. $fp = @fopen($bigavatarfile, 'wb');
  77. @fwrite($fp, $bigavatar);
  78. @fclose($fp);
  79. $fp = @fopen($middleavatarfile, 'wb');
  80. @fwrite($fp, $middleavatar);
  81. @fclose($fp);
  82. // 验证图片文件的正确性
  83. $biginfo = @getimagesize($bigavatarfile);
  84. $middleinfo = @getimagesize($middleavatarfile);
  85. if ( !$biginfo || !$middleinfo || $biginfo[2] == 4 || $middleinfo[2] == 4 ) {
  86. file_exists($bigavatarfile) && unlink($bigavatarfile);
  87. file_exists($middleavatarfile) && unlink($middleavatarfile);
  88. $success = 0;
  89. }
  90. // 删除临时存储的图片
  91. $tmpPath = $this->tempPath."{$uid}.jpg";
  92. @unlink($tmpPath);
  93. //临时保存头像
  94. $con=mysql_connect('localhost','root','root');
  95. mysql_select_db('zenyue');
  96. mysql_query("set names utf8");
  97. $sql="update zen_user set `face`='".$uid."' where m.$this->mid."'";
  98. mysql_query($sql);
  99. return '<?xml version="1.0" ?><root><face success="' . $success . '"/></root>';
  100. }
  101. // 从客户端访问头像图片的 url
  102. public function getAvatarUrl( $uid, $size='middle' ){
  103. $ci = &get_instance();
  104. $user_info = $ci->session->userdata('user_info');
  105. return $this->viewPath."{$user_info['mid']}/{$uid}_{$size}.jpg";
  106. }
  107. // 处理 HTTP Request
  108. // 返回值:如果是可识别的 request,处理后返回 true;否则返回 false。
  109. public function processRequest(){
  110. // 从 input 参数里拆解出自定义参数
  111. $arr = array();
  112. parse_str( $_GET['input'], $arr );
  113. $uid = $arr['uid'];
  114. if ( $_GET['a'] == 'uploadavatar') {
  115. // 第一步:上传原始图片文件
  116. echo $this->uploadAvatar( $uid );
  117. return true;
  118. } else if ( $_GET['a'] == 'rectavatar') {
  119. // 第二步:上传分割后的三个图片数据流
  120. echo $this->rectAvatar( $uid );
  121. return true;
  122. }
  123. return false;
  124. }
  125. // 编辑页面中包含 camera.swf 的 HTML 代码
  126. public function renderHtml( $uid ){
  127. // 把需要回传的自定义参数都组装在 input 里
  128. $ci = &get_instance();
  129. $user_info = $ci->session->userdata('user_info');
  130. $input = urlencode("u.session_id().'&mmid']);
  131. $baseUrl = '/public/zen/js/avatar/';
  132. $uc_api = urlencode( $this->urlPath.'avatar.php');
  133. $urlCameraFlash = "{$baseUrl}camera.swf?m=user&inajax=1&app;
  134. $urlCameraFlash = '<script src="'.$baseUrl.'common.js?B6k" type="text/javascript"></script><script type="text/javascript">document.write(AC_FL_RunContent("width","455","height","253","scale","exactfit","src","'.$urlCameraFlash.'","id","mycamera","name","mycamera","quality","high","bgcolor","#ffffff","wmode","transparent","menu","false","swLiveConnect","true","allowScriptAccess","always"));</script>';
  135. return $urlCameraFlash;
  136. }
  137. }
  138. ?>