Codeigniter+flash实现Avatar头像上传的程序
头像上传功能我们用到最多的就简单的flash+php来实现上传前剪切图片然后达到我们想要的效果了,下面我来给各位整理几个基于ci整合Avatar头像上传功能,希望例子能帮助到各位,然后在控制器中调用即可:实现方法,代码如下:
-
- $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'];
- }
-
- $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(emptyempty($_FILES['Filedata'])){
- return -3;
- }
-
- $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;
- }
- } else {
- @unlink($_FILES['Filedata']['tmp_name']);
- return -4;
- }
-
- $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 ){
-
- $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>';
- }
-
- 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";
- }
-
-
- public function processRequest(){
-
- $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;
- }
-
- public function renderHtml( $uid ){
-
- $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;
- }
- }
- ?>