繁体   English   中英

Laravel图片上传验证

[英]Laravel image upload validation

我正在尝试使用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错误

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

有任何想法吗?

查看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();
}

因此,可以肯定地说,只有imagick才能使用GD驱动程序来做到这一点。

只需使用"intervention/image": "~2" 〜2 "intervention/image": "~2"或将驱动程序更改为Imagick。 GD不本地支持BMP是一个已知问题。 您可以在github上查看问题页面以获取详细信息。

为什么你不`吨使用Laravel自定义规则图像的图像

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

        ]);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM