欢迎各位兄弟 发布技术文章
这里的技术是共享的
001 | <?php |
002 | /** |
003 | * |
004 | * @author hou.yongxu |
005 | * @date 2016/6/20 |
006 | */ |
007 | Yii::import('application.extensions.qiniu.*'); |
008 | require_once('pfop.php'); |
009 | require_once('http.php'); |
010 | require_once('io.php'); |
011 | require_once('rs.php'); |
012 | require_once('fop.php'); |
013 | require_once('utils.php'); |
014 |
015 |
016 | class QiNiu |
017 | { |
018 | private $_bucket = ""; |
019 | private $_key1 = ""; |
020 | private $_accessKey = ''; |
021 | private $_secretKey = ''; |
022 | private $_domain = ''; |
023 | private $_pipeline = ''; //队列 |
024 | private static $_self = null; |
025 | function QiNiu($params = array()) |
026 | { |
027 | //测试配置 |
028 | $default = Yii::app()->params['qiniuConfig']; //默认参数配置 |
029 | $default = array_merge($default, $params); //传过来的参数 |
030 | $this->_bucket = $default['bucket']; |
031 | $this->_accessKey = $default['accessKey']; |
032 | $this->_secretKey = $default['secretKey']; |
033 | $this->_domain = $default['domain']; |
034 | $this->_pipeline = $default['pipeline']; |
035 | Qiniu_SetKeys($this->_accessKey, $this->_secretKey); |
036 | } |
037 | |
038 | /** |
039 | * config |
040 | * @param unknown $params |
041 | * @return QiNiu |
042 | */ |
043 | public static function getInstance($params = array()) |
044 | { |
045 | return self::$_self === null ? (self::$_self = new QiNiu($params)) : self::$_self; |
046 | } |
047 | |
048 | /** |
049 | * 文件上传 |
050 | * @param string $key 生成图片的key |
051 | * @param sring $file 要上传的文件 |
052 | * @param array $option 保存多个缩略图文件 |
053 | * @return Ambigous <multitype:NULL unknown , multitype:NULL Qiniu_Error , multitype:NULL Ambigous <NULL, mixed> , multitype:unknown Qiniu_Error >|boolean |
054 | * option = array( |
055 | * 'files' => array( |
056 | array('w'=>200, 'h'=>100, 'filename'=>'ab1.jpg'), |
057 | array('w'=>200, 'filename'=>'ab2.jpg'), |
058 | ); |
059 | * ) |
060 | */ |
061 | public function upload($key, $file, $option = array()) |
062 | { |
063 | $putPolicy = new Qiniu_RS_PutPolicy($this->_bucket . ':' . $key); |
064 | $saveas = ''; |
065 | if(!empty($option['files'])) |
066 | { |
067 | $saveas = $this->_get_saveas_str($option['files']); |
068 | } |
069 | |
070 | $putPolicy->PersistentPipeline=$this->_pipeline; |
071 | $putPolicy->PersistentOps = $saveas; |
072 | $putPolicy->ReturnBody = '{"key":$(key),"hash":$(etag),"pid":$(persistentId)}'; |
073 | |
074 | $upToken = $putPolicy->Token(null); |
075 | $putExtra = new Qiniu_PutExtra(); |
076 | $putExtra->Crc32 = 1; |
077 | list($ret, $err) = Qiniu_PutFile($upToken, $key, $file, $putExtra); |
078 | if ($err !== null) { |
079 | return $err;//不处理返回错误信息 |
080 | } |
081 | //清除出现saveas不成功的问题重新切图 |
082 | if(!empty($option['files'])) |
083 | { |
084 | $this->save($key, $option['files']); |
085 | /** |
086 | foreach($option['files'] as $item) |
087 | { |
088 | $flg = $this->save($key, $item); |
089 | } |
090 | **/ |
091 | } |
092 |
093 | return true; |
094 | } |
095 | |
096 | /** |
097 | * 生成 saveas 一定要有filename否则异常 |
098 | * @param unknown $data |
099 | * @param string $fops |
100 | */ |
101 | private function _get_saveas_str($data, $fops = '') |
102 | { |
103 | if(isset($data['filename'])) |
104 | { |
105 | $temp = $fops ? ';' : ''; |
106 | $fops .= $temp . "imageView/2"; |
107 | $fops .= empty($data['w']) ? '' : ('/w/' . $data['w']); |
108 | $fops .= empty($data['h']) ? '' : ('/h/' . $data['h']); |
109 | $entry .= Qiniu_Encode("{$this->_bucket}:{$data['filename']}"); |
110 | $fops .= '|saveas/' . $entry ; |
111 | } |
112 | else |
113 | { |
114 | foreach($data as $item) |
115 | { |
116 | $fops = $this->_get_saveas_str($item, $fops); |
117 | } |
118 | } |
119 | return $fops; |
120 | } |
121 | |
122 | /** |
123 | * 另存为 暂时只支持 w h filename 参数 ,支持多数组 带有签名的url赞不支持多维数组,支持一维 |
124 | * @param unknown $key |
125 | * @param unknown $data |
126 | * @return Ambigous <multitype:NULL unknown , multitype:NULL Qiniu_Error , multitype:NULL Ambigous <NULL, mixed> , multitype:unknown Qiniu_Error > |
127 | * @dese 2种数组格式 array('w'=>对应的值,'h','filename'=>'必须存在') | array(array('w','h','filename'=>'必须存在'), array('w','h','filename'=>'必须存在')) |
128 | */ |
129 | public function save($key, $data) |
130 | { |
131 | $fops = $this->_get_saveas_str($data); |
132 | if(empty($fops)) |
133 | { |
134 | return false; |
135 | } |
136 | //生成签名 |
137 | //$sgin = $this->_get_savese_sgin($key, $fops); |
138 | //生成带有签名的url |
139 | //echo $return_url = 'http://' . $this->_domain . '/' . $key . '?' . $fops . '/sign/' . $sgin, "<br/>"; |
140 | $client = new Qiniu_MacHttpClient(null); |
141 | $pfop = new Qiniu_Pfop(); |
142 | $pfop->Pipeline=$this->_pipeline; |
143 | $pfop->Key = $key; |
144 | $pfop->Bucket = $this->_bucket; |
145 | $pfop->Fops = $fops ; |
146 | $pfop->Force = 1; |
147 | //访问生成 |
148 | return $pfop->MakeRequest($client); |
149 | } |
150 | |
151 | /** |
152 | * 返回签名 |
153 | * @param string $fops |
154 | */ |
155 | private function _get_savese_sgin($key, $fops) |
156 | { |
157 | $this->_mac = Qiniu_RequireMac($this->_mac); |
158 | $sgin = $this->_domain . '/' . $key . '?' . $fops; |
159 | return $this->_mac->Sign($sgin); |
160 | } |
161 | |
162 | /** |
163 | * 删除 |
164 | * @param unknown $key |
165 | */ |
166 | public function delete($key) |
167 | { |
168 | $client = new Qiniu_MacHttpClient(null); |
169 | return Qiniu_RS_Delete($client, $this->_bucket, $key); |
170 | } |
171 | } |
著作权归叶子博客所有。商业转载请联系博主获得授权,非商业转载请注明出处
本文出处:https://www.yezismile.com/index/titleinfo/id/410
来自 https://www.yezismile.com/index/titleinfo/id/410