rob_utopano's avatar

How to upload a mobile captured image into laravel in base64 format?

Hey guys,

I want to capture a picture on my website with the camera from the client (usually smartphone). It´s important that the picture is not stored locally and that the picture can be uploaded to the database in base64 format.

Is there any suggestion, you can give me? It would be really nice if someone can give me an code example, cause my trys do not operate right.

Thanks to all who can help!

0 likes
10 replies
Snapey's avatar

Its just a string. Just make sure the column is large enough in the database.

rob_utopano's avatar

@snapey Yeah thats clear, but I have a problem with the upload

This is a snippet of my view:

<form method="post" action="{{url('upload')}}" enctype="multipart/form-data">
      {{csrf_field()}}
        <div class="input-group control-group increment" >
          <input type="file" name="image" accept="image/*" capture>
        </div>
        <button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
</form>        

And this is my controller:

    public function store(Request $request)
    {
        $this->validate($request, [
                'image' => 'required'
        ]);

        $pic= new Picture();
        $pic->image=$request->image;
        $pic->save();
        
        return back()->with('success', '');
    }

I am getting the error that the picture input is not filled... So what can I do to fix this?

rob_utopano's avatar

@snapey thanks for the link

But if I try to upload the photo, I am getting an error from laravel, which says that image can not be null (in database) which indicates that there is no photo which was uploaded or not?

chrisgrim's avatar

In your Model do you have protected $guarded = []; ?

jlrdw's avatar

Why don't you upload like any other image and just store the image name in the database.

There's nothing special about an image taken from a mobile.

Optimize your images for the web.

chrisgrim's avatar

Often when I can't get my requests through it's because of a mass assignment issue. Try adding protected $guarded = [];

rob_utopano's avatar

@jlrdw this is a special requirement which is definitely not arguably cause this website is just for upload, it will not display any images. The data which was uploaded will run through different processes (the website is just the input-gateway) and will not be stored on the website, why they must be stored as base64.

Snapey's avatar

what does your controller look like now.

dd($request()->all) in your controller to check what has been uploaded.

Also, in the controller, substitute the upload for a simple string and check it gets persisted in the database

Please or to participate in this conversation.