Ic23's avatar
Level 4

Image intervention not working when try to upload image

Hi, I was following a tutorial how to create a profile page with the possibility to change the avatar image in the page. But it doesn't work. I'm using laravel and Image intervention package. I have a profile blade view page like this

<div class="col-md-9 col-md-offset-3">
    <h2>Profilo di {{ $user->name }}</h2>
        <h3>Username:{{ $user->username }}</h3>
    <h3>Email: {{ $user->email }}</h3>
</div>
<div class="col-md-9 col-md-offset-3">  
<div class="image">
    <img src="/img/users/{{ $user->avatar }}" style="width:150px;">
</div>  
<form enctype="multipart/form-data" action="/profile" method="POST" >
    <label>Aggiorna immagine profilo</label>
        <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>                         
</div>

The User controller to upload the image

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// Use Auth facede
use Auth;
use Image;

class UserController extends Controller
{
    public function profile() {
        
        return view('profile', array('user'=>Auth::user()));
    }
    
    public function update_avatar(Request $request) {
        
        // Funzione per la gestione dell'upload
        if($request->Hasfile('avatar')) {
            
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->GetClientOriginalExtension();
            
            $avatar_img = Image::make($avatar);
            $avatar_img->resize(200,200)->save(public_path('/img/users/' . $filename));
            dd($avatar_img);
            $user = Auth::user();
            $filename = $user->avatar;
            $user->save();
            
            return view('profile', array('user'=>Auth::user()));
        } 
    }
}

Web route file

Route::get('/profile', 'UserController@profile');

Route::post('/profile','UserController@update_avatar');

Basically there no error displayed when I try to upload an image, and the image path is not uploaded in the database. Thanks for any help.

0 likes
3 replies
Snapey's avatar

change

        if($request->Hasfile('avatar')) {

to

        if($request->hasFile('avatar')) {
Ic23's avatar
Level 4

Thanks, but still the same issue when I try to upload.

Ic23's avatar
Level 4

Found it! There was another error.

$filename = $user->avatar;

instead of

$user->avatar = filename;

Here's the update_avatar function that works:

    public function update_avatar(Request $request) {
        
        // Funzione per la gestione dell'upload
        if($request->hasFile('avatar')) {
            
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->GetClientOriginalExtension();
            
            $avatar_img = Image::make($avatar);
            $avatar_img->resize(200,200)->save(public_path('/img/users/' . $filename));
            
            $user = Auth::user();
            $user->avatar = $filename;
            
            $user->save();
            
            return view('profile', array('user'=>Auth::user()));
        } 
    }
1 like

Please or to participate in this conversation.