Laravel 5 干预中的图像验证

Image Validation in Laravel 5 Intervention

我已经在 Laravel 5.1 中安装了 intervention,我正在使用图像上传和调整大小,如下所示:

Route::post('/upload', function()
{
Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
});

我不明白的是,干预如何处理上传图片的验证?我的意思是,干预是否已经在其中进行了内置图像验证检查,还是我需要使用 Laravel Validation 手动添加的内容来检查文件格式、文件大小等?我已经通读了干预文档,但找不到有关使用 laravel 进行干预时图像验证如何工作的信息。

有人能给我指出正确的方向吗..

感谢@maytham 的评论为我指明了正确的方向。

我发现图像干预本身不做任何验证。所有图像验证都必须在将其传递给图像干预以进行上传之前完成。感谢 Laravel 的内置验证器,如 imagemime 类型,这使得图像验证变得非常容易。这就是我现在所拥有的,我首先验证文件输入,然后再将其传递给 Image Intervention。

处理干预前验证器检查Image Class:

 Route::post('/upload', function()
 {
    $postData = $request->only('file');
    $file = $postData['file'];

    // Build the input for validation
    $fileArray = array('image' => $file);

    // Tell the validator that this file should be an image
    $rules = array(
      'image' => 'mimes:jpeg,jpg,png,gif|required|max:10000' // max 10000kb
    );

    // Now pass the input and rules into the validator
    $validator = Validator::make($fileArray, $rules);

    // Check to see if validation fails or passes
    if ($validator->fails())
    {
          // Redirect or return json to frontend with a helpful message to inform the user 
          // that the provided file was not an adequate type
          return response()->json(['error' => $validator->errors()->getMessages()], 400);
    } else
    {
        // Store the File Now
        // read image from temporary file
        Image::make($file)->resize(300, 200)->save('foo.jpg');
    };
 });

希望对您有所帮助。

我有自定义表单,但此变体不起作用。所以我使用正则表达式验证

像这样:

  client_photo' => 'required|regex:/^data:image/'

可能对某人有帮助

简单地说,将其集成到得到验证

$this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);

图像支持的格式 http://image.intervention.io/getting_started/formats

The readable image formats depend on the chosen driver (GD or Imagick) and your local configuration. By default Intervention Image currently supports the following major formats.

    JPEG PNG GIF TIF BMP ICO PSD WebP

GD ✔️ ✔️ ✔️ - - - - ✔️ *

Imagick ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ *

  • 对于 WebP 支持,GD 驱动程序必须与 PHP 5 >= 5.5.0 或 PHP 7 一起使用才能使用 imagewebp()。如果使用 Imagick,则必须使用 libwebp 进行编译以支持 WebP。

查看make方法的文档,了解如何从不同来源读取图像格式,分别编码和保存以了解如何输出图像。

NOTE: (Intervention Image is an open source PHP image handling and manipulation library http://image.intervention.io/). This library does not validate any validation Rules, It was done by Larval Validator class

Laravel 文档 https://laravel.com/docs/5.7/validation

技巧一:(请求验证)

$request->validate([
   'title' => 'required|unique:posts|max:255',
   'body' => 'required',
   'publish_at' => 'nullable|date',
]); 

// Retrieve the validated input data...
$validated = $request->validated(); //laravel 5.7

技巧二:(控制器验证)

   $validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }
 public function store(Request $request)
    {
        $room = 1;
        if ($room == 1) {
            //image upload start
            if ($request->hasfile('photos')) {
                foreach ($request->file('photos') as $image) {
                    // dd($request->file('photos'));
                    $rand = mt_rand(100000, 999999);
                    $name = time() . "_"  . $rand . "." . $image->getClientOriginalExtension();
                    $name_thumb = time() . "_"  . $rand . "_thumb." . $image->getClientOriginalExtension();
                    //return response()->json(['a'=>storage_path() . '/app/public/postimages/'. $name]);
                    //move image to postimages folder
                    //dd('ok');
                    //  public_path().'/images/
                    $image->move(public_path() . '/postimages/', $name);
                    // 1280
                    $resizedImage = Image::make(public_path() . '/postimages/' . $name)->resize(300, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
             
                    // save file as jpg with medium quality
                    $resizedImage->save(public_path() . '/postimages/' . $name, 60);
                    // $resizedImage_thumb->save(public_path() . '/postimages/' . $name_thumb, 60);
                    $data[] = $name;
                   
                    //insert into picture table

                    $pic = new Photo();
                    $pic->name = $name;
                    $room->photos()->save($pic);
                }
            }
            return response()->json(['success' => true, 'message' => 'Room Created!!'], 200);
        } else {
            return response()->json(['success' => false, 'message' => 'Error!!']);
        }
    }
}