七牛上传分享
2016-08-24

侯哥写得七牛上传,在这里分享一下。

<?php
/**
 * 
 * @author hou.yongxu
 * @date 2016/6/20
 */
Yii::import('application.extensions.qiniu.*');
require_once('pfop.php');
require_once('http.php');
require_once('io.php');
require_once('rs.php');
require_once('fop.php');
require_once('utils.php');


class QiNiu
{
    private $_bucket = "";
    private $_key1 = "";
    private $_accessKey = '';
    private $_secretKey = '';
    private $_domain = '';
    private $_pipeline = '';            //队列
    private static $_self = null;
    function QiNiu($params = array())
    {
        //测试配置
        $default = Yii::app()->params['qiniuConfig'];   //默认参数配置
        $default = array_merge($default, $params);      //传过来的参数
        $this->_bucket = $default['bucket'];
        $this->_accessKey = $default['accessKey'];
        $this->_secretKey = $default['secretKey'];
        $this->_domain = $default['domain'];
        $this->_pipeline = $default['pipeline'];
        Qiniu_SetKeys($this->_accessKey, $this->_secretKey);
    }
    
    /**
     * config
     * @param unknown $params
     * @return QiNiu
     */
    public static function getInstance($params = array())
    {
        return self::$_self === null ? (self::$_self = new QiNiu($params)) : self::$_self;
    }
    
    /**
     * 文件上传
     * @param string $key   生成图片的key
     * @param sring $file   要上传的文件
     * @param array $option 保存多个缩略图文件
     * @return Ambigous <multitype:NULL unknown , multitype:NULL Qiniu_Error , multitype:NULL Ambigous <NULL, mixed> , multitype:unknown Qiniu_Error >|boolean
     * option = array(
     *  'files' => array(
            array('w'=>200, 'h'=>100, 'filename'=>'ab1.jpg'),
            array('w'=>200, 'filename'=>'ab2.jpg'),
        );
     * )
     */
    public function upload($key, $file, $option = array())
    {
        $putPolicy = new Qiniu_RS_PutPolicy($this->_bucket . ':' . $key);
        $saveas = '';
        if(!empty($option['files']))
        {
            $saveas = $this->_get_saveas_str($option['files']);
        }
        
        $putPolicy->PersistentPipeline=$this->_pipeline;
        $putPolicy->PersistentOps = $saveas;
        $putPolicy->ReturnBody = '{"key":$(key),"hash":$(etag),"pid":$(persistentId)}';
        
        $upToken = $putPolicy->Token(null);
        $putExtra = new Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        list($ret, $err) = Qiniu_PutFile($upToken, $key, $file, $putExtra);
        if ($err !== null) {
            return $err;//不处理返回错误信息
        }
        //清除出现saveas不成功的问题重新切图
        if(!empty($option['files']))
        {
            $this->save($key, $option['files']);
            /**
            foreach($option['files'] as $item)
            {
                $flg = $this->save($key, $item);
            }
            **/
        }

        return true;
    }
    
    /**
     * 生成 saveas 一定要有filename否则异常
     * @param unknown $data
     * @param string $fops
     */
    private function _get_saveas_str($data, $fops = '')
    {
        if(isset($data['filename']))
        {
            $temp = $fops ? ';' : '';
            $fops .= $temp . "imageView/2";
            $fops .= empty($data['w']) ? '' : ('/w/' . $data['w']);
            $fops .= empty($data['h']) ? '' : ('/h/' . $data['h']);
            $entry .= Qiniu_Encode("{$this->_bucket}:{$data['filename']}");
            $fops .= '|saveas/' . $entry ;
        }
        else
        {
            foreach($data as $item)
            {
                $fops = $this->_get_saveas_str($item, $fops);
            }
        }
        return $fops;
    }
    
    /**
     * 另存为 暂时只支持 w h filename 参数 ,支持多数组    带有签名的url赞不支持多维数组,支持一维  
     * @param unknown $key
     * @param unknown $data
     * @return Ambigous <multitype:NULL unknown , multitype:NULL Qiniu_Error , multitype:NULL Ambigous <NULL, mixed> , multitype:unknown Qiniu_Error >
     * @dese 2种数组格式 array('w'=>对应的值,'h','filename'=>'必须存在') | array(array('w','h','filename'=>'必须存在'), array('w','h','filename'=>'必须存在'))
     */
    public function save($key, $data)
    {
        $fops = $this->_get_saveas_str($data);
        if(empty($fops))
        {
            return false;
        }
        //生成签名
        //$sgin = $this->_get_savese_sgin($key, $fops);
        //生成带有签名的url
        //echo $return_url = 'http://' . $this->_domain . '/' . $key . '?' . $fops . '/sign/' . $sgin, "<br/>";
        $client = new Qiniu_MacHttpClient(null);
        $pfop = new Qiniu_Pfop();
        $pfop->Pipeline=$this->_pipeline;
        $pfop->Key = $key;
        $pfop->Bucket = $this->_bucket;
        $pfop->Fops =  $fops ;
        $pfop->Force = 1;
        //访问生成
        return $pfop->MakeRequest($client);
    }
    
    /**
     * 返回签名
     * @param string $fops
     */
    private function _get_savese_sgin($key, $fops)
    {
        $this->_mac = Qiniu_RequireMac($this->_mac);
        $sgin = $this->_domain . '/' . $key . '?' . $fops;
        return $this->_mac->Sign($sgin);
    }
    
    /**
     * 删除
     * @param unknown $key
     */
    public function delete($key)
    {
        $client = new Qiniu_MacHttpClient(null);
        return Qiniu_RS_Delete($client, $this->_bucket, $key);
    }
}

分享: