Home php教程 PHP源码 仿discuz 上传头像

仿discuz 上传头像

May 25, 2016 pm 05:12 PM
php

仿discuz 上传头像

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

仿discuz 上传头像

2. [代码][PHP]代码

<?php
class AvatarUploader
{
    // ? url
    private function getThisUrl()
    {
        $thisUrl = $_SERVER[&#39;SCRIPT_NAME&#39;];
        $thisUrl = "http://{$_SERVER[&#39;HTTP_HOST&#39;]}{$thisUrl}";
        return $thisUrl;
    }

    // ? base-url /
    private function getBaseUrl()
    {
        $baseUrl = $this->getThisUrl();
        $baseUrl = substr( $baseUrl, 0, strrpos( $baseUrl, &#39;/&#39; ) + 1 );
        return $baseUrl;
    }

    // ???? DIRECTORY_SEPARATOR
    private function getBasePath()
    {
        $basePath = $_SERVER[&#39;SCRIPT_FILENAME&#39;];
        $basePath = substr( $basePath, 0, strrpos($basePath, &#39;/&#39; ) + 1 );
        $basePath = str_replace( &#39;/&#39;, DIRECTORY_SEPARATOR, $basePath );
        return $basePath;
    }

    // ???????
    private function uploadAvatar( $uid )
    {
        // ??
        if ( empty($_FILES[&#39;Filedata&#39;]) ) {
            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[&#39;Filedata&#39;][&#39;tmp_name&#39;], $tmpPath) || @move_uploaded_file($_FILES[&#39;Filedata&#39;][&#39;tmp_name&#39;], $tmpPath)) {
            @unlink($_FILES[&#39;Filedata&#39;][&#39;tmp_name&#39;]);
            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[&#39;Filedata&#39;][&#39;tmp_name&#39;]);
            return -4; // Can not write to the data/tmp folder!
        }

        // ????? url
        $tmpUrl = $this->getBaseUrl() . "data/{$uid}";
        return $tmpUrl;
    }

    private function flashdata_decode($s) {
        $r = &#39;&#39;;
        $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[&#39;avatar1&#39;] );
        $middleavatar = $this->flashdata_decode( $_POST[&#39;avatar2&#39;] );
        $smallavatar  = $this->flashdata_decode( $_POST[&#39;avatar3&#39;] );
        if ( !$bigavatar || !$middleavatar || !$smallavatar ) {
            return &#39;<root><message type="error" value="-2" /></root>&#39;;
        }

        // ????
        $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, &#39;wb&#39;);
        @fwrite($fp, $bigavatar);
        @fclose($fp);

        $fp = @fopen($middleavatarfile, &#39;wb&#39;);
        @fwrite($fp, $middleavatar);
        @fclose($fp);

        $fp = @fopen($smallavatarfile, &#39;wb&#39;);
        @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 &#39;<?xml version="1.0" ?><root><face success="&#39; . $success . &#39;"/></root>&#39;;
    }

    // ?????? url
    public function getAvatarUrl( $uid, $size=&#39;middle&#39; )
    {
        return $this->getBaseUrl() . "data/{$uid}_{$size}.jpg";
    }

    //  HTTP Request
    // ??? request?? true?? false
    public function processRequest()
    {
        //  input ?
        $arr = array();
        parse_str( $_GET[&#39;input&#39;], $arr );
        $uid = intval($arr[&#39;uid&#39;]);

        if ( $_GET[&#39;a&#39;] == &#39;uploadavatar&#39;) {

            // ???????
            echo $this->uploadAvatar( $uid );
            return true;

        } else if ( $_GET[&#39;a&#39;] == &#39;rectavatar&#39;) {
        
            // ?????
            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 = &#39;<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="&#39;.$urlCameraFlash.&#39;" />
                <param name="menu" value="false" />
                <embed src="&#39;.$urlCameraFlash.&#39;" 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>&#39;;
        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[&#39;uid&#39;]);
$urlAvatarBig    = $au->getAvatarUrl( $uid, &#39;big&#39; );
$urlAvatarMiddle = $au->getAvatarUrl( $uid, &#39;middle&#39; );
$urlAvatarSmall  = $au->getAvatarUrl( $uid, &#39;small&#39; );
$urlCameraFlash = $au->renderHtml( $uid );
?>
<script type="text/javascript">
function updateavatar() {
    window.location.reload();
}
</script>
<img src="<?php echo $urlAvatarBig ?>">
<img src="<?php echo  $urlAvatarMiddle ?>">
<img src="<?php echo  $urlAvatarSmall ?>">
<hr>
<?php echo  $urlCameraFlash ?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles