codyjbutz's avatar

User Avatars in L5

Hello all,

I was wondering what the best way to go about implementing Avatars to users in Laravel 5? I want to be able to have users with Avatars but the only frameworks I have seen for this are for L4.

Any help would be useful.

Thanks!

0 likes
9 replies
codyjbutz's avatar

@Ruffles I guess I should have stated this project would be on a Intranet that has no access to the Internet at times. So Gravatar approach will not work, I need to host the files on the server, prreferably using the Laravel Filesystem.

SCC's avatar
SCC
Best Answer
Level 7

I have just implemented this on my site. It was a fairly straight forward process. as a quick run through of what I did. Just heading out so apologies if this is short and sweet.

You will need to expand your user table to include a field for the avatar path. Then I have a form that allows the users to have an upload option like this on their profile form.

http://imgur.com/T9WV9E3

I have a method in my User model to update for example (not the tidiest, just finished it a couple of days ago).

It should give you a good idea. I am using the intervention package to manipulate the images, for an Avatar you will likely need to resize it as I have done rather than expect the user to upload the right size as I was intending.

Essentially, upload the image using a form and then move it into the right directory, place the image name in the user table record. I have a settings area where the $img_path is stored so it can be changed rather than hard coded.

I also check whether there is an existing image and delete it from the directory first.

Again, apologies just running out but I thought it may give you a start, any questions drop them below and I will help if I can.

public function update($id)
{
    $img_path = Settings::findOrFail('avatar_path')->value;

    // To begin we will assign all the inputs we can to the database record, I have not used form model binding as        
    there is 
    // image manipulation and role changes that are done separately.

    $user = User::findOrFail($id);
    $user->username = Input::get('username');
    $user->name = Input::get('name');
    $user->email = Input::get('email');
    $user->gender = Input::get('gender');
    $user->country = Input::get('country');
    $user->website = Input::get('website');
    $user->bio = Input::get('bio');
    $user->roles()->sync(Input::get('userrole', []));

    //Check for and store user image

    if (Request::HasFile('imag'))
    {
        //  Before uploading a new image we will check if one already exists and delete it first.

        if ($user->imag != null)
        {
            $old_image = $user->imag;

            unlink(sprintf(public_path() . $img_path . '%s', $old_image));
        }

        //  Next we will get the image to be uploaded, rename it so as to be unique, save and then alter as required.

        

        $file = Request::file('imag');
                    
        $image_name = time() . '-' . $file->getClientOriginalName();

        $file->move(public_path() . $img_path, $image_name);

        $image_alter = Image::make(sprintf(public_path() . $img_path . '%s', $image_name))->resize(75, 75)->save();

        $user->imag = $image_name; // Note we add the image path to the databse field before the save.

    }

        //  Finally save the updated record to the database and return to the users view.

    $user->save();

    return redirect('/admin/users');
}
2 likes
tgif's avatar

I noticed that mysql field has an image datatype. Would it be a good idea to save the image to database? Maybe that would eliminate the need to check if an image exists.

noob

bobbybouwmann's avatar

@csuarez You never place a complete image in your database! To much data. Just place the destination of the file in your database and get the path when you need it ;)

1 like
vlass's avatar

im also want to add a new photo

Please or to participate in this conversation.