Show your form and how you're uploading this file. It's hard to tell exactly what you're doing from the supplied code. AFAIK, you can't use mime types for validation unless it is an actual file uploaded, like from <input type="file">
May 20, 2018
7
Level 3
Validation Blob Image
Help me, guys. I upload image in blob data to the server. And I'd like to know how to validate such data like image retrived from post-request. I've written this code, but it didn't work out for me:
$sliceContent = explode(',', $request->img['blob']);
$blobData = base64_decode($sliceContent[1]);
$imageFile = imagecreatefromstring($blobData);
$validateImages = Validator::make(['img' => $request->img['blob']], [
'img' => 'required|mimes:jpg,jpeg,png,bmp|max:300'
], [
'img.required' => 'Картинка должна быть загружена!',
'img.mimes' => 'Картинка должна соответствовать одному из следующих форматов: JPEG, JPG, PNG, BMP!',
'img.max' => 'Картинка не должна превышать размер в 300кб!'
])->validate();
Level 67
See if this helps: https://medium.com/@jagadeshanh/image-upload-and-validation-using-laravel-and-vuejs-e71e0f094fbb
They had to make their own custom validation rule, but it shows how. Seems like a lot of extra work when you can just upload a regular file.
public function boot()
{
Validator::extend('image64', function ($attribute, $value, $parameters, $validator) {
$type = explode('/', explode(':', substr($value, 0, strpos($value, ';')))[1])[1];
if (in_array($type, $parameters)) {
return true;
}
return false;
});
Validator::replacer('image64', function($message, $attribute, $rule, $parameters) {
return str_replace(':values',join(",",$parameters),$message);
});
}
Please or to participate in this conversation.