Home > php教程 > php手册 > PHP中整合UCHOME中上传头像插件

PHP中整合UCHOME中上传头像插件

WBOY
Release: 2016-06-06 19:58:59
Original
1156 people have browsed it

UCHOME现在很多论坛都是在使用该开源程序,其中上传头像模块很是好使,下面作者就使用网上有的分离出来UCHOME的AVATAR上传插件进行方法整合。 首先是将下载的插件包放到项目中:(这边先废话下,采用的是YII框架进行整合的,本来想放进YII的扩展包里面的,但

UCHOME现在很多论坛都是在使用该开源程序,其中上传头像模块很是好使,下面作者就使用网上有的分离出来UCHOME的AVATAR上传插件进行方法整合。

首先是将下载的插件包放到项目中:(这边先废话下,采用的是YII框架进行整合的,本来想放进YII的扩展包里面的,但是测试后发现不得行,就放到项目中进行整合了。)

1、将下载下来的插件包放入如下位置:

PHP中整合UCHOME中上传头像插件

2、定义配置下载包中的配置文件,配置信息要按照个人的去进行配置了,作者的配置信息如下:configs.global.php

<span>php
    </span><span>error_reporting</span>(7<span>);
    @</span><span>header</span>("Content-type: text/html; charset=utf-8"<span>);
    </span><span>session_cache_limiter</span>('public, must-revalidate'<span>);
    date_default_timezone_set(</span>'PRC'<span>);
    
    </span><span>/*</span><span>
    |---------------------------------------------------------------
    | PHP ERROR REPORTING LEVEL
    |---------------------------------------------------------------
    </span><span>*/</span>
    <span>//</span><span>    error_reporting(E_ALL);</span>
    
    <span>define</span>('UC_API', 'http://'.<span>$_SERVER</span>['HTTP_HOST'].'/avatar'<span>);
    </span><span>define</span>('UC_API_EXEC','http://'.<span>$_SERVER</span>['HTTP_HOST'].'/avatar/index.php');<span>//</span><span>执行存储操作的文件</span>
    <span>define</span>('UC_DATAURL', UC_API.'/data'<span>);
    </span><span>define</span>('UC_DATADIR', 'data/');<span>//</span><span>图片存放相对地址</span>
?>
Copy after login

重要的是下面4个define里面的配置信息。

3、在YII的Controller.php基类中增加2个方法:

  1.第一个方法是用来初始化页面头像和FLASH上传插件的:

<span>/*</span><span>*
 * 整合UCHOME分离出来的头像上传插件
 * @param $uid 程序中用户的ID int类型
 * @return Array('avatarUrl','flashUrl');
 </span><span>*/</span>
<span>public</span> <span>function</span> avatar(<span>$uid</span><span>){
    </span><span>$avatarPath</span> = Yii::app() -> request ->baseUrl.'/avatar/';<span>//</span><span>获取UCAVATAR插件引入包路径</span>
    <span>require_once</span>(<span>$avatarPath</span>.'/configs.global.php');<span>//</span><span>引入UCAVATAR配置文件</span>
    <span>require_once</span>(<span>$avatarPath</span>.'/avatar.php');<span>//</span><span>引入UCAVATAR核心类</span>
    <span>$objAvatar</span> = <span>new</span><span> Avatar();
    </span><span>$personAvatarUrl</span>=<span>$this</span>->getAvatar(<span>$uid</span>);<span>//</span><span>获取用户头像</span>
    <span>$uc_avatarflash</span> = <span>$objAvatar</span>->uc_avatar(<span>$uid</span>, (<span>empty</span>(<span>$_SCONFIG</span>['avatarreal'])?'virtual':'real'));<span>//</span><span>获取上传头像FLASH插件地址</span>
    <span>return</span> <span>array</span>('personAvatarUrl'=><span>$personAvatarUrl</span>,'avatarFlashUrl'=><span>$uc_avatarflash</span><span>);
}</span>
Copy after login

  2.第二个方法是用来获取不同规格的头像路径的:

    <span>/*</span><span>*
     * 获取用户上传的头像
     * @param $uid
     * @param $size 值为:small、middle、big
     * @return String avatar URL
     * @example $size 默认获取big大图
     </span><span>*/</span>
    <span>public</span> <span>function</span> getAvatar(<span>$uid</span>,<span>$size</span>='big'<span>){
        </span><span>$uid</span> = <span>sprintf</span>('%09d', <span>$uid</span><span>);
        </span><span>$dir1</span> = <span>substr</span>(<span>$uid</span>, 0, 3<span>);
        </span><span>$dir2</span> = <span>substr</span>(<span>$uid</span>, 3, 2<span>);
        </span><span>$dir3</span> = <span>substr</span>(<span>$uid</span>, 5, 2<span>);
        </span><span>//</span><span>获取头像文件在系统中的绝对位置</span>
        <span>$avatarUrl</span>=<span>$_SERVER</span>['DOCUMENT_ROOT'].'/avatar/data/avatar/'.<span>$dir1</span>.'/'.<span>$dir2</span>.'/'.<span>$dir3</span>.'/'.<span>substr</span>(<span>$uid</span>, -2).'_avatar_'.<span>$size</span>.'.jpg'<span>;
        </span><span>//</span><span>检测是否存在有上传头像</span>
        <span>if</span> (<span>file_exists</span>(<span>$avatarUrl</span>)){<span>//</span><span>有,输出头像的URL地址</span>
            <span>return</span> 'http://'.<span>$_SERVER</span>['HTTP_HOST'].'/avatar/data/avatar/'.<span>$dir1</span>.'/'.<span>$dir2</span>.'/'.<span>$dir3</span>.'/'.<span>substr</span>(<span>$uid</span>, -2).'_avatar_'.<span>$size</span>.'.jpg'<span>;
        }</span><span>else</span>{<span>//</span><span>无,输出默认头像地址</span>
            <span>return</span> 'http://'.<span>$_SERVER</span>['HTTP_HOST'].'/avatar/images/noavatar_'.<span>$size</span>.'.gif'<span>;
        }
    }</span>
Copy after login

到此整合UCHOME的头像上传插件就基本完成了,下面进行测试,测试代码如下:

<span>$avatar</span>=<span>$this</span>->avatar(1000<span>);
</span><span>$pic</span>=<span>$avatar</span>['personAvatarUrl'<span>];
</span><span>$avatarFlash</span> =<span>$avatar</span>['avatarFlashUrl'];
Copy after login

前台页面调用的测试信息如下:

未上传头像时候的页面:

PHP中整合UCHOME中上传头像插件

上传头像后的页面展示效果如下:

PHP中整合UCHOME中上传头像插件

点击完成按钮后就完成了头像的设置。

到此整合UCHOME的头像上传插件就完成了。

插件上传地址:http://download.csdn.net/detail/zhuzuoer/6833843

本文由作者在 http://www.cnblogs.com/ez1989/ 博客园上首发。
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template