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 待计算校验码的字符串 |
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
|
105 |
|
$find = array('+', '/'); |
49
|
105 |
|
$replace = array('-', '_'); |
50
|
105 |
|
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
|
3 |
|
$find = array('-', '_'); |
63
|
3 |
|
$replace = array('+', '/'); |
64
|
3 |
|
return base64_decode(str_replace($find, $replace, $str)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Wrapper for JSON decode that implements error detection with helpful |
69
|
|
|
* error messages. |
70
|
|
|
* |
71
|
|
|
* @param string $json JSON data to parse |
72
|
|
|
* @param bool $assoc When true, returned objects will be converted |
73
|
|
|
* into associative arrays. |
74
|
|
|
* @param int $depth User specified recursion depth. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
* @throws \InvalidArgumentException if the JSON cannot be parsed. |
78
|
|
|
* @link http://www.php.net/manual/en/function.json-decode.php |
79
|
|
|
*/ |
80
|
|
|
function json_decode($json, $assoc = false, $depth = 512) |
81
|
|
|
{ |
82
|
|
|
static $jsonErrors = array( |
83
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
84
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
85
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
86
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
87
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
88
|
81 |
|
); |
89
|
|
|
|
90
|
81 |
|
if (empty($json)) { |
91
|
15 |
|
return null; |
92
|
|
|
} |
93
|
72 |
|
$data = \json_decode($json, $assoc, $depth); |
94
|
|
|
|
95
|
72 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
96
|
|
|
$last = json_last_error(); |
97
|
|
|
throw new \InvalidArgumentException( |
98
|
|
|
'Unable to parse JSON data: ' |
99
|
|
|
. (isset($jsonErrors[$last]) |
100
|
|
|
? $jsonErrors[$last] |
101
|
|
|
: 'Unknown error') |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
72 |
|
return $data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 计算七牛API中的数据格式 |
110
|
|
|
* |
111
|
|
|
* @param $bucket 待操作的空间名 |
112
|
|
|
* @param $key 待操作的文件名 |
113
|
|
|
* |
114
|
|
|
* @return string 符合七牛API规格的数据格式 |
115
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/data-formats.html |
116
|
|
|
*/ |
117
|
|
|
function entry($bucket, $key) |
118
|
|
|
{ |
119
|
33 |
|
$en = $bucket; |
120
|
33 |
|
if (!empty($key)) { |
121
|
33 |
|
$en = $bucket . ':' . $key; |
122
|
33 |
|
} |
123
|
33 |
|
return base64_urlSafeEncode($en); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* array 辅助方法,无值时不set |
128
|
|
|
* |
129
|
|
|
* @param $array 待操作array |
130
|
|
|
* @param $key key |
131
|
|
|
* @param $value value 为null时 不设置 |
132
|
|
|
* |
133
|
|
|
* @return array 原来的array,便于连续操作 |
134
|
|
|
*/ |
135
|
|
|
function setWithoutEmpty(&$array, $key, $value) |
136
|
|
|
{ |
137
|
12 |
|
if (!empty($value)) { |
138
|
3 |
|
$array[$key] = $value; |
139
|
3 |
|
} |
140
|
12 |
|
return $array; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* 缩略图链接拼接 |
145
|
|
|
* |
146
|
|
|
* @param string $url 图片链接 |
147
|
|
|
* @param int $mode 缩略模式 |
148
|
|
|
* @param int $width 宽度 |
149
|
|
|
* @param int $height 长度 |
150
|
|
|
* @param string $format 输出类型 |
151
|
|
|
* @param int $quality 图片质量 |
152
|
|
|
* @param int $interlace 是否支持渐进显示 |
153
|
|
|
* @param int $ignoreError 忽略结果 |
154
|
|
|
* @return string |
155
|
|
|
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html |
156
|
|
|
* @author Sherlock Ren <[email protected]> |
157
|
|
|
*/ |
158
|
|
|
function thumbnail( |
159
|
|
|
$url, |
|
|
|
|
160
|
|
|
$mode, |
|
|
|
|
161
|
|
|
$width, |
|
|
|
|
162
|
|
|
$height, |
|
|
|
|
163
|
|
|
$format = null, |
|
|
|
|
164
|
|
|
$quality = null, |
|
|
|
|
165
|
|
|
$interlace = null, |
|
|
|
|
166
|
|
|
$ignoreError = 1 |
|
|
|
|
167
|
|
|
) { |
168
|
|
|
static $imageUrlBuilder = null; |
169
|
|
|
if (is_null($imageUrlBuilder)) { |
170
|
|
|
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 图片水印 |
178
|
|
|
* |
179
|
|
|
* @param string $url 图片链接 |
180
|
|
|
* @param string $image 水印图片链接 |
181
|
|
|
* @param numeric $dissolve 透明度 |
182
|
|
|
* @param string $gravity 水印位置 |
183
|
|
|
* @param numeric $dx 横轴边距 |
184
|
|
|
* @param numeric $dy 纵轴边距 |
185
|
|
|
* @param numeric $watermarkScale 自适应原图的短边比例 |
186
|
|
|
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html |
187
|
|
|
* @return string |
188
|
|
|
* @author Sherlock Ren <[email protected]> |
189
|
|
|
*/ |
190
|
|
|
function waterImg( |
191
|
|
|
$url, |
|
|
|
|
192
|
|
|
$image, |
|
|
|
|
193
|
|
|
$dissolve = 100, |
|
|
|
|
194
|
|
|
$gravity = 'SouthEast', |
|
|
|
|
195
|
|
|
$dx = null, |
|
|
|
|
196
|
|
|
$dy = null, |
|
|
|
|
197
|
|
|
$watermarkScale = null |
|
|
|
|
198
|
|
|
) { |
199
|
|
|
static $imageUrlBuilder = null; |
200
|
|
|
if (is_null($imageUrlBuilder)) { |
201
|
|
|
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* 文字水印 |
209
|
|
|
* |
210
|
|
|
* @param string $url 图片链接 |
211
|
|
|
* @param string $text 文字 |
212
|
|
|
* @param string $font 文字字体 |
213
|
|
|
* @param string $fontSize 文字字号 |
214
|
|
|
* @param string $fontColor 文字颜色 |
215
|
|
|
* @param numeric $dissolve 透明度 |
216
|
|
|
* @param string $gravity 水印位置 |
217
|
|
|
* @param numeric $dx 横轴边距 |
218
|
|
|
* @param numeric $dy 纵轴边距 |
219
|
|
|
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark |
220
|
|
|
* @return string |
221
|
|
|
* @author Sherlock Ren <[email protected]> |
222
|
|
|
*/ |
223
|
|
|
function waterText( |
224
|
|
|
$url, |
|
|
|
|
225
|
|
|
$text, |
|
|
|
|
226
|
|
|
$font = '黑体', |
|
|
|
|
227
|
|
|
$fontSize = 0, |
|
|
|
|
228
|
|
|
$fontColor = null, |
|
|
|
|
229
|
|
|
$dissolve = 100, |
|
|
|
|
230
|
|
|
$gravity = 'SouthEast', |
|
|
|
|
231
|
|
|
$dx = null, |
|
|
|
|
232
|
|
|
$dy = null |
|
|
|
|
233
|
|
|
) { |
234
|
|
|
static $imageUrlBuilder = null; |
235
|
|
|
if (is_null($imageUrlBuilder)) { |
236
|
|
|
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.