1
0

Cutimg.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\input\Argument;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use app\model\Talkskill;
  8. use app\model\UserSignLog;
  9. use app\model\Employee;
  10. use app\model\EmployeeMedal;
  11. use app\model\Medal as MedalModel;
  12. use app\model\CustomerClue;
  13. use app\model\TalkskillComment;
  14. use app\model\TrainDoneLog;
  15. use app\model\CustomerVisitLog;
  16. use app\model\VrFloder;
  17. use app\model\VrGroup;
  18. use app\model\VrView;
  19. use vr\Krpano;
  20. use OSS\OssClient;
  21. //
  22. class Cutimg extends Command
  23. {
  24. protected function configure()
  25. {
  26. $this->setName('cutimg')->addArgument('type', Argument::OPTIONAL, "type")->setDescription('cutimg');
  27. }
  28. protected function execute(Input $input, Output $output)
  29. {
  30. $vid = $input->getArgument('type');
  31. $data['view_id'] = $vid;
  32. // //场景信息
  33. $info = VrView::where('id',$data['view_id'])->field('id,vr_path,structure,vr_group_id')->findOrEmpty();
  34. // //切图中或切图完成直接结束
  35. if ($info->isEmpty() || in_array($info->cut_img_status,[4])){
  36. return false;
  37. exit;
  38. }
  39. //切图开始时间
  40. $info->cut_img_time = time();
  41. $info->save();
  42. // //原图
  43. $ali_oss_bindurl = config('app.vr_ali_oss_bindurl');
  44. $url = 'https://' . $ali_oss_bindurl . '/';
  45. $path = $info->structure.$info->vr_path.'.jpg';
  46. $info->pic_path = $url.$path;
  47. //垃圾数据
  48. if(strpos($info->pic_path,'.jpg')===false){
  49. return false;
  50. exit;
  51. }
  52. //oss文件是否存在
  53. if(!$this->ossCheck($path)){
  54. return false;
  55. exit;
  56. }
  57. // //下载原图
  58. $inputfile = 'upload/wzh/'.$info->vr_path.'.jpg';// 原图保存地址
  59. // // $measurement = '?x-oss-process=image/resize,w_100';//下载图片尺寸
  60. $measurement = '';
  61. $img = $this->downLoad($path,$inputfile);
  62. //下载原图失败
  63. if(!file_exists($inputfile)){
  64. return false;
  65. exit();
  66. }
  67. //开始切图
  68. $outputfilepath = 'upload/wzh/'.$info->vr_path.'/';//切图保存地址
  69. $krpano = new Krpano();
  70. $i = false;
  71. try {
  72. // 生成六面体的6张图
  73. $krpano->spheretocubeCube($inputfile, $outputfilepath);
  74. // 生成封面图
  75. $krpano->makepreview($inputfile, $outputfilepath);
  76. // 生成竖版预览图
  77. $krpano->spheretocubeVcube($inputfile, $outputfilepath);
  78. } catch (Exception $e) {
  79. trace($e->getMessage(), 'error');
  80. $i = true;
  81. }
  82. @unlink($inputfile);//删除本地原图
  83. $arr = ['image_b','image_d','image_f','image_l','image_r','image_u','preview','thumb'];
  84. //切图失败,可能只切成一张图
  85. if ($i) {
  86. foreach ($arr as $v1) {
  87. $img = $outputfilepath.$v1.'.jpg';//切图保存地址
  88. if(file_exists($img)) @unlink($img);////删除已经生成的切图
  89. }
  90. @rmdir('upload/wzh/'.$info->vr_path);//删除空文件夹
  91. return false;
  92. exit('切图失败');
  93. }
  94. //切图成功 上传oss
  95. $size = 0;//切图大小
  96. foreach ($arr as $v2) {
  97. //切图保存地址
  98. $img = $outputfilepath.$v2.'.jpg';
  99. $file = $info->structure.$v2.'.jpg';
  100. $res = $this->ossUpload($file,$img);
  101. if ($res) $size += filesize($img);
  102. @unlink($img);//删除本地文件
  103. }
  104. @rmdir('upload/wzh/'.$info->vr_path);//删除空文件夹
  105. $info->cut_img_status = 4;
  106. $info->cut_img_size = $size;
  107. $info->save();
  108. VrGroup::where('id',$info->vr_group_id)->inc('size',$size)->update();
  109. dataStatistics($info->root_id,'use_count',$size,'inc');//manage应用首页统计数据
  110. return true;
  111. }
  112. //下载图片
  113. private function dlfile($file_url, $save_to)
  114. {
  115. $in= fopen($file_url, "rb");
  116. $out= fopen($save_to, "wb");
  117. while ($chunk = fread($in,8192))
  118. {
  119. fwrite($out, $chunk, 8192);
  120. }
  121. fclose($in);
  122. fclose($out);
  123. }
  124. /*
  125. * oss文件上传
  126. */
  127. private function ossUpload($path, $file)
  128. {
  129. $accessKeyId = config('app.vr_ali_oss_access_key_id');
  130. $accessKeySecret = config('app.vr_ali_oss_access_key_secret');
  131. $endpoint = config('app.vr_ali_oss_end_point');
  132. $bucket = config('app.vr_ali_oss_bucket');
  133. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  134. try {
  135. $oss->uploadFile($bucket, $path, $file);
  136. } catch (OssException $e) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. /*
  142. * oss检测文件
  143. */
  144. private function ossCheck($file)
  145. {
  146. // $file = 'vr/2023/03/18/9F8E30932ECE44EB9C7FBC87A44FBD3D/9F8E30932ECE44EB9C7FBC87A44FBD3D.jpg';
  147. $accessKeyId = config('app.vr_ali_oss_access_key_id');
  148. $accessKeySecret = config('app.vr_ali_oss_access_key_secret');
  149. $endpoint = config('app.vr_ali_oss_end_point');
  150. $bucket = config('app.vr_ali_oss_bucket');
  151. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  152. try {
  153. $exist = $oss->doesObjectExist($bucket, $file);
  154. return $exist;
  155. } catch (OssException $e) {
  156. return false;
  157. }
  158. }
  159. public function downLoad($object,$downPath){
  160. // $accessKeyId = "阿里云accessKeyId";
  161. // $accessKeySecret = "阿里云accessKeySecret";
  162. // // Endpoint以杭州为例,其它Region请按实际情况填写。
  163. // $endpoint = "阿里云oss 外网地址endpoint d";
  164. // $bucket= "存储空间名称";
  165. $accessKeyId = config('app.vr_ali_oss_access_key_id');
  166. $accessKeySecret = config('app.vr_ali_oss_access_key_secret');
  167. $endpoint = config('app.vr_ali_oss_end_point');
  168. $bucket = config('app.vr_ali_oss_bucket');
  169. //下载地址
  170. $options = array(
  171. OssClient::OSS_FILE_DOWNLOAD => str_replace('/','\\',$downPath)
  172. );
  173. // 使用try catch捕获异常,如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
  174. // 使用try catch捕获异常。如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
  175. try{
  176. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  177. $ossClient->getObject($bucket, $object, $options);
  178. return true;
  179. } catch(OssException $e) {
  180. printf(__FUNCTION__ . ": FAILED\n");
  181. printf($e->getMessage() . "\n");
  182. return false;
  183. }
  184. }
  185. }