Image source not readable in Laravel 5.2 - Intervention Image

社会主义新天地 提交于 2019-12-03 11:36:51

Shouldn't it be Image::make($file->getRealPath()) instead of Image::make('public/uploads/', $file->getRealPath())?

Image::make() doesn't seem to take two arguments, so that could be your problem.

Try this:

$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();

$file->move('uploads', $fileName);

$img = Image::make($file->getRealPath())
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());

Or if you want to do it without moving the file first, try this:

$file = Input::file('file');
$img = Image::make($file)
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());

In L5.2 its not directly possible to get image from Input facade. For that we need to first store the image on server and then give path into Image facade to do operations on image.

Code goes likes this:

if ($request->hasFile('picture') ) {

        $destinationPath = public_path('uploads/user');
        $photoname = date("YmdHis");
        $file_extention = '.'.$request->file('picture')->getClientOriginalExtension();
        $photo = $photoname.$file_extention;
        $file_check = $request->file('picture')->move($destinationPath, $photo);

        $thumb_path = $destinationPath.'/thumbnail/'.$photo;

        $new_filePath =  $destinationPath.'/'.$photo;

        $assets_path = url('uploads/user/');

        $img = Image::make($assets_path.'/'.$photo)->fit(100)->save($thumb_path,40);

        $data['picture'] = $photo;           
    }

I was looking for direct solution i.e. as it was possible previously to take image directly from Input facade. If anyone of you have direct solution, show your code here and I'll reward you this bounty. Cheers.

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

This problem occurred when you resize the image after moving it

$file->move('uploads', $fileName);

$file->getRealPath() will return false after moving the image. You need to resize the image before the moving process. That's it ;)

$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!