namespace app\mobile\controller;

use app\common\controller\Base;
/**

  • Class Upload
  • @package app\api\controller 统一文件上传类
    */
    class Upload extends Base
    {
    /**

    • 上传单个文件
    • [filesOne description]
    • @param [type] $imgs [description]
    • @return [type] [description]
      /
      public function filesOne($max_filesize=8388608,$time='1',$size='348
      470',$max_filetime='30'){
      $type=Request()->param('type');
      $files=$_FILES['fileName'];
      //var_dump($files);exit;
      if (!empty($files)) {
      if ($files['error'] == 0) {
      $ext = strtolower(pathinfo($files['name'],PATHINFO_EXTENSION));
      $imagesExt=['jpeg','png','jpg','gif'];
      $videosExt=['mp4','avi','wmv','video'];
      $audiosExt=['mp3','audio'];
      $imgpath='';
      if(in_array($ext,$imagesExt)){
      $path = ROOT_PATH . 'public' . DS . 'uploads'.DS .'image' .DS . $type . DS;
      }elseif (in_array($ext,$videosExt)){
      $path = ROOT_PATH . 'public' . DS . 'uploads'.DS .'video' .DS . $type . DS;
      }elseif (in_array($ext,$audiosExt)){
      $path = ROOT_PATH . 'public' . DS . 'uploads'.DS .'music' .DS . $type . DS;
      }else{
      $ret = returnArr(-1,'','文件类型有误');
      return json($ret);
      }
      if (!is_dir($path)){
      mkdir($path,0777,true);
      }
      // 生成唯一的文件名
      $fileName = md5(uniqid(microtime(true),true)).'.'.$ext;
      // 将文件名拼接到指定的目录下
      $destName = $path.$fileName;
      if($files['size'] <= $max_filesize){
      // $file = move_uploaded_file($files['tmp_name'], $path); //将临时文件转移至规定位置
      if(in_array($ext,$videosExt)){
      $coverName=md5(uniqid(microtime(true),true)).'.jpg';
      // ffmpeg -i E:\phpstudy_pro\WWW\weilv\public\uploads\video\1\142883ac94d5eb5820c4b13b9263a66ec.mp4 -ss 1 -f image2 E:\phpstudy_pro\WWW\weilv\public\uploads\video\1\d99496887efed5587683a3e5e877ff24.jpg
      // $str = "ffmpeg -i " . $destName . " -y -f mjpeg -ss " . $time . " -t 0.001 -s $size " . $path . $coverName;
      // $str = "ffmpeg -i ".$destName." -ss 1 -f image2 " .$path.$coverName;
      $times=get_video_times($files['tmp_name']);//00:00:24
      $seconds=getSeconds($times);//24
      if($seconds > $max_filetime){ //时长判断
      $ret = returnArr(-3,'','上传的文件不能大于30秒');
      return json($ret);
      }
      // 进行文件移动
      if (!move_uploaded_file($files['tmp_name'],$destName)){
      $ret = returnArr(-2,'','文件上传失败');
      return json($ret);
      }
      $imgpath=get_img_by_video($destName, $path.$coverName,$size,$time);//截取视频第一秒第一帧
      }
      $msg = array(
      'code' => 1,
      'ext' => $ext,
      'path' => $destName,
      'filename'=> $fileName,
      'coverName'=> $imgpath,
      'time'=> $seconds,
      // 'ctime'=> $ctime,
      );
      $ret = returnArr(1,$msg,'文件上传成功');
      }elseif($files['size'] > $max_filesize){
      $ret = returnArr(-3,'','上传的文件不能大于8M');

          }else{
              $ret = returnArr(-3,'','上传文件异常');
      
          }
      }else{
          $ret = returnArr(-3,'','上传文件异常');
      
      }

      }else{
      $ret = returnArr(-3,'','上传文件异常');

      }
      return json($ret);
      }

    /**

    • 上传多个文件
    • [filesUp description]
    • @param [type] $imgs [description]
    • @return [type] [description]
      */
      public function filesUp(){
      $imgurl = [];
      $imgs=$_FILES['fileName'];
      $type=Request()->param('type');
      if (count($imgs['name']) > 0) {
      for ($i=0; $i < count($imgs['name']); $i++) {
      $arr[$i]['name'] = $imgs['name'][$i];
      $arr[$i]['type'] = $imgs['type'][$i];
      $arr[$i]['tmp_name'] = $imgs['tmp_name'][$i];
      $arr[$i]['error'] = $imgs['error'][$i];
      $arr[$i]['size'] = $imgs['size'][$i];
      }
      foreach ($arr as $key => $value) {
      if ($value['error'] == 0) {
      $ext = strtolower(pathinfo($value['name'],PATHINFO_EXTENSION));
      $path = ROOT_PATH . 'public' . DS . 'uploads'.DS .'image' .DS . $type . DS;
      if (!is_dir($path)){
      mkdir($path,0777,true);
      }
      // 生成唯一的文件名
      $fileName = md5(uniqid(microtime(true),true)).'.'.$ext;
      // 将文件名拼接到指定的目录下
      $destName = $path."/".$fileName;
      //var_dump($path);exit;
      if (!move_uploaded_file($value['tmp_name'],$destName)){
      $ret = returnArr(-2,'','文件上传失败');
      return json($ret);
      }
      $imgurl[] = $destName;//json_encode
      }
      }
      }
      $data=json_encode($imgurl);
      $ret = returnArr(1,$data,'文件上传成功');
      return json($ret);

    }

    /**

    • 时分秒转换成秒
    • @param $string
    • @return float|int
      /
      function getSeconds($string)
      {
      $arr = explode(':', $string);
      $len = count($arr);
      return (int)$arr[$len - 1] + (int)$arr[$len - 2]
      60 + (($len > 2 ? (int)$arr[$len - 3] : 0) 60 60);
      }

                 /**获取视频缩略图
    • $videopath : 视频绝对地址
    • $imgpath : 缩略图保存地址
    • $time : 截取第几帧为缩略图
      */
      function get_img_by_video($videopath, $imgpath,$size, $time = 1,$ffempeg_path='E:\apizz\ffmpeg-20190202-6dc06e9-win32-shared\bin\ffmpeg.exe')
      {

      if (!file_exists($videopath)) return false;
      if (strpos(PHP_OS, 'WIN') !== false) { //windows系统
          $str = "{$ffempeg_path} -i {$videopath} -y -f mjpeg -ss 3 -t {$time} -s {$size} {$imgpath}";
         system($str);
          return $imgpath;
      }

      }

            /**
    • 获取视频总时长
    • @param string $ffmpeg_path [ffmpeg.exe的绝对路径]
    • @param string $video_path [video的绝对路径]
      */
      function get_video_times($video_path, $ffmpeg_path = 'E:\apizz\ffmpeg-20190202-6dc06e9-win32-shared\bin\ffmpeg.exe')
      {

      if (!file_exists($video_path)){
          $ret=returnArr(-1,'','视频文件不存在');
          return $ret;
      } //return ['status' => 0, 'msg' => '视频文件不存在'];
      
      $times = false;
      $commond = "{$ffmpeg_path} -i {$video_path} 2>&1";
      exec($commond, $str_res, $str_r);
      
      if (is_array($str_res)) {
          foreach ($str_res as $v) {
              if (strpos($v, 'Duration') !== false) {
                  $times = substr($v, stripos($v, '.') - 8, 8);//'  Duration: 00:24:28.14, start: 0.000000, bitrate: 486 kb/s'
                  break;
              }
          }
      }

      // $ret= $times ? returnArr(1,$times,'cg') :returnArr(1,'','获取视频时间失败');
      return $times;
      //return $times ? ['status' => 1, 'msg' => $times] : ['status' => 0, 'msg' => '获取视频时间失败'];
      }

}