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?
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);
Extended validation rules
getimagesize
You can use base64 streaming