Passed
Pull Request — master (#355)
by
unknown
20:42
created

arraySort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
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
The doc comment 待计算校验码的字符串 at position 0 could not be parsed: Unknown type name '待计算校验码的字符串' at position 0 in 待计算校验码的字符串.
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
        $keysValue = array();
76
        foreach ($array as $k => $v) {
77
            $keysValue[$k] = $v[$key];
78
        }
79
        array_multisort($keysValue, $sort, $array);
80
        return $array;
81
    }
82
83
    /**
84
     * Wrapper for JSON decode that implements error detection with helpful
85
     * error messages.
86
     *
87
     * @param string $json JSON data to parse
88 90
     * @param bool $assoc When true, returned objects will be converted
89
     *                        into associative arrays.
90 90
     * @param int $depth User specified recursion depth.
91 18
     *
92
     * @return mixed
93 84
     * @throws \InvalidArgumentException if the JSON cannot be parsed.
94
     * @link http://www.php.net/manual/en/function.json-decode.php
95 84
     */
96
    function json_decode($json, $assoc = false, $depth = 512)
97
    {
98
        static $jsonErrors = array(
99
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
100
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
101
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
102
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
103
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
104
        );
105 84
106
        if (empty($json)) {
107
            return null;
108
        }
109
        $data = \json_decode($json, $assoc, $depth);
110
111
        if (JSON_ERROR_NONE !== json_last_error()) {
112
            $last = json_last_error();
113
            throw new \InvalidArgumentException(
114
                'Unable to parse JSON data: '
115
                . (isset($jsonErrors[$last])
116
                    ? $jsonErrors[$last]
117
                    : 'Unknown error')
118
            );
119 36
        }
120 36
121 36
        return $data;
122 36
    }
123 36
124
    /**
125
     * 计算七牛API中的数据格式
126
     *
127
     * @param string $bucket 待操作的空间名
128
     * @param string $key 待操作的文件名
129
     *
130
     * @return string  符合七牛API规格的数据格式
131
     * @link http://developer.qiniu.com/docs/v6/api/reference/data-formats.html
132
     */
133
    function entry($bucket, $key)
134
    {
135
        $en = $bucket;
136
        if (!empty($key)) {
137 12
            $en = $bucket . ':' . $key;
138 3
        }
139 3
        return base64_urlSafeEncode($en);
140 12
    }
141
142
    /**
143
     * array 辅助方法,无值时不set
144
     *
145
     * @param array $array 待操作array
146
     * @param string $key key
147
     * @param string $value value 为null时 不设置
148
     *
149
     * @return array 原来的array,便于连续操作
150
     */
151
    function setWithoutEmpty(&$array, $key, $value)
152
    {
153
        if (!empty($value)) {
154
            $array[$key] = $value;
155
        }
156
        return $array;
157
    }
158
159
    /**
160
     * 缩略图链接拼接
161
     *
162
     * @param  string $url 图片链接
163
     * @param  int $mode 缩略模式
164
     * @param  int $width 宽度
165
     * @param  int $height 长度
166
     * @param  string $format 输出类型
167
     * @param  int $quality 图片质量
168
     * @param  int $interlace 是否支持渐进显示
169 3
     * @param  int $ignoreError 忽略结果
170 3
     * @return string
171 3
     * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
172 3
     * @author Sherlock Ren <[email protected]>
173
     */
174 3
    function thumbnail(
175
        $url,
176
        $mode,
177
        $width,
178
        $height,
179
        $format = null,
180
        $quality = null,
181
        $interlace = null,
182
        $ignoreError = 1
183
    ) {
184
185
        static $imageUrlBuilder = null;
186
        if (is_null($imageUrlBuilder)) {
187
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
188
        }
189
190
        return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args());
191
    }
192
193
    /**
194
     * 图片水印
195
     *
196
     * @param  string $url 图片链接
197
     * @param  string $image 水印图片链接
198
     * @param  numeric $dissolve 透明度
199
     * @param  string $gravity 水印位置
200
     * @param  numeric $dx 横轴边距
201 3
     * @param  numeric $dy 纵轴边距
0 ignored issues
show
Bug introduced by
The type Qiniu\numeric was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
202 3
     * @param  numeric $watermarkScale 自适应原图的短边比例
203 3
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
204 3
     * @return string
205
     * @author Sherlock Ren <[email protected]>
206 3
     */
207
    function waterImg(
208
        $url,
209
        $image,
210
        $dissolve = 100,
211
        $gravity = 'SouthEast',
212
        $dx = null,
213
        $dy = null,
214
        $watermarkScale = null
215
    ) {
216
217
        static $imageUrlBuilder = null;
218
        if (is_null($imageUrlBuilder)) {
219
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
220
        }
221
222
        return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args());
223
    }
224
225
    /**
226
     * 文字水印
227
     *
228
     * @param  string $url 图片链接
229
     * @param  string $text 文字
230
     * @param  string $font 文字字体
231
     * @param  string $fontSize 文字字号
232
     * @param  string $fontColor 文字颜色
233
     * @param  numeric $dissolve 透明度
234
     * @param  string $gravity 水印位置
235
     * @param  numeric $dx 横轴边距
236
     * @param  numeric $dy 纵轴边距
237 3
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
238 3
     * @return string
239 3
     * @author Sherlock Ren <[email protected]>
240 3
     */
241
    function waterText(
242 3
        $url,
243
        $text,
244
        $font = '黑体',
245
        $fontSize = 0,
246
        $fontColor = null,
247
        $dissolve = 100,
248
        $gravity = 'SouthEast',
249
        $dx = null,
250
        $dy = null
251
    ) {
252
253 18
        static $imageUrlBuilder = null;
254 18
        if (is_null($imageUrlBuilder)) {
255
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
256
        }
257 18
258 18
        return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args());
259 18
    }
260 18
261 18
    /**
262 18
     *  从uptoken解析accessKey和bucket
263
     *
264
     * @param $upToken
265
     * @return array(ak,bucket,err=null)
266
     */
267
    function explodeUpToken($upToken)
268
    {
269
        $items = explode(':', $upToken);
270
        if (count($items) != 3) {
271
            return array(null, null, "invalid uptoken");
272
        }
273
        $accessKey = $items[0];
274
        $putPolicy = json_decode(base64_urlSafeDecode($items[2]));
275
        $scope = $putPolicy->scope;
276
        $scopeItems = explode(':', $scope);
277
        $bucket = $scopeItems[0];
278
        return array($accessKey, $bucket, null);
279
    }
280
}
281