laravel图像验证图像干预 [英] laravel image validation image intervention

查看:94
本文介绍了laravel图像验证图像干预的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vue js组件中上传了一个文件,该文件在服务器中发送了base64

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>

并在我的laravel im控制器中使用图像干预通过Image::make

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);
     }
  }

在保存之前如何验证图像?它在base64中,我不知道如何在laravel上对其进行验证.

how can i validate the image before saving? it is in base64 i dont know how to validate it on laravel.

推荐答案

AppServiceProvider里面,我放置了自定义验证

Inside the AppServiceProvider i put the custom validation

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);
    });
}

,然后在validation.php上放

'image64' => 'The :attribute must be a file of type: :values.',

现在我可以用它来验证请求

now i can use this in validating the request

'image' => 'required|image64:jpeg,jpg,png'

https://medium.com/@jagadeshanh/image-upload-and-validation-using-laravel-and-vuejs-e71e0f094fbb

这篇关于laravel图像验证图像干预的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