ajax - How to verify that a blob file is an image in laravel?
仅有的幸福
仅有的幸福 2017-05-16 16:49:55
0
3
737

If the form is submitted in the normal way, there will be no problem with background verification:

        $file = $request->avatar;
        $input = array('image' => $file);
        $rules = array(
            'image' => 'image'
        );
        $validator = \Validator::make($input, $rules);
        if ( $validator->fails() ) {
            return \Response::json([
                'success' => false,
                'errors' => $validator->getMessageBag()->toArray()
            ]);

        }

However, when submitted using the formData object, the image file is converted into a blob file and cannot be verified:

        $('#uploadAvatar').on('click', function (e) {

            $('#uploadAvatar').html('正在保存...');
            $("#image").cropper('getCroppedCanvas').toBlob(function (blob) {
                var formData = new FormData();

                formData.append('croppedImage', blob);

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });


                $.ajax({
                    type: "POST",
                    url: "{{ url('/avatar') }}",
                    processData: false,
                    contentType: false,
                    cache: false,
                    data: formData
                }).done(function (response) {
                    showResponse(response);
                }).fail(function (data) {
                    alert('提交失败,请尝试重新提交');
                });

            });

        });

How can the background verify that the blob file is an image?

仅有的幸福
仅有的幸福

reply all(3)
过去多啦不再A梦

It’s very simple. Wouldn’t it be nice to make a temporary variable and restore it?
$file = file_put_contents('/path/to/new/file_name', $blob);

phpcn_u1582

Extended validation rulesgetimagesize

大家讲道理

You can use base64 streaming

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!