phplaravel-4webcam.js

Store base64-encoded string to specified folder


Good day! I just want to put the image captured on the specified folder, not on the database itself via ajax call. This is what I've done:

JS File:

$('#btn-save-snapshot').on('click', function() {

        Webcam.snap(function(data_uri)
        {

            $.ajax({
                url: '/capture',
                type: 'POST',
                data: 
                {
                    captimage: data_uri
                },
                success: function(data)
                {
                    alert('Saved!');
                },
                error: function(xhr)
                {
                    alert($.parseJSON(xhr.responseText)['error']['message']);
                }
            });
        });
    });

I don't know how to save it to a specified folder. I already used Input::file('captimage')->move($destination) on controller but it doesn't work.


Solution

  • You only get back a string, not the image itself (normally base64-format). So what you have to do, is sending the information to a seperate php file, and then use file_put_contents to save the image. Before saving, you have to remove the prefix.

    So you could do something like that: (if you get back a base64-string):

    file_put_contents('/capture/img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST['captImage'])));
    

    If it's not base64, please post the result, so the content of your captImage-variable.