tp6 保存base64图片

发布时间 2023-11-02 15:01:00作者: 现世中的素人
 public function upload()
    {
        $data = Request::param('link');
        $type = Request::param('type');
        $image_parts = explode(";base64,", $data);
        $image_type = explode("image/", $image_parts[0])[1];
        $image_data = base64_decode($image_parts[1]);
        $uniqueName = uniqid('front_', true) . '.' . $image_type;

        $relativePath  = BASE_PATH . '/upload/';
        $savePath = $relativePath . $uniqueName;
        file_put_contents($savePath, $image_data);

//        if($savePath) {
//            # 上传文件
//            $instance = ALi::setConf($type);
//            $instance->getBuilder();
//            $result = $instance->uploadOss($uniqueName, $savePath);
//
//            if (isset($result['info']['http_code']) && $result['info']['http_code'] == 200) {
//                return success(Status::SUCCESS, '操作成功', [
//                    "link" => $result['info']['url']
//                ]);
//            } else {
//                return error(Status::ERROR, '传输异常 请重试!', null);
//            }
//        }else{
//            return error(Status::ERROR, '存储失败!', null);
//        }
    }