【问题标题】:Laravel image upload validationLaravel 图片上传验证
【发布时间】:2017-07-25 20:35:53
【问题描述】:

我正在尝试使用 laravel 验证来验证上传的文件,但遇到了问题。

这是我的代码:

$this->validate($request, [
        'image' =>'mimetypes:image/jpeg,image/png,image/gif',

        ]);

        $avatar = $request->file('image');

        $fileName = time(). '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300,300)->save( public_path('uploads/avatar/' . $fileName));

        $user = Auth::user();
        $user->avatar = $fileName;
        $user->save();

问题是当我使用 bmp 文件时,我收到此错误: Gd error

我正在使用干预图像包。我宁愿不切换到 imagick 驱动程序。

有什么想法吗?

【问题讨论】:

  • 我认为您必须在图像数组中声明 BMP mime。像这样:'image' =>'mimetypes:image/jpeg,image/png,image/gif,image/bmp',
  • 您使用的是哪个干预镜像包版本?

标签: php laravel validation laravel-5


【解决方案1】:

查看Intervention包代码,可以看到processBmp函数的两种实现:

干预/图像/Gd/Encoder.php:

protected function processBmp()
{
    throw new \Intervention\Image\Exception\NotSupportedException(
        "BMP format is not supported by Gd Driver."
    );
}

干预/图像/Imagick/Encoder.php:

protected function processBmp()
{
    $format = 'bmp';
    $compression = \Imagick::COMPRESSION_UNDEFINED;
    $imagick = $this->image->getCore();
    $imagick->setFormat($format);
    $imagick->setImageFormat($format);
    $imagick->setCompression($compression);
    $imagick->setImageCompression($compression);
    return $imagick->getImagesBlob();
}

所以我认为可以肯定地说,您不能使用 GD 驱动程序,只能使用 imagick。

【讨论】:

    【解决方案2】:

    只需使用"intervention/image": "~2" 或将您的驱动程序更改为 Imagick。 GD 本身不支持 BMP 是一个已知问题。详情可以查看issue page on github

    【讨论】:

      【解决方案3】:

      为什么你不为图像image 使用 Laravel 自定义规则?

      $this->validate($request, [
              'image' =>'image',
      
              ]);
      

      【讨论】:

        【解决方案4】:

        希望此解决方案能解决您的错误,请尝试以下逻辑

         public function postUpload(Request $request)
            {
        
            $input = $request->all();
            $rules = array(
              'uploadFile' => 'image|max:8000'
            );
        
            $validation = Validator::make($input, $rules);
        
            if ($validation->fails())
            {
              return array(
                  'validation_failed' => true,
                  'errors'            => $validation->errors()->toArray()
              );
            }
        
            $file = $request->uploadFile;
            $destinationPath = 'uploads/img';
        
            // Get real extension according to mime type
            $ext = $file->extension();
            // Hash processed file name, including the real extension
                $hashname           = date('H.i.s').'-'.md5($request->_token).'.'.$ext;
                $upload_success     = $request->uploadFile->storeAs($destinationPath, $hashname);
        
        
                Image::configure(array('driver' => 'imagick'));
                $img = Image::make(storage_path() . '/app/uploads/img/' . $hashname);
                $img->resize(230, null, function ($constraint) {
                        $constraint->aspectRatio();
                });
                $img->save(storage_path() . '/app/uploads/lowres/' .$hashname ,80);
        
                $user_image = new User_images();
                $user_image->id_user = Auth::user()->id;
                $user_image->handler = $hashname;
                $user_image->save();
        
        
                return array('status' => 'success','message'=> 'Image has been uploaded successfully','file_path'=>'/uploads/'.$hashname);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-15
          • 2015-03-13
          • 2015-09-14
          • 2017-05-28
          • 2022-12-09
          • 1970-01-01
          • 1970-01-01
          • 2014-12-06
          相关资源
          最近更新 更多