functions.php 7.7 KB

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