Codeigniter+flash实现Avatar头像上传的程序
头像上传功能我们用到最多的就简单的flash+php来实现上传前剪切图片然后达到我们想要的效果了,下面我来给各位整理几个基于ci整合Avatar头像上传功能,希望例子能帮助到各位,然后在控制器中调用即可:实现方法,代码如下:
//重新设置session(防止新传头像无法显示) $this ->session->set_userdata( 'user_info' , $user_info ); //项目决对路径 $base_path = rtrim( str_replace ( '\\' , '/' , FCPATH), '/' ); require APPPATH. 'libraries/avatar.class.php' ; $au = new avatar( $base_path . '/uploads/user/' . $user_info [ 'mid' ]. '/' , $base_path . '/uploads/user/temp/' , '/uploads/user/' , '/faceapi/' ); if (! $user_info [ 'face' ]){ $uid = 'uface_' . $user_info [ 'mid' ]; } else { $uid = $user_info [ 'face' ]; } //开源代码phpfensi.com $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' ); $this ->data[ 'urlCameraFlash' ] = $au ->renderHtml( $uid ); $this ->data[ 'uid' ] = $uid ; //头像标示 $this ->load->view( 'center/member/edit_face' , $this ->data);视图中的调用方法,代码如下:
<div style= "float:left;width:455px;height:253px;overflow:hidden;margin-left:10px;" ><?php echo $urlCameraFlash ?></div> <script type= "text/javascript" > function updateavatar(){ $( '#userface' ).attr( 'src' , '/uploads/user/<?php echo $user_info[' mid '].' / '.$uid?>_big.jpg?aid=' +Math.random()); } </script>下面再来补充一下avatar.class.php了,代码如下:
<?php class avatar{ public $savePath ; public $tempPath ; public $viewPath ; public $urlPath ; public $mid ; public function __construct( $savePath , $tempPath , $viewPath , $urlPath , $mid ){ $this ->savePath= $savePath ; $this ->tempPath= $tempPath ; $this ->viewPath= 'http://' . $_SERVER [ 'HTTP_HOST' ]. $viewPath ; $this ->urlPath= 'http://' . $_SERVER [ 'HTTP_HOST' ]. $urlPath ; $this ->mid = $mid ; } // 第一步:上传原始图片文件 private function uploadAvatar( $uid ){ // 检查上传文件的有效性 if ( empty empty ( $_FILES [ 'Filedata' ])){ return -3; // No photograph be upload! } // 本地临时存储位置 $tmpPath = $this ->tempPath. "{$uid}.jpg" ; // 如果临时存储的文件夹不存在,先创建它 $dir =dirname( $tmpPath ); if (! file_exists ( $dir )){ @ mkdir ( $dir ,0777, true ); } // 如果同名的临时文件已经存在,先删除它 if ( file_exists ( $tmpPath )){ @unlink( $tmpPath ); } // 把上传的图片文件保存到预定位置 if ( @ copy ( $_FILES [ 'Filedata' ][ 'tmp_name' ], $tmpPath ) || @move_uploaded_file( $_FILES [ 'Filedata' ][ 'tmp_name' ], $tmpPath )) { @unlink( $_FILES [ 'Filedata' ][ 'tmp_name' ]); list( $width , $height , $type , $attr ) = getimagesize ( $tmpPath ); if ( $width < 10 || $height < 10 || $width > 3000 || $height > 3000 || $type == 4 ) { @unlink( $tmpPath ); return -2; // Invalid photograph! } } else { @unlink( $_FILES [ 'Filedata' ][ 'tmp_name' ]); return -4; // Can not write to the data/tmp folder! } // 用于访问临时图片文件的 url $tmpUrl = $this ->viewPath. "temp/{$uid}.jpg" ; return $tmpUrl ; } private function flashdata_decode( $s ) { $r = '' ; $l = strlen ( $s ); for ( $i =0; $i < $l ; $i = $i +2) { $k1 = ord( $s [ $i ]) - 48; $k1 -= $k1 > 9 ? 7 : 0; $k2 = ord( $s [ $i +1]) - 48; $k2 -= $k2 > 9 ? 7 : 0; $r .= chr ( $k1 << 4 | $k2 ); } return $r ; } // 第二步:上传分割后的三个图片数据流 private function rectAvatar( $uid ){ // 从 $_POST 中提取出三个图片数据流 $bigavatar = $this ->flashdata_decode( $_POST [ 'avatar1' ] ); $middleavatar = $this ->flashdata_decode( $_POST [ 'avatar2' ] ); if ( ! $bigavatar || ! $middleavatar ) { return '<root><message type="error" value="-2" /></root>' ; } //不存在目录,则创建 if (! file_exists ( $this ->savePath)){ @ mkdir ( $this ->savePath,0777, true ); } // 保存为图片文件 $bigavatarfile = $this ->savePath. "{$uid}_big.jpg" ; $middleavatarfile = $this ->savePath. "{$uid}_middle.jpg" ; $success = 1; $fp = @ fopen ( $bigavatarfile , 'wb' ); @fwrite( $fp , $bigavatar ); @fclose( $fp ); $fp = @ fopen ( $middleavatarfile , 'wb' ); @fwrite( $fp , $middleavatar ); @fclose( $fp ); // 验证图片文件的正确性 $biginfo = @ getimagesize ( $bigavatarfile ); $middleinfo = @ getimagesize ( $middleavatarfile ); if ( ! $biginfo || ! $middleinfo || $biginfo [2] == 4 || $middleinfo [2] == 4 ) { file_exists ( $bigavatarfile ) && unlink( $bigavatarfile ); file_exists ( $middleavatarfile ) && unlink( $middleavatarfile ); $success = 0; } // 删除临时存储的图片 $tmpPath = $this ->tempPath. "{$uid}.jpg" ; @unlink( $tmpPath ); //临时保存头像 $con =mysql_connect( 'localhost' , 'root' , 'root' ); mysql_select_db( 'zenyue' ); mysql_query( "set names utf8" ); $sql = "update zen_user set `face`='" . $uid . "' where mid='" . $this ->mid. "'" ; mysql_query( $sql ); return '<?xml version="1.0" ?><root><face success="' . $success . '"/></root>' ; } // 从客户端访问头像图片的 url public function getAvatarUrl( $uid , $size = 'middle' ){ $ci = &get_instance(); $user_info = $ci ->session->userdata( 'user_info' ); return $this ->viewPath. "{$user_info['mid']}/{$uid}_{$size}.jpg" ; } // 处理 HTTP Request // 返回值:如果是可识别的 request,处理后返回 true;否则返回 false。 public function processRequest(){ // 从 input 参数里拆解出自定义参数 $arr = array (); parse_str ( $_GET [ 'input' ], $arr ); $uid = $arr [ 'uid' ]; if ( $_GET [ 'a' ] == 'uploadavatar' ) { // 第一步:上传原始图片文件 echo $this ->uploadAvatar( $uid ); return true; } else if ( $_GET [ 'a' ] == 'rectavatar' ) { // 第二步:上传分割后的三个图片数据流 echo $this ->rectAvatar( $uid ); return true; } return false; } // 编辑页面中包含 camera.swf 的 HTML 代码 public function renderHtml( $uid ){ // 把需要回传的自定义参数都组装在 input 里 $ci = &get_instance(); $user_info = $ci ->session->userdata( 'user_info' ); $input = urlencode( "uid={$uid}&FSESSIONID=" .session_id(). '&mid=' . $user_info [ 'mid' ]); $baseUrl = '/public/zen/js/avatar/' ; $uc_api = urlencode( $this ->urlPath. 'avatar.php' ); $urlCameraFlash = "{$baseUrl}camera.swf?m=user&inajax=1&appid=1&ucapi={$uc_api}&input={$input}&uploadSize=2048" ; $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>' ; return $urlCameraFlash ; } } ?>查看更多关于Codeigniter+flash实现Avatar头像上传的程序 - php上传下的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29333