Laravel: Save Base64 .png file to public folder from controller

后端 未结 8 1729
灰色年华
灰色年华 2021-02-05 07:46

I send a png image file to controller in base64 via Ajax. I\'ve already test and sure that controller has received id but still can\'t save it to public folder.

8条回答
  •  隐瞒了意图╮
    2021-02-05 07:50

    My solution is:

    public function postTest() {
            $data = Input::all();
    
            //get the base-64 from data
            $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);
    
            //decode base64 string
            $image = base64_decode($base64_str);
            Storage::disk('local')->put('imgage.png', $image);
            $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
            echo $storagePath.'imgage.png'; 
            $response = array(
                'status' => 'success',
            );
            return Response::json( $response  );
    }
    

提交回复
热议问题