bluesky777 bluesky777 - 5 months ago 77
AngularJS Question

Upload image from ng-camera with ng-file-upload to intervention

I'm using danialfarid/ng-file-upload and bcabanes/ng-camera. This is my code (coffee):

file = $scope.vm.picture
if (file)

Upload.upload({
url: "::imagenes/store",
fields: {'Title': "test"},
file: file,
headers: {
'Accept': 'application/json;odata=verbose', 'content-type': 'image/jpeg', 'X-RequestDigest': $("#__REQUESTDIGEST").val()
}
}).success((data, status, headers)->
console.log('Complete!');
);


My navigator shows (slowly) that data were sent, but I don't know how to save that image with Laravel Intervention. Here's some code:

$file = Request::file("file");
$info = explode(".", $file->getClientOriginalName());


I don't know if I can use Request::file("file"), because it's a base64 image taken by ng-camara:

ng-camera(
capture-message="Sonrie!"
output-height="320"
output-width="426"
crop-height="320"
crop-width="426"
image-format="jpeg"
jpeg-quality="100"
action-message="Tomar foto"
snapshot="vm.picture"
flash-fallback-url="/images/webcam.swf"
shutter-url="/sounds/shutter.mp3"
style="display: inline-block")


How do I send the base64 image and how do I save it? Thank you for your help!

Answer

Well, I got it like this: (Coffee)

$scope.upload = ()->
    file = $scope.vm.picture

    if (file) 

        file = file.replace(/^data\:image\/\w+\;base64\,/, '')

        $http.post('imagenes/store', {foto: file, paciente_id: $scope.paciente.id}).then( (r)->
            toastr.success 'Uploaded correctly.'
        , (r2)->
            toastr.error 'Uploaded error', 'Error'
        )

I call the function with a button (jade):

md-button.md-raised(ng-show="vm.picture" type="button" ng-click="upload()") Save

In Laravel:

public function postStore()
{
    $folder = 'images/perfil/';

    if (Request::has('foto')) {
        $folder = 'images/perfil/';
        File::makeDirectory($folder, $mode = 0777, true, true);

        // THIS IS THE IMPORTANT!!! ------------
        $file = Request::input("foto");
        $binary_data = base64_decode( $file );
        $result = file_put_contents($folder .'/aName.jpg', $binary_data);
        // -------------------------------------

        $img = Image::make($folder . '/aName.jpg');
        $img->fit(300);
        $img->save();
    }

    return 'Saved';
}