<?php
namespace app\mgrapi\controller;
use app\common\RetUtil;
use think\Controller;
use think\Exception;
use think\facade\Env;
class Upload extends Controller
{
public function upload(){
$file=request()->file('file');
if(!$file){
throw new Exception('上传文件不能为空');
}
//获取图片上传路径
$path=$file->move(Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'文件目录');
//生成缩略图
if($path){
$info_thumb=Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'文件目录'.DIRECTORY_SEPARATOR.$path->getSaveName();
}
$pathinfo=pathinfo($info_thumb);
$image = \think\Image::open($info_thumb);
$thumbPath=$pathinfo['dirname'].DIRECTORY_SEPARATOR.$pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
if ($image->height()>150 && $image->width()>150){
$image->thumb(150,150)->save($thumbPath);
}else{
$image->thumb($image->width(),$image->height())->save($thumbPath);
}
$save=pathinfo($path->getSaveName());
$thumbPath=DIRECTORY_SEPARATOR.'文件目录'.DIRECTORY_SEPARATOR.$save['dirname'].DIRECTORY_SEPARATOR.$pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
return RetUtil::succ('操作成功', [
'fileurl' => DIRECTORY_SEPARATOR .'文件目录'.DIRECTORY_SEPARATOR.$path->getSaveName(),
'thumb' =>$thumbPath
]);
}
}