Image source not readable in Laravel 5.2 - Intervention Image

前端 未结 6 1628
孤独总比滥情好
孤独总比滥情好 2021-02-07 18:27

I have a small problem concerning the resizing process of a given image, I am trying to submit a form containing an input type -->file<-- I was able to upload a picture witho

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 18:35

    Uploading a file and resizing it before saving is as easy as that: (Without validation or checks)

    You can directly pass an instance of UploadedFile to InterventionImage::make()

    public function upload(Request $request)
    {
        $file = $request->file('file');
    
        $filename = $file->getClientOriginalName();
    
        $img = \Image::make($file);
        $img->resize(320, 240)->save(public_path('uploads/'.$filename))
    
    }
    

    If you want to save original size and resized image:

        $img->save(public_path('uploads/'.$filename))
            ->resize(320, 240)
            ->save(public_path('uploads/thumb_'.$filename));
    

    This was just tested on currently latest 5.2 version with is 5.2.45

    [EDIT:]

    If you call

    $file->move();
    

    Don't use

    $file->getRealPath() 
    

    afterwards, because this will return false after calling move()

        $filename = $file->getClientOriginalName();
        $file->move('uploads', $filename);
        dd($file->getRealPath());
    

提交回复
热议问题