How to Display Google Profile Image in PHP Comments (OAuth Integration)
We will:
👉 use Google login
👉 get user profile image
👉 store it in database
👉 display it in comments
1️⃣ Get profile image from Google
When using Google OAuth, you receive user data like:
{
"id": "12345",
"name": "John Doe",
"email": "john@email.com",
"picture": "https://lh3.googleusercontent.com/..."
}
👉 The picture field is the profile image URL
2️⃣ Add profile_image column
Run this SQL:
ALTER TABLE users ADD profile_image VARCHAR(500);
3️⃣ Save image during login
In your Google login callback:
$profileImage = $googleUser['picture'];
$stmt = $conn->prepare("
INSERT INTO users (username, email, profile_image)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE profile_image=?
");
$stmt->bind_param("ssss",
$googleUser['name'],
$googleUser['email'],
$profileImage,
$profileImage
);
$stmt->execute();
👉 This keeps the profile image updated
4️⃣ Load profile image with comments
Update your query:
SELECT c.*, u.username, u.profile_image
FROM comments c
LEFT JOIN users u ON c.user_id = u.id
WHERE c.post_id = ?
ORDER BY c.id DESC
5️⃣ Display profile image
<div class="comment">
<img src="<?= $row['profile_image'] ?>" class="avatar">
<strong><?= $row['username'] ?></strong>
<p><?= htmlspecialchars($row['comment']) ?></p>
</div>
6️⃣ Style avatar
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
object-fit: cover;
}
🔥 Result
Each comment shows user profile image
Looks like real social platform
Better user experience
🔐 Security Tips
always escape comment text
validate image URL
fallback if image is missing
✨ Optional: Default image
$img = $row['profile_image'] ?: 'default.png';
✅ Summary
To show Google profile image:
get image from OAuth
store in DB
join with comments
display in UI
🚀 Next Step
You can extend this by:
upload custom profile image
user profile page
comment likes
Hope this helps 🚀
Login with Google