使用ng-file-upload将图像从ng-camera上传到干预

时间:2016-05-16 18:34:27

标签: angularjs laravel intervention

我正在使用danialfarid / ng-file-upload和bcabanes / ng-camera。这是我的代码(咖啡):

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!');
    );

我的导航器(缓慢地)显示数据已发送,但我不知道如何使用Laravel Intervention保存该图像。这是一些代码:

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

我不知道我是否可以使用Request :: file(“file”),因为它是ng-camara拍摄的base64图像:

                    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")

如何发送base64图像以及如何保存?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

好吧,我这样得到了: (咖啡)

$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'
        )

我用按钮(玉)调用该函数:

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

在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';
}