How to get Gravatar image from Email - Helper Class and Advanced implementation
Gravatar is one of the most beautiful systems to use images on any of the websites. Gravatar has given us the ability to use and email and get some customized email as we need. Let's learn about that today.
How to get Gravatar image from Email - Helper Class
First, we would make a helper class to get and set the gravatar image. Let's do it quickly.
<?php
/**
* Gravatar Helper Class
*
* Validate Gravatar Image and Set the URL
*/
class GravatarHelper
{
/**
* Validate the Email
*
* Check if the email has any gravatar image or not
*
* @param string $email Email of the User
*
* @return boolean true, if there is an image. false otherwise
*/
public static function validate_gravatar($email) {
$hash = md5($email);
$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
$headers = @get_headers($uri);
if (!preg_match("|200|", $headers[0])) {
$has_valid_avatar = FALSE;
} else {
$has_valid_avatar = TRUE;
}
return $has_valid_avatar;
}
/**
* Get Gravat Image URL
*
* Get the Gravatar Image From An Email address
*
* @param string $email User Email
* @param integer $size size of image
* @param string $d type of image if not gravatar image
*
* @return string gravatar image URL
*/
public static function gravatar_image( $email, $size=0, $d="" ) {
$hash = md5($email);
$image_url = 'https://www.gravatar.com/avatar/' . $hash. '?s='.$size.'&d='.$d;
return $image_url;
}
}
<?php
$email = 'manirujjamanakash@gmail.com';
$isEmailValidated = GravatarHelper::validate_email( $email );
if ( $isEmailValidated ) {
echo GravatarHelper::gravatar_image($email, 100); //
}
<?php if(GravatarHelper::validate_gravatar($email)): ?>
<img src="<?= GravatarHelper::gravatar_image($email, 100) ?>" alt="" />
<?php endif; ?>
Code Parts
validate_gravatar($email)
Pass an email to this function. If there is an email in gravatar.com for this email this function will return true , otherwise it'll return false
gravatar_image($email, $size="", $d="")
Pass an email, size(optional), d(optional) to this function. This will return an image URL if there is any image
Reference Article
About how to use Gravatar Email as Image - https://en.gravatar.com/site/implement/images/php
Good Class-Based Example - https://github.com/emberlabs/gravatarlib
PHP If-else-elseif and Switch-case
PHP String Functions - All necessary String functions in PHP to manage strings better.
Popular Tutorials
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development