123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace Qiniu;
- use Qiniu\Config;
- if (!defined('QINIU_FUNCTIONS_VERSION')) {
- define('QINIU_FUNCTIONS_VERSION', Config::SDK_VER);
-
- function crc32_file($file)
- {
- $hash = hash_file('crc32b', $file);
- $array = unpack('N', pack('H*', $hash));
- return sprintf('%u', $array[1]);
- }
-
- function crc32_data($data)
- {
- $hash = hash('crc32b', $data);
- $array = unpack('N', pack('H*', $hash));
- return sprintf('%u', $array[1]);
- }
-
- function base64_urlSafeEncode($data)
- {
- $find = array('+', '/');
- $replace = array('-', '_');
- return str_replace($find, $replace, base64_encode($data));
- }
-
- function base64_urlSafeDecode($str)
- {
- $find = array('-', '_');
- $replace = array('+', '/');
- return base64_decode(str_replace($find, $replace, $str));
- }
-
- function json_decode($json, $assoc = false, $depth = 512)
- {
- static $jsonErrors = array(
- JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
- JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
- JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
- JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
- JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
- );
- if (empty($json)) {
- return null;
- }
- $data = \json_decode($json, $assoc, $depth);
- if (JSON_ERROR_NONE !== json_last_error()) {
- $last = json_last_error();
- throw new \InvalidArgumentException(
- 'Unable to parse JSON data: '
- . (isset($jsonErrors[$last])
- ? $jsonErrors[$last]
- : 'Unknown error')
- );
- }
- return $data;
- }
-
- function entry($bucket, $key)
- {
- $en = $bucket;
- if (!empty($key)) {
- $en = $bucket . ':' . $key;
- }
- return base64_urlSafeEncode($en);
- }
-
- function setWithoutEmpty(&$array, $key, $value)
- {
- if (!empty($value)) {
- $array[$key] = $value;
- }
- return $array;
- }
-
- function thumbnail(
- $url,
- $mode,
- $width,
- $height,
- $format = null,
- $quality = null,
- $interlace = null,
- $ignoreError = 1
- ) {
- static $imageUrlBuilder = null;
- if (is_null($imageUrlBuilder)) {
- $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
- }
- return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args());
- }
-
- function waterImg(
- $url,
- $image,
- $dissolve = 100,
- $gravity = 'SouthEast',
- $dx = null,
- $dy = null,
- $watermarkScale = null
- ) {
- static $imageUrlBuilder = null;
- if (is_null($imageUrlBuilder)) {
- $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
- }
- return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args());
- }
-
- function waterText(
- $url,
- $text,
- $font = '黑体',
- $fontSize = 0,
- $fontColor = null,
- $dissolve = 100,
- $gravity = 'SouthEast',
- $dx = null,
- $dy = null
- ) {
- static $imageUrlBuilder = null;
- if (is_null($imageUrlBuilder)) {
- $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
- }
- return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args());
- }
-
- function explodeUpToken($upToken)
- {
- $items = explode(':', $upToken);
- if (count($items) != 3) {
- return array(null, null, "invalid uptoken");
- }
- $accessKey = $items[0];
- $putPolicy = json_decode(base64_urlSafeDecode($items[2]));
- $scope = $putPolicy->scope;
- $scopeItems = explode(':', $scope);
- $bucket = $scopeItems[0];
- return array($accessKey, $bucket, null);
- }
- }
|