tp 图片上传 水印 缩略图

134 阅读1分钟
 //单图片上传
    public function singleUpload(Request $request){
        try {
            $file = $request->file('logo');
            $mineType = $file->getMime();
            #根据文件类型进行归类;
            $imageType = [
                'image/jpeg',
                'image/jpeg',
                'image/png'
            ];
            switch ($mineType) {
                case in_array($mineType,$imageType):
                    $path = Filesystem::disk('public')->putFile('image',$file);
                    break;
                case 'wokd':
                    $path = Filesystem::disk('public')->putFile('word',$file);
                    break;
                default:
                    $path = Filesystem::disk('public')->putFile('file',$file);
            }
            $data = [
                'path' => 'http://www.tptext.com/storage/'.$path
            ];
//            return success(['data'=>$path]);
            return success($data);
        }catch (FileException $fileException){
            return fail($fileException->getMessage());
        }

    }
    //多图片上传
    public function multiUpload(Request $request){
        $files = $request->file('file');
        $paths = [];
        $errors = [];
        foreach ($files as $file) {
            try {
                validate(['file'=>'fileExt:jpg,png,jpeg,doc,docx,xls,xlsx'])->check(['file'=>$file]);
                $path = Filesystem::disk('public')->putFile('image',$file);
                $paths[] = 'http://www.tptext.com/storage/'.$path;
            } catch (ValidateException $validateException) {
                $errors[]= [
                    'reason'=> $validateException->getMessage(),
                    'name'=>$file->getOriginalName()
                ];
            }
        }
        $data = [
            'path'=>$paths,
            'errors'=>$errors
        ];
        return success($data);
    }
public function enrollsAdd(Request $request){
     // 1 接受表单数据
    $param = $request->all();
    $image = \think\Image::open(\request()->file('user_img'));
    $file_name = \request()->file('user_img')->getOriginalName();
    $old_file_name = pathinfo($file_name)['basename'];
    //缩略图
    $image->thumb(60,60)->save('/storage/'.$old_file_name);
    //文字水印
    $image->text('十年',getcwd().'./static/font/font.ttf',20,'#ff0000',\think\Image::THUMB_CENTER)->save('/storage/'.$old_file_name);
    //图片水印
    $image->water('./storage/截图20220315093104.png')->save('/storage/'.$old_file_name);
    $user_img = '/storage/'.$old_file_name;

    //实例化模型 添加入库
     $model = new \app\enroll\model\Enroll();
     $model->save([
         'user_name'=>$param['user_name'],
         'user_img'=>'/'.$user_img,
         'user_sex'=>$param['user_sex'],
         'user_age'=>$param['user_age'],
         'user_title'=>$param['user_title']
     ]);
     //进入商品列表
     return redirect('/enroll/enrollList');

}