Qiniu.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu
  6. {
  7. public function token($config, $upload)
  8. {
  9. $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  10. # 暂时没用到callback
  11. $policy = array(
  12. 'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
  13. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  14. );
  15. $token = $auth->uploadToken($upload['bucket'], null, 3600);
  16. $domain = 'http://up-z1.qiniup.com/';
  17. return array('qiniu', $token, $domain);
  18. }
  19. public function callback()
  20. {
  21. $body = file_get_contents('php://input');
  22. Dever::log($body, 'qiniu_callback');
  23. $body = json_decode($body, true);
  24. return $body;
  25. }
  26. # 视频转码
  27. public function convert($key, $file, $config, $upload)
  28. {
  29. $accessKey = $config['appkey'];
  30. $secretKey = $config['appsecret'];
  31. $host = $yun['host'];
  32. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  33. $bucket = $upload['bucket'];
  34. # 转码
  35. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  36. $pipeline = $upload['pipeline'];
  37. if ($pipeline) {
  38. $array = explode(',', $pipeline);
  39. $index = array_rand($array);
  40. if (isset($array[$index])) {
  41. $pipeline = $array[$index];
  42. }
  43. } else {
  44. $pipeline = 'convert';
  45. }
  46. $ext = 'mp4';
  47. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  48. $ext = 'mp3';
  49. }
  50. $force = false;
  51. //转码完成后通知到你的业务服务器。
  52. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  53. $notifyUrl = false;
  54. $config = new \Qiniu\Config();
  55. //$config->useHTTPS=true;
  56. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  57. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  58. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  59. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  60. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  61. /*
  62. echo "\n====> pfop avthumb result: \n";
  63. if ($err != null) {
  64. var_dump($err);
  65. } else {
  66. echo "PersistentFop Id: $id\n";
  67. }
  68. die;
  69. //查询转码的进度和状态
  70. list($ret, $err) = $pfop->status($id);
  71. echo "\n====> pfop avthumb status: \n";
  72. if ($err != null) {
  73. var_dump($err);
  74. } else {
  75. var_dump($ret);
  76. }*/
  77. }
  78. }