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

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

What I dont understand is, how does intervention process the validation for the uploaded image? I mean, does intervention already has the inbuild image validation check in it or is that something I need to manually add using Laravel Validation to check for the file formats, file sizes, etc..? I have read through the intervention docs and I couldnt find info on how image validation works when using intervention with laravel.

Can someone point me in the right direction please..

推荐答案

感谢@maytham为我指出了正确的方向.

我发现图像干预本身不会进行任何验证.所有图像验证都必须在将其传递给图像干预进行上传之前完成.由于Laravel内置了imagemime类型的验证器,这使得图像验证非常容易.这就是我现在所拥有的,在将文件输入传递给图像干预之前,我首先验证文件输入.

Validator Check Before Processing 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');
    };
 });

希望这能帮上忙.

Laravel相关问答推荐

如何重置laravel ui密码

如何使用 Laravel 进行继承

如何在 Laravel 中将 Select 选项表单插入数据库

Laravel查询多对多关系

向 Laravel 模型查询添加计算字段

所有 POST 请求上的 Laravel 4 CSRF

使用 Amazon S3 的 Storage::get() 返回 false

Laravel PHP 框架的新手. /以外的路由不起作用

Carbon现在时间错了

为什么人们将 .env 放入 gitignore?

如何在 Laravel 中为模型命名,以免与现有类发生冲突?

Laravel 5 - 多对多 - Attach versus Save

WhereNotExists Laravel Eloquent

Laravel 说 Auth guard [] 没有定义

Laravel:命令未找到

如何清理 Laravel Bootstrap 缓存配置文件?

如何在 Laravel 5 中的每个相关输入字段旁边显示验证错误?

Laravel/PHPUnit:断言 json 元素存在而不定义值

Laravel Eloquent 多对多查询 whereIn

Laravel 4 控制器模板/Blade - 正确的方法?