用户
 找回密码
 入住 CI 中国社区
搜索
查看: 19380|回复: 31
收起左侧

[库 Library] flash ucenter 头像上传剪切功能(LIB)

  [复制链接]
发表于 2012-3-2 12:53:08 | 显示全部楼层 |阅读模式
咱BBS里有一个头像上传剪切功能整合ci2.1(已上传附件) http://codeigniter.org.cn/forums/thread-12957-1-1.html
是基于UC的,
camera.swf、locale.xml两文件你没有就应该百度一下ucenter你就知道了

先把camera.swf、locale.xml两个文件放到网站目录images下
把avatarlib.php放到你的libraries里(一般application/libraries/)

libraries

PHP复制代码
 
<?php
class Avatarlib
{
    function __construct()
    {
        $this->ci =& get_instance();
        $this->base_url   = $this->ci->config->base_url();
        /**                 *上传响应程序URL                 */
        $this->doaction   = 'http://localhost:80/index.php/avatar/doavatar';
        /**                 *临时上传目录,上传成功后自动删除,777属性哦                 *图片上传成功但没有保存请自行删除                 */
        $this->tmppath    = 'tmp/';
        /**                 * 保存图片目录,777属性哦                 */
        $this->avatarpath = 'avatar/';
        /**                 * FCPATH CI你懂的                  */
        $this->systempath = FCPATH;
    }
    /**         * 修改头像          * @param        int $uid          * @return        boole $returnhtml         */
    function uc_avatar($uid, $returnhtml = TRUE)
    {
        $uid            = intval($uid);
        $input          = urlencode("uid=$uid");
        $uc_avatarflash = $this->base_url . '/images/camera.swf?inajax=1&appid=1&input=' . $input . '&agent=' . md5($_SERVER['HTTP_USER_AGENT']) . '&ucapi=' . urlencode($this->doaction) . '&avatartype=real';
        if ($returnhtml)
        {
            return '<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="' . $uc_avatarflash . '" />                                <param name="menu" value="false" />                                <embed src="' . $uc_avatarflash . '" 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>';
        }
        else
        {
            return array(
                ' #ffffff',
                'wmode',
                'transparent',
                'menu',
                'false',
                'swLiveConnect',
                'true',
                'allowScriptAccess',
                'always'
            );
        }
    }
    /**         * 设置目录         * @param int $uid         */
    function get_home($uid)
    {
        $uid  = sprintf("%09d", $uid);
        $dir1 = substr($uid, 0, 3);
        $dir2 = substr($uid, 3, 2);
        $dir3 = substr($uid, 5, 2);
        return $dir1 . '/' . $dir2 . '/' . $dir3;
    }
    /**         * 获取目录         * @param int $uid         * @param string $dir         */
    function set_home($uid, $dir = '.')
    {
        $uid  = sprintf("%09d", $uid);
        $dir1 = substr($uid, 0, 3);
        $dir2 = substr($uid, 3, 2);
        $dir3 = substr($uid, 5, 2);
        !is_dir($dir . '/' . $dir1) && mkdir($dir . '/' . $dir1, 0777, TRUE);
        !is_dir($dir . '/' . $dir1 . '/' . $dir2) && mkdir($dir . '/' . $dir1 . '/' . $dir2, 0777, TRUE);
        !is_dir($dir . '/' . $dir1 . '/' . $dir2 . '/' . $dir3) && mkdir($dir . '/' . $dir1 . '/' . $dir2 . '/' . $dir3, 0777, TRUE);
    }
    /**         *应UC的相应,不变,上传时触发         *返回上传文件的地址         */
    function onuploadavatar()
    {
        @header("Expires: 0");
        @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
        @header("Pragma: no-cache");
        $arr = array();
        parse_str($_GET['input'], $arr);
        $uid = intval($arr['uid']);
        if (empty($uid))
        {
            return -1;
        }
        if (empty($_FILES['Filedata']))
        {
            return -3;
        }
        $forder                  = $this->tmppath;
        $file_forder             = $this->systempath . $forder;
        $config['upload_path']   = $file_forder;
        $config['allowed_types'] = 'jpg|jpeg|gif|png|bmp|jpe';
        $config['max_size']      = '2048';
        $config['file_name']     = 'useravatar' . $uid;
        $this->ci->load->library('upload', $config);
        $field_name = 'Filedata';
        if ($this->ci->upload->do_upload($field_name))
        {
            $data = $this->ci->upload->data();
            return $this->base_url . $forder . $data['file_name'];
        }
        return $this->ci->upload->display_errors() . $config['upload_path'];
    }
    /**         * 保存图片时响应         */
    function onrectavatar()
    {
        @header("Expires: 0");
        @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
        @header("Pragma: no-cache");
        header("Content-type: application/xml; charset=utf-8");
        $arr = array();
        parse_str($_GET['input'], $arr);
        $uid = intval($arr['uid']);
        if (empty($uid))
        {
            return '<root><message type="error" value="-1" /></root>';
        }
        $home = $this->get_home($uid);
        if (!is_dir($this->systempath . $this->avatarpath . $home))
        {
            $this->set_home($uid, $this->systempath . $this->avatarpath);
        }
        $bigavatarfile    = $this->systempath . $this->avatarpath . $this->get_avatar($uid, 'big');
        $middleavatarfile = $this->systempath . $this->avatarpath . $this->get_avatar($uid, 'middle');
        $smallavatarfile  = $this->systempath . $this->avatarpath . $this->get_avatar($uid, 'small');
        $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>';
        }
        $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;
        }
        $filetype = '.jpg';
        @unlink($this->systempath . $this->tmppath . 'useravatar' . $uid . $filetype);
        if ($success)
        {
            return '<?xml version="1.0" ?><root><face success="1"/></root>';
        }
        return '<?xml version="1.0" ?><root><face success="0"/></root>';
    }
    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;
    }
    function get_avatar($uid, $size = 'big')
    {
        $size = in_array($size, array(
            'big',
            'middle',
            'small'
        )) ? $size : 'big';
        $uid  = abs(intval($uid));
        $uid  = sprintf("%09d", $uid);
        $dir1 = substr($uid, 0, 3);
        $dir2 = substr($uid, 3, 2);
        $dir3 = substr($uid, 5, 2);
        return $dir1 . '/' . $dir2 . '/' . $dir3 . '/' . substr($uid, -2) . "_avatar_$size.jpg";
    }
    function avatar_show($uid, $size = 'small', $returnsrc = FALSE)
    {
        $size       = in_array($size, array(
            'big',
            'middle',
            'small'
        )) ? $size : 'small';
        $avatarfile = $this->get_avatar($uid, $size);
        return $returnsrc ? $this->base_url . $this->avatarpath . $avatarfile : '<img src="' . $this->base_url . $this->avatarpath . $avatarfile . '">';
    }
}
 
