I have a file upload in my vue js component which sends base64 in server
methods: {
onFileChange(e) {
console.log(e.target.files[0]);
let fileReader = new FileReader();
fileReader.readAsDataURL(e.target.files[0]);
fileReader.onload = (e) => {
this.product.cover_image = e.target.result
};
},
<div class="form-group">
<label for="exampleInputFile">Upload Image of Product</label>
<input type="file" ref="fileupload" v-on:change="onFileChange" id="exampleInputFile">
</div>
and in my controller in laravel im using image intervention to save the image via Image::make
public function store(Request $request){
$this->validate($request, [
'name' => 'required|max:255',
'price' => 'required|numeric',
]);
$image = $request->get('cover_image');
$name = time().'.' . explode('/', explode(':', substr($image, 0, strpos($image, ';')))[1])[1];
Image::make($request->get('cover_image'))->save(public_path('cover_images/').$name);
$product = new Product;
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->cover_image = $name;
if($product->save()) {
return new ProductsResource($product);
}
}
how can i validate the image before saving? it is in base64 i dont know how to validate it on laravel.
If you only want to validate uploaded file to be an image type:
Laravel 5.6 image validation rule
Inside the
AppServiceProvider
i put the custom validationand on the
validation.php
i put thenow i can use this in validating the request
credits to https://medium.com/@jagadeshanh/image-upload-and-validation-using-laravel-and-vuejs-e71e0f094fbb