<?php
class AvatarUploader
{
// 本次頁面請求的 url
private function getThisUrl()
{
$thisUrl = $_SERVER['SCRIPT_NAME'];
$thisUrl = "http://{$_SERVER['HTTP_HOST']}{$thisUrl}";
return $thisUrl;
}javascript
// 本次頁面請求的 base-url(尾部有 /)
private function getBaseUrl()
{
$baseUrl = $this->getThisUrl();
$baseUrl = substr( $baseUrl, 0, strrpos( $baseUrl, '/' ) + 1 );
return $baseUrl;
}php
// 用於存儲的本地文件夾(尾部有一個 DIRECTORY_SEPARATOR)
private function getBasePath()
{
$basePath = $_SERVER['SCRIPT_FILENAME'];
$basePath = substr( $basePath, 0, strrpos($basePath, '/' ) + 1 );
$basePath = str_replace( '/', DIRECTORY_SEPARATOR, $basePath );
return $basePath;
}java
// 第一步:上傳原始圖片文件
private function uploadAvatar( $uid )
{
// 檢查上傳文件的有效性
if ( empty($_FILES['Filedata']) ) {
return -3; // No photograph be upload!
}api
// 本地臨時存儲位置
$tmpPath = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}";app
// 若是臨時存儲的文件夾不存在,先建立它
$dir = dirname( $tmpPath );
if ( !file_exists( $dir ) ) {
@mkdir( $dir, 0777, true );
}post
// 若是同名的臨時文件已經存在,先刪除它
if ( file_exists($tmpPath) ) {
@unlink($tmpPath);
}ui
// 把上傳的圖片文件保存到預約位置
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!
}this
// 用於訪問臨時圖片文件的 url
$tmpUrl = $this->getBaseUrl() . "data/{$uid}";
return $tmpUrl;
}url
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;
}spa
// 第二步:上傳分割後的三個圖片數據流
private function rectAvatar( $uid )
{
// 從 $_POST 中提取出三個圖片數據流
$bigavatar = $this->flashdata_decode( $_POST['avatar1'] );
$middleavatar = $this->flashdata_decode( $_POST['avatar2'] );
$smallavatar = $this->flashdata_decode( $_POST['avatar3'] );
if ( !$bigavatar || !$middleavatar || !$smallavatar ) {
return '<root><message type="error" value="-2" /></root>';
}
// 保存爲圖片文件
$bigavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_big.jpg";
$middleavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_middle.jpg";
$smallavatarfile = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}_small.jpg";
$success = 1;
$fp = @fopen($bigavatarfile, 'wb');
@fwrite($fp, $bigavatar);
@fclose($fp);
$fp = @fopen($middleavatarfile, 'wb');
@fwrite($fp, $middleavatar);
@fclose($fp);
$fp = @fopen($smallavatarfile, 'wb');
@fwrite($fp, $smallavatar);
@fclose($fp);
// 驗證圖片文件的正確性
$biginfo = @getimagesize($bigavatarfile);
$middleinfo = @getimagesize($middleavatarfile);
$smallinfo = @getimagesize($smallavatarfile);
if ( !$biginfo || !$middleinfo || !$smallinfo || $biginfo[2] == 4 || $middleinfo[2] == 4 || $smallinfo[2] == 4 ) {
file_exists($bigavatarfile) && unlink($bigavatarfile);
file_exists($middleavatarfile) && unlink($middleavatarfile);
file_exists($smallavatarfile) && unlink($smallavatarfile);
$success = 0;
}
// 刪除臨時存儲的圖片
$tmpPath = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}";
@unlink($tmpPath);
return '<?xml version="1.0" ?><root><face success="' . $success . '"/></root>';
}
// 從客戶端訪問頭像圖片的 url
public function getAvatarUrl( $uid, $size='middle' )
{
return $this->getBaseUrl() . "data/{$uid}_{$size}.jpg";
}
// 處理 HTTP Request
// 返回值:若是是可識別的 request,處理後返回 true;不然返回 false。
public function processRequest()
{
// 從 input 參數裏拆解出自定義參數
$arr = array();
parse_str( $_GET['input'], $arr );
$uid = intval($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 裏
$input = urlencode( "uid={$uid}" );
$baseUrl = $this->getBaseUrl();
$uc_api = urlencode( $this->getThisUrl() );
$urlCameraFlash = "{$baseUrl}camera.swf?ucapi={$uc_api}&input={$input}";
$urlCameraFlash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="447" height="477" id="mycamera" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="scale" value="exactfit" />
<param name="wmode" value="transparent" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="movie" value="'.$urlCameraFlash.'" />
<param name="menu" value="false" />
<embed src="'.$urlCameraFlash.'" quality="high" bgcolor="#ffffff" width="447" height="477" name="mycamera" align="middle" allowScriptAccess="always" allowFullScreen="false" scale="exactfit" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>';
return $urlCameraFlash;
}
}
header("Expires: 0");
header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
header("Pragma: no-cache");
header("Cache-Control:no-cache");
$au = new AvatarUploader();
if ( $au->processRequest() ) {
exit();
}
// 顯示編輯頁面,頁面中包含 camera.swf $uid = intval($_GET['uid']); $urlAvatarBig = $au->getAvatarUrl( $uid, 'big' ); $urlAvatarMiddle = $au->getAvatarUrl( $uid, 'middle' ); $urlAvatarSmall = $au->getAvatarUrl( $uid, 'small' ); $urlCameraFlash = $au->renderHtml( $uid ); ?> <script type="text/javascript"> function updateavatar() { window.location.reload(); } </script> <img src="<?= $urlAvatarBig ?>"> <img src="<?= $urlAvatarMiddle ?>"> <img src="<?= $urlAvatarSmall ?>"> <hr> <?= $urlCameraFlash ?>