I'm using Laravel 5.3
,
I crop a image with javascript and upload it with ajax.
The cropped image is changed to a blob file when uploading.
But in backend,How to check the blob file is a image?
public function changeAvatar(Request $request)
{
$user = \Auth::user();
$blob = $request->croppedImage;
$destinationPath = 'images/uploads/';
$fileName = $user->id . '_' . time() . '.png';
$file = file_put_contents($destinationPath.$fileName, $blob);
$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()
]);
}
$user ->avatar = '/'.$destinationPath.$fileName;
$user ->save();
return \Response::json([
'success'=>true,
'avatar'=>asset($destinationPath.$fileName),
]);
}
the file is a image,but the error is always like this:
{"success":false,"errors":{"image":["image must be an image."]}}