头像php源码,仿discuz 上传头像

本文介绍了一个使用PHP编写的用户头像上传处理脚本,包括上传验证、文件路径管理、不同尺寸图片生成及URL获取。重点讲解了AvatarUploader类中关键方法,如图片尺寸检查、文件上传操作和URL构建。
摘要由CSDN通过智能技术生成

仿discuz 上传头像

1.[图片] ]6H2)I{}EKG4GC2RN_%[5_A.jpg

a623a82b4236d99b7806ea7141d0b439.png

2.[代码][PHP]代码<?php

class AvatarUploader

{

// ? url

private function getThisUrl()

{

$thisUrl = $_SERVER['SCRIPT_NAME'];

$thisUrl = "http://{$_SERVER['HTTP_HOST']}{$thisUrl}";

return $thisUrl;

}

// ? base-url /

private function getBaseUrl()

{

$baseUrl = $this->getThisUrl();

$baseUrl = substr( $baseUrl, 0, strrpos( $baseUrl, '/' ) + 1 );

return $baseUrl;

}

// ???? DIRECTORY_SEPARATOR

private function getBasePath()

{

$basePath = $_SERVER['SCRIPT_FILENAME'];

$basePath = substr( $basePath, 0, strrpos($basePath, '/' ) + 1 );

$basePath = str_replace( '/', DIRECTORY_SEPARATOR, $basePath );

return $basePath;

}

// ???????

private function uploadAvatar( $uid )

{

// ??

if ( empty($_FILES['Filedata']) ) {

return -3; // No photograph be upload!

}

// ?

$tmpPath = $this->getBasePath() . "data" . DIRECTORY_SEPARATOR . "{$uid}";

// ????

$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->getBaseUrl() . "data/{$uid}";

return $tmpUrl;

}

private function flashdata_decode($s) {

$r = '';

$l = strlen($s);

for($i=0; $i

$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'] );

$smallavatar = $this->flashdata_decode( $_POST['avatar3'] );

if ( !$bigavatar || !$middleavatar || !$smallavatar ) {

return '';

}

// ????

$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" ?>';

}

// ?????? 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 = '

';

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

?>

function updateavatar() {

window.location.reload();

}

<?php%20echo%20%24urlAvatarBig%20?>

<?php%20echo%20%20%24urlAvatarMiddle%20?>

<?php%20echo%20%20%24urlAvatarSmall%20?>


相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值