复制代码


controllers

PHP复制代码
 
<?php
if (!defined('BASEPATH'))
{
    exit('No direct script access allowed');
}
class Avatar extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('avatarlib');
    }
    public function index()
    {
        $data['uid']         = 1;
        $data['avatarflash'] = $this->avatarlib->uc_avatar($data['uid']);
        $data['avatarhtml']  = $this->avatarlib->avatar_show($data['uid'], 'big');
        $this->load->view('hello', $data);
    }
    function doavatar()
    {
        $action = 'on' . $_GET['a'];
        $data   = $this->avatarlib->$action();
        echo $data;
    }
}
 
复制代码


咱BBS里有一个头像上传剪切功能整合ci2.1 http://codeigniter.org.cn/forums/thread-12957-1-1.html

学习哦。camera.swf没有开源哦
ppt.rar (2.8 KB, 下载次数: 459)
 楼主| 发表于 2012-3-2 12:54:15 | 显示全部楼层
哦槽 代码怎么乱了
发表于 2012-3-3 19:33:04 | 显示全部楼层
能在项目里用吗
 楼主| 发表于 2012-3-3 23:00:18 | 显示全部楼层
我学习时是可以用的。但是能不能用这真不知道,因为没有开源。至于能不能成功部署在项目里,那么我说:可以的。
发表于 2012-3-6 18:25:17 | 显示全部楼层
baiyuxiong 发表于 2012-3-3 19:33
能在项目里用吗

我已经成功部署在自己的项目中
发表于 2012-3-6 21:34:36 | 显示全部楼层
zjqyyf 发表于 2012-3-6 18:25
我已经成功部署在自己的项目中

好,我研究下
发表于 2012-4-3 23:11:20 | 显示全部楼层
代码乱的蛋疼
 楼主| 发表于 2012-4-4 01:20:09 | 显示全部楼层
o熊小猫o 发表于 2012-4-3 23:11
代码乱的蛋疼

必须的,下载就附件里的吧,那个不乱.
发表于 2012-4-4 11:08:02 | 显示全部楼层
嗯,下载的蛮清晰。呵呵
发表于 2012-4-18 15:25:44 | 显示全部楼层
为什么我这不好用?唉…………

本版积分规则