代码之家  ›  专栏  ›  技术社区  ›  Neel

Laravel 5干预中的图像验证

  •  21
  • Neel  · 技术社区  · 10 年前

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

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

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

    有人能告诉我正确的方向吗。。

    5 回复  |  直到 10 年前
        1
  •  42
  •   Neel    9 年前

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

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

    处理干预前验证程序检查 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');
        };
     });
    

    希望这有帮助。

        2
  •  16
  •   Rahul Hirve    8 年前

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

    $this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);
    
        3
  •  4
  •   venkatskpi    7 年前

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

    可读图像格式取决于所选驱动程序(GD或 Imagick)和本地配置。默认干预图像 当前支持以下主要格式。

        JPEG PNG GIF TIF BMP ICO PSD WebP
    

    GD----*

    Imagick公司*

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

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

    注:( Intervention Image是一个开源的PHP图像处理和操作库 http://image.intervention.io/ ). 此库不验证任何验证规则,由Larval完成 验证程序类

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

    提示1: (请求验证)

    $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
    

    提示2: (控制器验证)

       $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
    
        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }
    
        4
  •  2
  •   Alex Semenov    9 年前

    我有custum形式,这个变体不起作用。所以我使用了正则表达式验证

    这样地:

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

    可能会对某人有帮助

        5
  •  0
  •   Mohammad Ali Abdullah    5 年前
     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!!']);
            }
        }
    }
    
    推荐文章