And why it doesn't work the way you do it?
Maybe this:
<img src="{{ url(Auth::user()->picture ??
'/uploads/default/user.png') }}" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
In laravel How can I default set the profile image for user if he left upload image. default image created like this: https://imgur.com/RLZveYY click this url For example: If user enter his name: John Doe => then profile image will be default selected "JD" in cilrcle format. This is my simple default profile image code:
<img src="<?php echo (Auth::user()->picture != NULL) ? url(Auth::user()->picture) : url("uploads/default/user.png"); ?>" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
So what should I do for this method? can anyone has idea...?
And why it doesn't work the way you do it?
Maybe this:
<img src="{{ url(Auth::user()->picture ??
'/uploads/default/user.png') }}" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
@nakov this also works. Now I want to decide if there is no image uploaded in user profile section. It would default generate the profile image from user First and last name character... So how can I achieve this..? Do you have any suggestion..? Should I have write any extra line of code...?
@neeraj1005 but if an image with those credentials doesn't exists on the server then it will show an empty image tag..
Otherwise just create an accessor method in the User model, for example:
public function getProfilePictureAttribute()
{
if ($this->picture)
{
return $this->picture;
}
return substr($this->first_name, 0, 1) . substr($this->last_name, 0, 1) . '.png';
}
Of course modify it for your needs, then just use auth()->user()->profile_picture
@nakov I test this code but it returns blank image Here is the output link https://imgur.com/gSwyETC
<img src="{{ auth()->user()->profile_picture }}" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
@neeraj1005 that's what I said above as well.. Do you have an image in a folder for each user? You might need to prepend the path to the folder as well, I said that I will give you an idea, but you should modify it for your usage.
@nakov No, I have not an image for each user. But I want to create a default image which take users First name letter and last name letter if user does not upload any images.
And if i uses this technique
<img src="{{ url(Auth::user()->picture ??
'/uploads/default/user.png') }}" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
this will upload an default image which is present in the folder
@neeraj1005 that one is not uploading it is using an image that already exists in your project.. so keep using that, with the user credentials it will not work unless an actual image exists.
@nakov can you tell me how this function will work! And how to call this function for default image?
public function getProfilePictureAttribute()
{
if ($this->picture)
{
return $this->picture;
}
return substr($this->name, 0, 1) . substr($this->lastname, 0, 1) . '.png';
}
This function will work, only if you have name and lastname as fields in your database, and now the user does not have a picture, but you want to use a default one.
So let's say I am registered on your site, my name is Trajche and lastname is Nakov.. This function will generate TN.png. For this to work you will have to have a TN.png file in your public folder, and then you can use it like this:
<img src="{{ url(Auth::user()->profile_picture ??
'/uploads/default/user.png') }}" class="img-profile rounded-circle" alt="User Image" width="24" height="24">
Just remember, it will not upload an image, the image MUST exists in the public directory.
If you want to upload MANY images for each credentials into a folder that is in the public directory, then you will prefix the folder here:
return '/yourfolder/' . substr($this->name, 0, 1) . substr($this->lastname, 0, 1) . '.png';
Now the image MUST be present in public/yourfolder/.
@nakov in my public folder no file is created... '/uploads/default/'
return '/uploads/default/' . substr($this->name, 0, 1) . substr($this->lastname, 0, 1) . '.png';
@neeraj1005 I hope that you will understand what I am saying.. this code:
return '/uploads/default/' . substr($this->name, 0, 1) . substr($this->lastname, 0, 1) . '.png';
Does not creates a file. You will have to PUT a file in there yourself somehow in order for that file to be used.. and it should be named exactly as I said above, for example TN.png
@nakov My question is this. I want dynamically create an avatar or image using Users' First Name or Last Name when users registered the first time. Like this image: https://imgur.com/EZyKrh4
@nakov These are very complicated, If you have simplest way then plz share... I did not find any suitable answer. and thanks for your response :)
@neeraj1005 ofcourse it is not easy to create an image using PHP. I've never done that, that's why I shared a google search.
I personally will add a random avatar image as a default instead of creating it using the user initials. That's what Laracasts does and many other sites too. So just put 2-3 placeholder images in your public folder and use one of them for the user if they don't specify one. And do that when they are registering, so the field is always filled with a path to an actual image.
Just curious what if two users have the same initials, couldn't you just have a default image like some sites have.
This might be what you are looking for: https://github.com/socialityio/laravel-default-profile-image
Please or to participate in this conversation.