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