Qiniu.php 5.1 KB

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