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 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 teach 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 Name with  PHP

Now let’s start coding, to create a default profile picture from the name using php

You should also learn the following examples –

Create PHP Registration Form

Create PHP Login Form

Create a Short Name From First & Last Name

This PHP script defines a function, ProfilePicFromName, which generates a default profile picture label based on the first characters of the first and last words in a full name. The result is stored in the variable $shortName.

// 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;


Explanation –

  • The PHP script initializes a full name variable, $fullName, and calls the function ProfilePicFromName with this value.
  • The ProfilePicFromName function generates a default profile picture label based on the first characters of the first and last words in the full name.
  • The function uses explode to separate the full name into an array of words and extracts the first and last words.
  • It then obtains the first character of the first word and the first character of the last word.
  • The default profile picture label is created by concatenating these characters, converting them to uppercase, and returned as $defaultProfile.
  • The result is stored in the variable $shortName.

Display Profile with short name

This HTML code displays a profile section with a div for the profile image showing the generated short name using PHP, and a heading showing the full name.

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


Design Default Profile Picture

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

This CSS code defines styles for a profile container (.profile) with fixed dimensions, a centered layout, and a circular profile image (.profile-image) with a background color, font size, and color, using a large font size to display a short name inside the circle.

        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;