Upload.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace common\util;
  3. use common\components\AjaxException;
  4. class Upload
  5. {
  6. public static function dealWithEventSourceDataFile($accident_id)
  7. {
  8. //******先存储图片,返回完整路径******//
  9. $dir = "../web/uploads/case/" . $accident_id; //设定存储上传文件的文件夹
  10. if (!is_dir($dir)) {
  11. mkdir($dir, 0777, TRUE); //如果文件夹目录不存在则创建
  12. }
  13. $fileName = $_FILES["file"]["name"]; //得到上传文件的名字
  14. $oldPath = $dir . "/" . $fileName; // 路径+旧文件名
  15. if ($_FILES['file']['size'] > 1073741824) {
  16. throw new AjaxException("文件大小{$_FILES['file']['size']}已超出1G限制!"); //通知前端图片大小非法
  17. }
  18. //生成文件名
  19. $path = "/uploads/case/" . $accident_id . "/" . rawurlencode($fileName);
  20. //完成上传
  21. move_uploaded_file($_FILES['file']['tmp_name'], $oldPath); // 上传
  22. //文件转码
  23. $env = require __DIR__ . '/../config/env/currentEnv.php';
  24. if (in_array($env, ["product"])) {
  25. /**
  26. * ffmpeg -i 111.wmv -c:v libx264 2.mp4
  27. * ffmpeg -i input.file -codec:a libmp3lame output.mp3
  28. */
  29. $type = explode(".", $fileName);
  30. if (strtolower($type[count($type) - 1]) == "avi" || strtolower($type[count($type) - 1]) == "wmv" || strtolower($type[count($type) - 1]) == "mkv") {
  31. $newFileName = "";
  32. foreach ($type as $typeValue) {
  33. if ($typeValue != $type[count($type) - 1]) {
  34. $newFileName .= $typeValue . ".";
  35. }
  36. }
  37. $newFileName .= "mp4";
  38. $oriPath = __DIR__ . "/../../frontend/web/uploads/case/" . $accident_id . "/" . $fileName;
  39. $newPath = __DIR__ . "/../../frontend/web/uploads/case/" . $accident_id . "/" . $newFileName;
  40. $result = shell_exec("/usr/local/ffmpeg/bin/ffmpeg -i '$oriPath' -c:v libx264 '$newPath' 2>&1");
  41. $pathNew = "/uploads/case/" . $accident_id . "/" . rawurlencode($newFileName);
  42. return [$newFileName, $pathNew];
  43. } else if (strtolower($type[count($type) - 1]) == "wav") {
  44. $newFileName = "";
  45. foreach ($type as $typeValue) {
  46. if ($typeValue != $type[count($type) - 1]) {
  47. $newFileName .= $typeValue . ".";
  48. }
  49. }
  50. $newFileName .= "mp3";
  51. $oriPath = __DIR__ . "/../../frontend/web/uploads/case/" . $accident_id . "/" . $fileName;
  52. $newPath = __DIR__ . "/../../frontend/web/uploads/case/" . $accident_id . "/" . $newFileName;
  53. $result = shell_exec("/usr/local/ffmpeg/bin/ffmpeg -i '$oriPath' -codec:a libmp3lame '$newPath' 2>&1");
  54. $pathNew = "/uploads/case/" . $accident_id . "/" . rawurlencode($newFileName);
  55. return [$newFileName, $pathNew];
  56. }
  57. }
  58. //返回数据
  59. return [$fileName, $path];
  60. }
  61. }