123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace app\command;
- use think\console\Command;
- use think\console\input\Argument;
- use think\console\Input;
- use think\console\Output;
- use app\model\Talkskill;
- use app\model\UserSignLog;
- use app\model\Employee;
- use app\model\EmployeeMedal;
- use app\model\Medal as MedalModel;
- use app\model\CustomerClue;
- use app\model\TalkskillComment;
- use app\model\TrainDoneLog;
- use app\model\CustomerVisitLog;
- use app\model\VrFloder;
- use app\model\VrGroup;
- use app\model\VrView;
- use vr\Krpano;
- use OSS\OssClient;
- //
- class Cutimg extends Command
- {
- protected function configure()
- {
- $this->setName('cutimg')->addArgument('type', Argument::OPTIONAL, "type")->setDescription('cutimg');
- }
- protected function execute(Input $input, Output $output)
- {
- $vid = $input->getArgument('type');
- $data['view_id'] = $vid;
- // //场景信息
- $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 false;
- exit;
- }
- //切图开始时间
- $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;
- //垃圾数据
- if(strpos($info->pic_path,'.jpg')===false){
- return false;
- exit;
- }
- //oss文件是否存在
- if(!$this->ossCheck($path)){
- return false;
- exit;
- }
- // //下载原图
- $inputfile = 'upload/wzh/'.$info->vr_path.'.jpg';// 原图保存地址
- // // $measurement = '?x-oss-process=image/resize,w_100';//下载图片尺寸
- $measurement = '';
- $img = $this->downLoad($path,$inputfile);
-
- //下载原图失败
- if(!file_exists($inputfile)){
- return false;
- exit();
- }
- //开始切图
- $outputfilepath = '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;
- }
- @unlink($inputfile);//删除本地原图
- $arr = ['image_b','image_d','image_f','image_l','image_r','image_u','preview','thumb'];
- //切图失败,可能只切成一张图
- if ($i) {
- foreach ($arr as $v1) {
- $img = $outputfilepath.$v1.'.jpg';//切图保存地址
- if(file_exists($img)) @unlink($img);////删除已经生成的切图
- }
- @rmdir('upload/wzh/'.$info->vr_path);//删除空文件夹
- return false;
- exit('切图失败');
- }
- //切图成功 上传oss
- $size = 0;//切图大小
- foreach ($arr as $v2) {
- //切图保存地址
- $img = $outputfilepath.$v2.'.jpg';
- $file = $info->structure.$v2.'.jpg';
- $res = $this->ossUpload($file,$img);
- if ($res) $size += filesize($img);
- @unlink($img);//删除本地文件
- }
- @rmdir('upload/wzh/'.$info->vr_path);//删除空文件夹
-
- $info->cut_img_status = 4;
- $info->cut_img_size = $size;
- $info->save();
- VrGroup::where('id',$info->vr_group_id)->inc('size',$size)->update();
- dataStatistics($info->root_id,'use_count',$size,'inc');//manage应用首页统计数据
- 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)
- {
- $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 {
- $oss->uploadFile($bucket, $path, $file);
- } catch (OssException $e) {
- return false;
- }
- return true;
- }
- /*
- * 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;
- }
- }
- public function downLoad($object,$downPath){
- // $accessKeyId = "阿里云accessKeyId";
- // $accessKeySecret = "阿里云accessKeySecret";
- // // Endpoint以杭州为例,其它Region请按实际情况填写。
- // $endpoint = "阿里云oss 外网地址endpoint d";
-
- // $bucket= "存储空间名称";
- $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');
- //下载地址
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => str_replace('/','\\',$downPath)
- );
- // 使用try catch捕获异常,如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
- // 使用try catch捕获异常。如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
- try{
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
-
- $ossClient->getObject($bucket, $object, $options);
- return true;
- } catch(OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return false;
- }
- }
- }
|