Codeigniter+flash implementation of Avatar Avatar upload Program

Source: Internet
Author: User
Tags mkdir ord tmp folder urlencode codeigniter

Then call in the controller: implementation method

The code is as follows Copy Code

Reset session (prevent new avatar from being displayed)
$this->session->set_userdata (' User_info ', $user _info);

The path of the project decision
$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;//Head icon
$this->load->view (' Center/member/edit_face ', $this->data);


Calling methods in the view

The code is as follows Copy Code


<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>

Let's add avatar.class.php.

The code is as follows Copy Code

<?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;

}
The first step: upload the original image file
Private Function Uploadavatar ($uid) {
Check the validity of uploaded files
if (Empty ($_files[' filedata ')) {
return-3; No photograph be upload!
}
Local temporary storage location
$tmpPath = $this->temppath. " {$uid}.jpg ";
If the temporarily stored folder does not exist, create it first
$dir =dirname ($tmpPath);
if (!file_exists ($dir)) {
@mkdir ($dir, 0777, true);
}
If a temporary file with the same name already exists, delete it first
if (file_exists ($tmpPath)) {
@unlink ($tmpPath);
}
Save the uploaded picture file to a predetermined location
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 < | | $height < | | $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!
}

  //the URL used to access the temporary picture file
   $tmpUrl = $this->viewpath. " temp/{$uid}.jpg ";
  return $tmpUrl;
 }
 private function Flashdata_decode ($s) {
   $r = ';
   $l = strlen ($s);
&NBSP;&N Bsp;for ($i =0 $i < $l; $i = $i +2) {
    $k 1 = ord ($s [$i])-1
    $k 1 = $k > 9? 7:0;
    $k 2 = ord ($s [$i +1])-48;
    $k 2-= $k 2 > 9? 7:0;
    $r. = Chr ($k 1 << 4 | $k 2);
  }
  return $r;
 }

Step two: Upload the split three image data stream
Private Function Rectavatar ($uid) {
Extract three image streams from $_post
$bigavatar = $this->flashdata_decode ($_post[' avatar1 '));
$middleavatar = $this->flashdata_decode ($_post[' avatar2 '));
if (! $bigavatar | |! $middleavatar) {
Return ' <root><message type= ' ERROR "value="-2 "/></root>";
}

Directory is not present, create
if (!file_exists ($this->savepath)) {
@mkdir ($this->savepath,0777, true);
}

Save as Picture file
$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);

Verifying the correctness of picture files
$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;
}
Delete a temporary stored picture
$tmpPath = $this->temppath. " {$uid}.jpg ";
@unlink ($tmpPath);
Temporary Save Avatar
$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> ';
}
Access to the URL of the avatar picture from the client
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 ";
}
Handling HTTP Request
Return value: If it is an identifiable request, returns true after processing, or false.
Public Function ProcessRequest () {
To disassemble a custom parameter from the input parameter
$arr = Array ();
PARSE_STR ($_get[' input '), $arr);
$uid = $arr [' uid '];
if ($_get[' a '] = = ' Uploadavatar ') {
The first step: upload the original image file
echo $this->uploadavatar ($UID);
return true;
else if ($_get[' a '] = = ' Rectavatar ') {
Step two: Upload the split three image data stream
echo $this->rectavatar ($UID);
return true;
}
return false;
}
Edit the HTML code in the page that contains camera.swf
Public Function renderhtml ($uid) {
Assemble the custom parameters that need to be returned in the 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.