• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

CodingStatus

- Learn Coding to Build Web Applications

  • HTML
  • JavaScript
  • jQuery
  • ReactJS
  • Ajax
  • Node.js
  • PHP
  • SQL
  • Interview Questions

Default Profile Picture From First Name and Last Name in PHP

November 28, 2022 By Md Nurullah Leave a Comment

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

Contents

  • Create Default Profile Picture From User Name after Registration using PHP
    • Create a Short Name From First Name and Last name in PHP
    • Display Default Profile Picture with Short Name in HTML
    • Design Default Profile Picture using CSS

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
<?php 
 
// 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; ?>
   
</div>
<h4><?php echo $fullName; ?></h4>
</div>

 

Design Default Profile Picture using CSS

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

<style>
    .profile{
        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;

    
}
    </style>

 

Related Posts:

  • PHP Interview Questions and Answers
  • Javascript Interview Questions and Answers for Freshers &…
  • Admin Panel Template in PHP with Free Source Code
  • Node.js MySQL CRUD Operation with Example

Filed Under: PHP

Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India. I'm a Software Engineer. I have been working in the Web Technology field for 3 years. Here, I blog about Web Development & Designing. Even I help developers to build the best Web Applications.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow us

  • facebook
  • twitter
  • linkedin

Latest Tutorials

  • HTML Heading Tags
  • Compress Image Size in PHP Without Losing Quality
  • How to Trigger a Click Event on Pressing Enter Key Using jQuery
  • HTML Elements
  • JavaScript Countdown Timer

Popular Tutorials

  • Compress Image Size in PHP Without Losing Quality
  • File Upload in PHP with Example & Code Demo
  • PHP Form Validation with example
  • PHP Registration Form
  • Default Profile Picture From First Name and Last Name in PHP

Categories

  • Ajax (10)
  • Django (5)
  • HTML (4)
  • Interview Questions (5)
  • JavaScript (18)
  • jQuery (11)
  • Laravel (2)
  • Node.js (22)
  • PHP (39)
  • ReactJS (35)
  • SQL (12)
  • Tips (7)
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved