123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace app\jobs;
- use think\queue\Job;
- use think\facade\Console;
- use app\model\VrFloder;
- use app\model\VrGroup;
- use app\model\VrView;
- use vr\Krpano;
- use OSS\OssClient;
- use think\facade\Log;
- class CutImgOne
- {
- public function fire(Job $job, $queuedata)
- {
- // Log::close();
- try {
- if($job->attempts() > 1) {
- //通过这个方法可以检查这个任务已经重试了几次了
- $job->delete();
- }else{
- $res = $this->jobDone($queuedata);
- if($res){
- $job->delete();
- }else{
- VrView::where('id',$queuedata['view_id'])->update(['cut_img_status'=>3,'cut_img_ing'=>0]);
- }
- }
- } catch (OssException $e) {
- //报错直接结束
- VrView::where('id',$queuedata['view_id'])->update(['cut_img_status'=>3,'cut_img_ing'=>0]);
- $job->delete();
- }
- }
- public function failed($queuedata)
- {
- // ...任务达到最大重试次数后,失败了
- }
- //切图
- //VrView::where('id',23)->update(['cut_img_size'=>0]);
- private function jobDone($data){
- $t = microtime(true);
- $vid = $data['view_id'];
- VrView::where('id',$vid)->update(['cut_img_size'=>1]);
- // Log::close();
- // //场景信息
- $info = VrView::where('id',$data['view_id'])->field('id,vr_path,structure,vr_group_id')->findOrEmpty();
- // //切图中或切图完成直接结束
- if ($info->isEmpty() || in_array($info->cut_img_status,[4])) return true;
- VrView::where('id',$vid)->update(['cut_img_size'=>12]);
- // //切图开始时间
- $info->cut_img_time = time();
- $info->save();
- // //原图
- $ali_oss_bindurl = config('app.vr_ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/';
- $path = $info->structure.$info->vr_path.'.jpg';
- $info->pic_path = $url.$path;
- VrView::where('id',$vid)->update(['cut_img_size'=>123]);
- //垃圾数据
- if(strpos($info->pic_path,'.jpg')===false) return false;
- //oss文件是否存在
- if(!$this->ossCheck($path)) return false;
- VrView::where('id',$vid)->update(['cut_img_size'=>1234]);
- // //下载原图
- $inputfile = 'public/upload/wzh/'.$info->vr_path.'.jpg';// 原图保存地址
- // // $measurement = '?x-oss-process=image/resize,w_100';//下载图片尺寸
- $measurement = '';
- $this->dlfile($info->pic_path.$measurement,$inputfile);
- //下载原图失败
- if(!file_exists($inputfile)) return false;
- VrView::where('id',$vid)->update(['cut_img_size'=>12345]);
- //开始切图
- $outputfilepath = 'public/upload/wzh/'.$info->vr_path.'/';//切图保存地址
- $krpano = new Krpano();
- $i = false;
- try {
- // 生成六面体的6张图
- $krpano->spheretocubeCube($inputfile, $outputfilepath);
- // 生成封面图
- $krpano->makepreview($inputfile, $outputfilepath);
- // 生成竖版预览图
- $krpano->spheretocubeVcube($inputfile, $outputfilepath);
- } catch (Exception $e) {
- trace($e->getMessage(), 'error');
- $i = true;
- }
- VrView::where('id',$vid)->update(['cut_img_size'=>123456]);
- @unlink($inputfile);//删除本地原图
- $arr = ['image_b','image_d','image_f','image_l','image_r','image_u','preview','thumb'];
- $res = 0;
- foreach ($arr as $key => $value) {
- $res++;
- }
- return $res;
- echo '-';
- //切图失败,可能只切成一张图
- if ($i) {
- foreach ($arr as $v1) {
- $img = $outputfilepath.$v1.'.jpg';//切图保存地址
- if(file_exists($img)) @unlink($img);////删除已经生成的切图
- }
- @rmdir('public/upload/wzh/'.$info->vr_path);//删除空文件夹
- return false;
- }
- VrView::where('id',$vid)->update(['cut_img_size'=>1234567]);
- //切图成功 上传oss
- $size = 0;//切图大小
- VrView::where('id',$vid)->update(['logo_path'=>'','mask'=>'','hotspot'=>'','hlookat'=>'']);
-
- $oss = $this->ossObject();
- $bucket = config('app.vr_ali_oss_bucket');
- foreach ($arr as $v2) {
- $view = VrView::where('id',$vid)->find();
- VrView::where('id',$vid)->update(['logo_path'=>$view->logo_path.','.$v2]);
- //切图保存地址
- $img = $outputfilepath.$v2.'.jpg';
- $file = $info->structure.$v2.'.jpg';
-
- $t1 = microtime(true);
- $tt = $t1 - $t;
- VrView::where('id',$vid)->update(['hlookat'=>$tt]);
- $res = $this->ossUpload($file,$img,$oss,$bucket);
- VrView::where('id',$vid)->update(['mask'=>$view->mask.','.$img]);
- VrView::where('id',$vid)->update(['hotspot'=>$view->hotspot.','.$file]);
- $str = $res ? '1' : '0';
- if ($res) $size += filesize($img);
- @unlink($img);//删除本地文件
- }
- @rmdir('public/upload/wzh/'.$info->vr_path);//删除空文件夹
- VrView::where('id',$vid)->update(['cut_img_size'=>12345678]);
- $info->cut_img_status = 4;
- $info->cut_img_size = $size;
- $info->save();
- VrGroup::where('id',$info->vr_group_id)->inc('size',$size)->update();
- return true;
- }
- //下载图片
- private function dlfile($file_url, $save_to)
- {
- $in= fopen($file_url, "rb");
- $out= fopen($save_to, "wb");
- while ($chunk = fread($in,8192))
- {
- fwrite($out, $chunk, 8192);
- }
- fclose($in);
- fclose($out);
- }
- /*
- * oss文件上传
- */
- private function ossUpload($path, $file,$oss,$bucket)
- {
- try {
- $oss->uploadFile($bucket, $path, $file);
- } catch (OssException $e) {
- return false;
- }
- return true;
- }
- /*
- 0,单个vr头部分享
- 1,分享单个vr
- 2,侧边栏电话
- * 获取oss对象
- */
- private function ossObject()
- {
- $accessKeyId = config('app.vr_ali_oss_access_key_id');
- $accessKeySecret = config('app.vr_ali_oss_access_key_secret');
- $endpoint = config('app.vr_ali_oss_end_point');
- $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
- return $oss;
- }
- /*
- * oss检测文件
- */
- private function ossCheck($file)
- {
- // $file = 'vr/2023/03/18/9F8E30932ECE44EB9C7FBC87A44FBD3D/9F8E30932ECE44EB9C7FBC87A44FBD3D.jpg';
- $accessKeyId = config('app.vr_ali_oss_access_key_id');
- $accessKeySecret = config('app.vr_ali_oss_access_key_secret');
- $endpoint = config('app.vr_ali_oss_end_point');
- $bucket = config('app.vr_ali_oss_bucket');
- $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
- try {
- $exist = $oss->doesObjectExist($bucket, $file);
- return $exist;
- } catch (OssException $e) {
- return false;
- }
- }
- }
|