laravel - Laravel 5干预中的图像验证

标签 laravel laravel-5 laravel-validation intervention laravel-filesystem

我已经在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类:

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


希望这可以帮助。

关于laravel - Laravel 5干预中的图像验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893439/

相关文章:

php - 使用 Laravel 5.5 如何通过迁移复制表中的列?

通配符 (*) 点表示法的 Laravel 自定义验证消息

php - 'where' 中的 Laravel 数字比较不起作用

php - 如何在 Laravel 中获取平均时间

Laravel Auth::guard ('api' )->user() 返回 null

php - 如何在 Laravel 中将表列值与 Auth::user->id 进行比较

php - 拉维尔 5 : Middleware before & after into routes

php - 在 Laravel 中,如何让 "with"返回一个 bool 值而不是对象,如果存在则返回 true,如果不存在则返回 false

laravel - undefined variable : errors in Laravel

php - 有没有办法在自定义\Illuminate\Contracts\Validation\Rule 中使用参数