1 | <?php |
||
2 | |||
3 | namespace Qiniu; |
||
4 | |||
5 | use Qiniu\Config; |
||
6 | |||
7 | if (!defined('QINIU_FUNCTIONS_VERSION')) { |
||
8 | define('QINIU_FUNCTIONS_VERSION', Config::SDK_VER); |
||
9 | |||
10 | /** |
||
11 | * 计算文件的crc32检验码: |
||
12 | * |
||
13 | * @param $file string 待计算校验码的文件路径 |
||
14 | * |
||
15 | * @return string 文件内容的crc32校验码 |
||
16 | */ |
||
17 | function crc32_file($file) |
||
18 | { |
||
19 | 6 | $hash = hash_file('crc32b', $file); |
|
20 | 6 | $array = unpack('N', pack('H*', $hash)); |
|
21 | 6 | return sprintf('%u', $array[1]); |
|
22 | } |
||
23 | |||
24 | /** |
||
25 | * 计算输入流的crc32检验码 |
||
26 | * |
||
27 | * @param $data 待计算校验码的字符串 |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
28 | * |
||
29 | * @return string 输入字符串的crc32校验码 |
||
30 | */ |
||
31 | function crc32_data($data) |
||
32 | { |
||
33 | 18 | $hash = hash('crc32b', $data); |
|
34 | 18 | $array = unpack('N', pack('H*', $hash)); |
|
35 | 18 | return sprintf('%u', $array[1]); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * 对提供的数据进行urlsafe的base64编码。 |
||
40 | * |
||
41 | * @param string $data 待编码的数据,一般为字符串 |
||
42 | * |
||
43 | * @return string 编码后的字符串 |
||
44 | * @link http://developer.qiniu.com/docs/v6/api/overview/appendix.html#urlsafe-base64 |
||
45 | */ |
||
46 | function base64_urlSafeEncode($data) |
||
47 | { |
||
48 | 114 | $find = array('+', '/'); |
|
49 | 114 | $replace = array('-', '_'); |
|
50 | 114 | return str_replace($find, $replace, base64_encode($data)); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * 对提供的urlsafe的base64编码的数据进行解码 |
||
55 | * |
||
56 | * @param string $str 待解码的数据,一般为字符串 |
||
57 | * |
||
58 | * @return string 解码后的字符串 |
||
59 | */ |
||
60 | function base64_urlSafeDecode($str) |
||
61 | { |
||
62 | 21 | $find = array('-', '_'); |
|
63 | 21 | $replace = array('+', '/'); |
|
64 | 21 | return base64_decode(str_replace($find, $replace, $str)); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * 二维数组根据某个字段排序 |
||
69 | * @param array $array 要排序的数组 |
||
70 | * @param string $key 要排序的键 |
||
71 | * @param string $sort 排序类型 SORT_ASC SORT_DESC |
||
72 | * return array 排序后的数组 |
||
73 | */ |
||
74 | function arraySort($array, $key, $sort = SORT_ASC) |
||
75 | { |
||
76 | $keysValue = array(); |
||
77 | foreach ($array as $k => $v) { |
||
78 | $keysValue[$k] = $v[$key]; |
||
79 | } |
||
80 | array_multisort($keysValue, $sort, $array); |
||
81 | return $array; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Wrapper for JSON decode that implements error detection with helpful |
||
86 | * error messages. |
||
87 | * |
||
88 | 90 | * @param string $json JSON data to parse |
|
89 | * @param bool $assoc When true, returned objects will be converted |
||
90 | 90 | * into associative arrays. |
|
91 | 18 | * @param int $depth User specified recursion depth. |
|
92 | * |
||
93 | 84 | * @return mixed |
|
94 | * @throws \InvalidArgumentException if the JSON cannot be parsed. |
||
95 | 84 | * @link http://www.php.net/manual/en/function.json-decode.php |
|
96 | */ |
||
97 | function json_decode($json, $assoc = false, $depth = 512) |
||
98 | { |
||
99 | static $jsonErrors = array( |
||
100 | JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
||
101 | JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
||
102 | JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
||
103 | JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
||
104 | JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
||
105 | 84 | ); |
|
106 | |||
107 | if (empty($json)) { |
||
108 | return null; |
||
109 | } |
||
110 | $data = \json_decode($json, $assoc, $depth); |
||
111 | |||
112 | if (JSON_ERROR_NONE !== json_last_error()) { |
||
113 | $last = json_last_error(); |
||
114 | throw new \InvalidArgumentException( |
||
115 | 'Unable to parse JSON data: ' |
||
116 | . (isset($jsonErrors[$last]) |
||
117 | ? $jsonErrors[$last] |
||
118 | : 'Unknown error') |
||
119 | 36 | ); |
|
120 | 36 | } |
|
121 | 36 | ||
122 | 36 | return $data; |
|
123 | 36 | } |
|
124 | |||
125 | /** |
||
126 | * 计算七牛API中的数据格式 |
||
127 | * |
||
128 | * @param string $bucket 待操作的空间名 |
||
129 | * @param string $key 待操作的文件名 |
||
130 | * |
||
131 | * @return string 符合七牛API规格的数据格式 |
||
132 | * @link http://developer.qiniu.com/docs/v6/api/reference/data-formats.html |
||
133 | */ |
||
134 | function entry($bucket, $key) |
||
135 | { |
||
136 | $en = $bucket; |
||
137 | 12 | if (!empty($key)) { |
|
138 | 3 | $en = $bucket . ':' . $key; |
|
139 | 3 | } |
|
140 | 12 | return base64_urlSafeEncode($en); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * array 辅助方法,无值时不set |
||
145 | * |
||
146 | * @param array $array 待操作array |
||
147 | * @param string $key key |
||
148 | * @param string $value value 为null时 不设置 |
||
149 | * |
||
150 | * @return array 原来的array,便于连续操作 |
||
151 | */ |
||
152 | function setWithoutEmpty(&$array, $key, $value) |
||
153 | { |
||
154 | if (!empty($value)) { |
||
155 | $array[$key] = $value; |
||
156 | } |
||
157 | return $array; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * 缩略图链接拼接 |
||
162 | * |
||
163 | * @param string $url 图片链接 |
||
164 | * @param int $mode 缩略模式 |
||
165 | * @param int $width 宽度 |
||
166 | * @param int $height 长度 |
||
167 | * @param string $format 输出类型 |
||
168 | * @param int $quality 图片质量 |
||
169 | 3 | * @param int $interlace 是否支持渐进显示 |
|
170 | 3 | * @param int $ignoreError 忽略结果 |
|
171 | 3 | * @return string |
|
172 | 3 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html |
|
173 | * @author Sherlock Ren <[email protected]> |
||
174 | 3 | */ |
|
175 | function thumbnail( |
||
176 | $url, |
||
177 | $mode, |
||
178 | $width, |
||
179 | $height, |
||
180 | $format = null, |
||
181 | $quality = null, |
||
182 | $interlace = null, |
||
183 | $ignoreError = 1 |
||
184 | ) { |
||
185 | |||
186 | static $imageUrlBuilder = null; |
||
187 | if (is_null($imageUrlBuilder)) { |
||
188 | $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
||
189 | } |
||
190 | |||
191 | return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args()); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * 图片水印 |
||
196 | * |
||
197 | * @param string $url 图片链接 |
||
198 | * @param string $image 水印图片链接 |
||
199 | * @param numeric $dissolve 透明度 |
||
200 | * @param string $gravity 水印位置 |
||
201 | 3 | * @param numeric $dx 横轴边距 |
|
202 | 3 | * @param numeric $dy 纵轴边距 |
|
203 | 3 | * @param numeric $watermarkScale 自适应原图的短边比例 |
|
204 | 3 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html |
|
205 | * @return string |
||
206 | 3 | * @author Sherlock Ren <[email protected]> |
|
207 | */ |
||
208 | function waterImg( |
||
209 | $url, |
||
210 | $image, |
||
211 | $dissolve = 100, |
||
212 | $gravity = 'SouthEast', |
||
213 | $dx = null, |
||
214 | $dy = null, |
||
215 | $watermarkScale = null |
||
216 | ) { |
||
217 | |||
218 | static $imageUrlBuilder = null; |
||
219 | if (is_null($imageUrlBuilder)) { |
||
220 | $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
||
221 | } |
||
222 | |||
223 | return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args()); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * 文字水印 |
||
228 | * |
||
229 | * @param string $url 图片链接 |
||
230 | * @param string $text 文字 |
||
231 | * @param string $font 文字字体 |
||
232 | * @param string $fontSize 文字字号 |
||
233 | * @param string $fontColor 文字颜色 |
||
234 | * @param numeric $dissolve 透明度 |
||
235 | * @param string $gravity 水印位置 |
||
236 | * @param numeric $dx 横轴边距 |
||
237 | 3 | * @param numeric $dy 纵轴边距 |
|
238 | 3 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark |
|
239 | 3 | * @return string |
|
240 | 3 | * @author Sherlock Ren <[email protected]> |
|
241 | */ |
||
242 | 3 | function waterText( |
|
243 | $url, |
||
244 | $text, |
||
245 | $font = '黑体', |
||
246 | $fontSize = 0, |
||
247 | $fontColor = null, |
||
248 | $dissolve = 100, |
||
249 | $gravity = 'SouthEast', |
||
250 | $dx = null, |
||
251 | $dy = null |
||
252 | ) { |
||
253 | 18 | ||
254 | 18 | static $imageUrlBuilder = null; |
|
255 | if (is_null($imageUrlBuilder)) { |
||
256 | $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
||
257 | 18 | } |
|
258 | 18 | ||
259 | 18 | return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args()); |
|
260 | 18 | } |
|
261 | 18 | ||
262 | 18 | /** |
|
263 | * 从uptoken解析accessKey和bucket |
||
264 | * |
||
265 | * @param $upToken |
||
266 | * @return array(ak,bucket,err=null) |
||
267 | */ |
||
268 | function explodeUpToken($upToken) |
||
269 | { |
||
270 | $items = explode(':', $upToken); |
||
271 | if (count($items) != 3) { |
||
272 | return array(null, null, "invalid uptoken"); |
||
273 | } |
||
274 | $accessKey = $items[0]; |
||
275 | $putPolicy = json_decode(base64_urlSafeDecode($items[2])); |
||
276 | $scope = $putPolicy->scope; |
||
277 | $scopeItems = explode(':', $scope); |
||
278 | $bucket = $scopeItems[0]; |
||
279 | return array($accessKey, $bucket, null); |
||
280 | } |
||
281 | } |
||
282 |