<?php
namespace app\api\controller\v1;

use app\hander\HelpHander;
use think\Controller;
use think\Db;
use think\Exception;

class Upload extends Controller
{

    public function upfile(){
        $file = request()->file('uploadFile');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
            ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'file');
        if($info){
            $img = '/uploads/file/' . $info->getSaveName();
            $img = str_replace('\\', '/', $img);
            HelpHander::success(['path' => request()->domain(true).$img]);
        }else{
            // 上传失败获取错误信息
            HelpHander::error($file->getError());
        }
    }

    public function upimg(){ // 只能上传规定的图片,添加水印,时间 地点 人物 模块
        $file = request()->file('uploadFile');
        // 移动到框架应用根目录/uploads/ 目录下
        $config = config('max_upload_img');
        $info = $file->validate($config)
            ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'file');
        if($info){
            $img = '/uploads/file/' . $info->getSaveName();
            $img = str_replace('\\', '/', $img);

            try{
                $this->thumb($img);
                $image = \think\Image::open('.'.$img);
                $width = $image->width();
                $height = $image->height();

                $size = 100;
                $curSize = round($size*$width/2000);
                $h = 100;
                $curH = round($h*$width/2000);

                $address = input('address', '', 'trim');
                $mode = input('mode', '', 'trim');
                $userId = input('userId/d', 0);
                $uname = Db::name('user')->where('id', $userId)->value('real_name');
                $font = env('root_path') . 'public/static/hylht.ttf';

                $i = 3;
                if($address){
                    $i = 4;
                }

                $arr1 = $this->getSize(date('Y-m-d H:i:s'),$curSize,$width,$height,$i,$curH);
                $arr2 = $this->getSize($mode,$curSize,$width,$height,$i-1,$curH);
                if($address){
                    $arr3 = $this->getSize($address,$curSize,$width,$height,2,$curH);
                }
                $arr4 = $this->getSize($uname,$curSize,$width,$height,1,$curH);
                $image->text(date('Y-m-d H:i:s'), $font, $curSize, '#ffffff', $arr1);
                $image->text($mode, $font, $curSize, '#ffffff', $arr2);
                if($address){
                    $image->text($address, $font, $curSize, '#ffffff', $arr3);
                }
                $image->text($uname, $font, $curSize, '#ffffff', $arr4)->save('.' . $img);
            }catch (\Exception $e){
                trace('图片上传失败:'.$e->getMessage());
                HelpHander::error('图片上传失败');
            }

            HelpHander::success(['path' => request()->domain(true).$img]);
        }else{
            // 上传失败获取错误信息
            HelpHander::error($file->getError());
        }
    }

    private function thumb($img){
        $image = \think\Image::open('.'.$img);
        $width = $image->width();
        $height = $image->height();
        if($width > 2000||$height > 2000){
            $image->thumb(2000,2000,\think\Image::THUMB_SCALING)->save('.' . $img);
        }
    }

    private function getSize($text,$size,$weight,$height,$i=1,$ch=100,$angle=0){
        //获取文字信息
        $font = env('root_path').'public/static/hylht.ttf';
        $info = imagettfbbox($size, $angle, $font, $text);
        $minx = min($info[0], $info[2], $info[4], $info[6]);
        $maxx = max($info[0], $info[2], $info[4], $info[6]);
        $miny = min($info[1], $info[3], $info[5], $info[7]);
        $maxy = max($info[1], $info[3], $info[5], $info[7]);
        /* 计算文字初始坐标和尺寸 */
        $x = $minx;
        $y = abs($miny);
        $w = $maxx - $minx;
        $h = $maxy - $miny;

        $xx = ($weight - $w) / 2;
        $yy = $height - ($h+$ch)*$i;

        return [$xx,$yy];
    }

    public function uply(){
        trace($_FILES);
        $file = request()->file('filedata1');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->validate([ 'ext' => 'wav' ])
            ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'luyin');
        if($info){
            $img = '/uploads/luyin/' . $info->getSaveName();
            $img = str_replace('\\', '/', $img);
            $img = request()->domain(true).$img;
            trace($img);
            HelpHander::success(['path' => request()->domain(true).$img]);
        }else{
            // 上传失败获取错误信息
            HelpHander::error($file->getError());
        }
    }
}