Qiniu.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu
  6. {
  7. private $client;
  8. private $token;
  9. public function token($config, $upload)
  10. {
  11. $cur = time();
  12. if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  13. $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  14. # 暂时没用到callback
  15. $policy = array(
  16. 'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
  17. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  18. );
  19. $token = $auth->uploadToken($upload['bucket'], null, 3600);
  20. $up['token'] = $token;
  21. $up['token_endtime'] = $cur + 3600 - 60;
  22. $up['where_id'] = $config['id'];
  23. Dever::db('upload/yun')->update($up);
  24. } else {
  25. $token = $config['token'];
  26. }
  27. $domain = 'http://up-z1.qiniup.com/';
  28. return array('qiniu', $token, $domain, $upload['bucket']);
  29. }
  30. public function callback()
  31. {
  32. $body = file_get_contents('php://input');
  33. Dever::log($body, 'qiniu_callback');
  34. $body = json_decode($body, true);
  35. return $body;
  36. }
  37. # 连接
  38. public function connect($config, $upload)
  39. {
  40. list($type, $token, $domain, $bucket) = $this->token($config, $upload);
  41. $this->token = $token;
  42. // 构建 UploadManager 对象
  43. $this->client = new \Qiniu\Storage\UploadManager();
  44. return $this;
  45. }
  46. # 上传文件
  47. public function upload($file, $source_file, $options = array(), $base64 = false)
  48. {
  49. if (!$this->client) {
  50. return array();
  51. }
  52. $method = 'putFile';
  53. if ($base64) {
  54. $method = 'put';
  55. }
  56. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  57. return $ret;
  58. }
  59. # 视频转码
  60. public function convert($key, $file, $config, $upload)
  61. {
  62. $accessKey = $config['appkey'];
  63. $secretKey = $config['appsecret'];
  64. $host = $yun['host'];
  65. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  66. $bucket = $upload['bucket'];
  67. # 转码
  68. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  69. $pipeline = $upload['pipeline'];
  70. if ($pipeline) {
  71. $array = explode(',', $pipeline);
  72. $index = array_rand($array);
  73. if (isset($array[$index])) {
  74. $pipeline = $array[$index];
  75. }
  76. } else {
  77. $pipeline = 'convert';
  78. }
  79. $ext = 'mp4';
  80. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  81. $ext = 'mp3';
  82. }
  83. $force = false;
  84. //转码完成后通知到你的业务服务器。
  85. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  86. $notifyUrl = false;
  87. $config = new \Qiniu\Config();
  88. //$config->useHTTPS=true;
  89. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  90. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  91. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  92. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  93. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  94. /*
  95. echo "\n====> pfop avthumb result: \n";
  96. if ($err != null) {
  97. var_dump($err);
  98. } else {
  99. echo "PersistentFop Id: $id\n";
  100. }
  101. die;
  102. //查询转码的进度和状态
  103. list($ret, $err) = $pfop->status($id);
  104. echo "\n====> pfop avthumb status: \n";
  105. if ($err != null) {
  106. var_dump($err);
  107. } else {
  108. var_dump($ret);
  109. }*/
  110. }
  111. # 视频截图
  112. public function cover()
  113. {
  114. //vframe/jpg/offset/7/w/480/h/360
  115. }
  116. }