Qiniu.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 = '')
  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 = '')
  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 upload($file, $source_file, $options = array(), $base64 = false)
  49. {
  50. if (!$this->client) {
  51. return array();
  52. }
  53. $method = 'putFile';
  54. if ($base64) {
  55. $method = 'put';
  56. }
  57. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  58. return $ret;
  59. }
  60. # 视频转码
  61. public function convert($key, $file, $config, $upload)
  62. {
  63. $accessKey = $config['appkey'];
  64. $secretKey = $config['appsecret'];
  65. $host = $yun['host'];
  66. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  67. $bucket = $upload['bucket'];
  68. # 转码
  69. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  70. $pipeline = $upload['pipeline'];
  71. if ($pipeline) {
  72. $array = explode(',', $pipeline);
  73. $index = array_rand($array);
  74. if (isset($array[$index])) {
  75. $pipeline = $array[$index];
  76. }
  77. } else {
  78. $pipeline = 'convert';
  79. }
  80. $ext = 'mp4';
  81. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  82. $ext = 'mp3';
  83. }
  84. $force = false;
  85. //转码完成后通知到你的业务服务器。
  86. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  87. $notifyUrl = false;
  88. $config = new \Qiniu\Config();
  89. //$config->useHTTPS=true;
  90. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  91. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  92. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  93. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  94. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  95. /*
  96. echo "\n====> pfop avthumb result: \n";
  97. if ($err != null) {
  98. var_dump($err);
  99. } else {
  100. echo "PersistentFop Id: $id\n";
  101. }
  102. die;
  103. //查询转码的进度和状态
  104. list($ret, $err) = $pfop->status($id);
  105. echo "\n====> pfop avthumb status: \n";
  106. if ($err != null) {
  107. var_dump($err);
  108. } else {
  109. var_dump($ret);
  110. }*/
  111. }
  112. # 视频截图
  113. public function cover($key, $video, $num = 1, $local = 2)
  114. {
  115. $file = $video . '?vframe/jpg/offset/' . $num;
  116. if ($local == 1) {
  117. $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
  118. return $data['url'] . '?time=' . time();
  119. } else {
  120. return $file;
  121. }
  122. //vframe/jpg/offset/7/w/480/h/360
  123. }
  124. }