【问题标题】:Image Validation in Laravel 5 InterventionLaravel 5 干预中的图像验证
【发布时间】:2015-10-31 19:49:07
【问题描述】:

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

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

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

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

【问题讨论】:

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


【解决方案1】:

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

我发现图像干预本身不会进行任何验证。所有图像验证都必须在传递给图像干预以进行上传之前完成。感谢 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');
    };
 });

希望这会有所帮助。

【讨论】:

  • 'photo' 是从哪里来的 $file = Input::file('photo');
  • @Chriz74 是表单中的输入名称
  • 为什么不使用 $file = $request->photo; ?
  • 好点@Chriz74 用$request 更新了代码,而不是使用Input:: 在表单中输入名称。
  • 如果我想获得图像的最大尺寸怎么办?
【解决方案2】:

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

像这样:

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

可能对某人有帮助

【讨论】:

    【解决方案3】:

    简单地说,将其集成以获取验证

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

    【讨论】:

      【解决方案4】:

      图片支持的格式 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();
          }
      

      【讨论】:

        【解决方案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!!']);
                }
            }
        }
        

        【讨论】:

        • ** 必须使用 1. 使用命令作曲家需要干预/图像。 2.使用Intervention\Image\ImageManagerStatic作为Image;
        猜你喜欢
        • 2019-01-16
        • 1970-01-01
        • 2016-01-11
        • 2018-01-11
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 2015-10-10
        相关资源
        最近更新 更多