Default Profile Picture From First Name and Last Name in PHP

When a user signs up on the website, the default picture is created from the user name. This default picture is
displayed as a profile picture unless the user changes their own profile picture.

Generally, the same avatar picture is set as a default profile picture for all users. But If you want to display the default profile picture based on each user then you need to create the profile picture from the user’s name dynamically.

In this code snippet, I will learn you how to generate a default profile picture from the first name and last name in PHP. After that when you implement it the profile picture will be different for each user and relevant to their name

php profile picture from first name last name

Create Default Profile Picture From User Name after Registration using PHP

Create a Short Name From First Name and Last name in PHP

First, we have to split the full name into first name and last name and generate short characters from the user’s name.

  • First of all, Create a function ProfilePicFromName() with a parameter $fullName.
  • After that implement all the next steps within the created function
  • Convert Full Name into an array using explode() method.
  • Get the First Word from the full name using the current() method
  • Get the Last word from the full name using the end() method
  • Get the First character from the first word using substr()
  • Get the First character from the Last word using substr()
  • Make the short name from both characters
  • And then return the short name
// Full name of the user 
$fullName = 'Md Nurullah  khan'; 
$shortName = ProfilePicFromName($fullName);

function ProfilePicFromName($fullName) {
     $fullNameArr = explode(" ", $fullName);
     $firstWord = current($fullNameArr);
     $lastWord  = end($fullNameArr);
     $firstCharacter = substr($firstWord, 0, 1);
     $lastCharacter = substr($lastWord, 0, 1);
     $defaultProfile = strtoupper($firstCharacter.$lastCharacter);
     return $defaultProfile;



Display Default Profile Picture with Short Name in HTML

Now, Display Default Profile Picture with Short Name on HTML page

<div class="profile">
<div class="profile-image">
    <?php echo $shortName; ?>
<h4><?php echo $fullName; ?></h4>


Design Default Profile Picture using CSS

You can also use the following CSS code to design the default profile picture

        width: 140px;
        height: 140px;
        text-align: center;
 .profile-image {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: #dbdadf;
    font-size: 40px;
    color: #545353;
    line-height: 139px;



Leave a Comment