0

0

仿discuz 上传头像

PHP中文网

PHP中文网

发布时间:2016-05-25 17:12:19

|

2079人浏览过

|

来源于php中文网

原创

仿discuz 上传头像

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

AI Portrait Generator
AI Portrait Generator

AI 头像工具,上传照片创建自己的艺术肖像。

下载

仿discuz 上传头像

2. [代码][PHP]代码

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<$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 )
    {
        //  $_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 '';
    }

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

@@##@@">
@@##@@">
@@##@@">

仿discuz 上传头像仿discuz 上传头像仿discuz 上传头像

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

15

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

4

2026.01.13

nginx部署php项目教程汇总
nginx部署php项目教程汇总

本专题整合了nginx部署php项目教程汇总,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 8.5万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号