sebas2201
sebas2201

Reputation: 155

Image source not readable with Laravel Image Intervention

So this has been a problem for me for a while now and it just doesn't seem to work, I have a form where users can submit a new profile picure:

<form enctype="multipart/form-data" action="/profile" method="POST">
    <input type="file" name="avatar">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="submit" class="pull-right btn btn-sm btn-primary">
</form>

When the button is clicked they are being redirected to the controller which activates the following function:

public function update_avatar(Request $request)
{
    $avatar = $request->avatar;
    $extension = File::extension($avatar);
    $filename = time() . '.' . $extension;
    Image::make($avatar)->resize(350, 350)->save( public_path('/uploads/avatars/' . $filename ) );

    $user = Sentinel::getUser();
    $user->profile->avatar = $filename;
    $user->save();
}

But whatever I do the controller always returns the message

(1/1) NotReadableException

Image source not readable

Does anyone know how I can fix this?

EDIT: I've changed my Image::make line to the following:

$image = Image::make($avatar->getRealPath())->resize(350, 350)->save( public_path('/uploads/avatars/' . $filename ) );

But now I am encountering a new error:

(1/1) FatalErrorException

Call to a member function getRealPath() on string

Upvotes: 1

Views: 8952

Answers (1)

OuailB
OuailB

Reputation: 1119

Try this instead :

Image::make($avatar->getRealPath())->resize(350, 350)->save( public_path('/uploads/avatars/' . $filename ) );

Edit: use this, to retrieve your UploadedFile

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

Edit #2: For the extension use this instead :

$extension = $avatar->getClientOriginalExtension();

Note: Do not forget to authorize only images files in your validation rules.

Intead, you can "hardcode" the extension, Intervention is able to save & convert into the specified image format (jpg, png ..)

Upvotes: 1

Related Questions