⬆️ ⬇️

Gravatar application on your site

Surely, many have heard about the Internet service Gravatar, I recently had to use it and therefore I decided to write a little instruction. How to use it on your website, the nuances associated with its use, I will show examples of the use of Gravatara. I also want to note that at most this article is a translation of the official PHP API for this service.



The principle of the service:



When I first heard about this service, I immediately imagined an incredibly voluminous API that turns up these same avatars, but in fact everything turned out differently. In order to get a user's avatar, we just need his email address and now almost all the work has been done. After we have learned the address we need to form a link which in the simplest form consists of two parts:

  1. Website address: www.gravatar.com/avatar where we can request avatars.
  2. MD5 Hash email address, I will tell you about it in detail below.


As a result, we should have the following link: www.gravatar.com/avatar/hash . That's all, by this link you can get a simple avatar 80x80, for the email address from which we received the MD5 hash. This link can be used as a picture, on any resource, also if a file extension is required, and in some cases it is, then add to the end of the hash ".jpg", after which the address of the avatar will look like this: www.gravatar.com/avatar / hash.jpg . All this is a theory, and then a little practice and examples:



MD5 Hash:



Everyone knows how to use the standard function md5 () in PHP, for those who forgot how it is made out or simply did not know, below is an example:



$hash = md5 ( $str );



')

We pass the string for hashing as a parameter, in response, the function will return us an MD5 hash, which we will use the avatar receipt in the future. But it’s not so simple, the fact is that the line breaks or upper case characters can creep into the string to be processed, as we end up with a hash that is not suitable for use. Therefore, before we form the hash of the email address, we need to process it as follows:



$email = " " ;

$email = trim ( $email ); // .

$email = strtolower ( $mail ); // .

$email = md5 ( $email ); // .





After the operation of this code, the hash can be used without fear that the result will be erroneous. Also below is an abbreviated entry of this code snippet:



$hash = md5 ( strtolower ( trim ( $email ) ) );





In this example, the $ email variable contains a raw email address, and after running in the $ has h variable, the hash will be ready to use.



Parameterization query:



By itself, this service is already extremely useful and unique, and most importantly easy to use. In addition, you can just get an avatar knowing only the email address, you can also specify additional parameters for it, all in the same request. Next, I will describe what parameters are available to request their values ​​and also give examples of their use.



Parameter:Description:Parameter name:Parameter values:
The sizeThis parameter sets the size of the image you receive.s = or size =Integer from 0 to 512
Default imageSpecifies the image that will be received if no image is associated with the specified e-mail address.d = or default =404

mm

identicon

monsterid

wavatar

retro
Default image forcedForces image to load by default.f = or forcedefault =y
Age ratingSpecifies the age rating for the image, if for the specified rating there is no image, then the image will be returned for a rating that is lower than the specified one or the default image.r = or rating =g - For use on any sites. jpg - May contain harsh, offensive words, violence scenes, erotic images. r - May contain obscene expressions, violent violence scenes, naked body, and drug propaganda. x - Content is only suitable for adults. categories.




All the above parameters are transmitted in the GET request , as a pair of value, for example, in order to get an avatar of 100x100 pixels you need to make a link of the following type: www.gravatar.com/avatar/hash?s=100 or www.gravatar.com/avatar/ hash? size = 100 . It is also possible to combine parameters with an ampersand (&), in this form the link will look like this: www.gravatar.com/avatar/hash?size=100?default=mm . The resulting link can be immediately used for the img tag.



By and large, this and everything, in the text above, is enough information to use the service in your site and in the plugins for a variety of CMS. You can get additional information from the official site , there you can also download various libraries, PHP and other programming languages, to work with this service.

Source: https://habr.com/ru/post/128877/



All Articles