<?php
declare (strict_types=1);
namespace app\admin\controller\v1;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 15:05
// +----------------------------------------------------------------------
use app\Request;
use app\admin\controller\AuthController;
use library\utils\Qiniu;
use think\Image;

class Upload extends AuthController{


    public function index(Request $request){
        $file =$request->file('file');
            $isZ = $request->post('isz',1);
        if(empty($file)) {
            return app('json')->fail("未上传文件");
        }
        $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
        if($isZ == 1) {
            $image_size = @getimagesize($rootTmp);
            if ($image_size[0] > 1000) {
                $imgS = Image::open($rootTmp);
                $imgS->thumb(1000, $image_size[1]);
                $imgS->save($rootTmp);
            } else {
                if ($image_size[1] > 1000) {
                    $imgS = Image::open($rootTmp);
                    $imgS->thumb($image_size[0], 1000);
                    $imgS->save($rootTmp);
                }
            }
        }
        $qiniu = new  Qiniu;
        $img_url = $qiniu->updateFile('img', $rootTmp, $rootTmp);
        if(empty($img_url['url'])){
            return app('json')->fail( $qiniu->getError());
        }

        return app('json')->success(['img'=>$img_url['url']]);
    }

}