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

后端 未结 8 1734
灰色年华
灰色年华 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  );
    }
    
    0 讨论(0)
  • 2021-02-05 07:59
    $file = base64_decode($request['image']);
            $folderName = 'public/uploads/';
            $safeName = str_random(10).'.'.'png';
            $destinationPath = public_path() . $folderName;
            $success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
            print $success;
    
    0 讨论(0)
  • 2021-02-05 07:59

    This is an easy mistake.

    You are using public_path incorrectly. It should be:

    $path = public_path() . "/img/designs/" . $png_url;
    

    Also, I would avoid your method of sending the image. Look at a proper upload in a form and use Laravel's Input::file method.

    0 讨论(0)
  • 2021-02-05 08:01

    what am i doing is using basic way

    $file = base64_decode($request['profile_pic']);
                $folderName = '/uploads/users/';
                $safeName = str_random(10).'.'.'png';
                $destinationPath = public_path() . $folderName;
                file_put_contents(public_path().'/uploads/users/'.$safeName, $file);
    
               //save new file path into db
                $userObj->profile_pic = $safeName;
            }
    
    0 讨论(0)
  • 2021-02-05 08:06

    I'v done it!!

    I replaced $data->base64_image to $_POST['base64_image'] and then use $result = file_put_contents($path, $image); instead of Image::make($image->getRealPath())->save($path);

    But this doesn't look like a laravel ways. I you have another way that look more elegant please tell me!

    0 讨论(0)
  • 2021-02-05 08:13

    Actually, Input::all() returns an array of inputs so you have following:

    $data = Input::all();
    

    Now your $data is an array not an object so you are trying to access the image as an object like:

    $data->base64_image
    

    So, it's not working. You should try using:

    $image = $data['base64_image'];
    

    Since it's (base64_image) accessible from $_POST then Input::file('base64_image') won't work because Input::file('base64_image') checks the $_FILES array and it's not there in your case.

    0 讨论(0)
提交回复
热议问题