One way is to use Gravatar's API (like this website uses) to fetch pictures from there. There's a lesson which is part of the series Let's Build Larabook called Gravatars And View Presenters. It's in Laravel 4 but it should work for Laravel 5 too.
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!
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.
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');
}
Please or to participate in this conversation.