Download the PHP package forxer/gravatar without Composer
On this page you can find all versions of the php package forxer/gravatar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download forxer/gravatar
More information about forxer/gravatar
Files in forxer/gravatar
Package gravatar
Short Description A library providing easy gravatar integration.
License MIT
Homepage https://github.com/forxer/gravatar
Informations about the package gravatar
Gravatar
Gravatar is a small library intended to provide easy integration of... Gravatar :) It will help you generate the URL for Gravatar images and profiles in many ways.
To use it in a Laravel project, please look at: laravel-gravatar
Index
- Installation
- Requirements
- With Composer
- Without Composer
- Usage
- Use helpers
- Use the Gravatar base class
- Single Gravatar image/profile
- Multiples Gravatar images/profiles
- Instanciate the dedicated classes
- Mandatory parameter
- Optional parameters
- Gravatar image size
- Default Gravatar image
- Gravatar image max rating
- Gravatar image file-type extension
- Force to always use the default image
- Gravatar profile format
- License
Installation
Requirements
- PHP 8.2 or newer
If you want to use it with a version earlier than PHP 8.2, please use one of the previous versions that suits your needs: v4, v3, v2 or v1.
With Composer
The easiest way to install Gravatar is via Composer. Run the following command in the root of your project:
Without Composer
You should use composer, it's so convenient. However, if you really do not want, or can not, you can download the latest version and unpack the archive.
Then, you do what it takes to use it with your own autoloader. The examples below use the Composer autoloader.
Back to top ^
Usage
There are many ways to use this library:
- Use helpers fonctions
- Use the Gravatar base class with its
Gravatar::image()
andGravatar::profile()
methods - Instantiate the dedicated classes
Gravatar\Image()
andGravatar\Profile()
All of these ways return instances of Gravatar\Image
and Gravatar\Profile
that allow you to define specific settings/parameters as needed.
Whatever method you use, you could use the url()
method to retrieve it. Or display the URL directly because they implement the __toString()
method.
Use helpers
The easiest way to use this library is to use the helper functions.
Since version 4, in order to avoid conflicts with other packages, we no longer define the gravatar()
and gravatar_profile()
helpers. It's up to you to do this in your app with names that work for you and don't conflict with others. Here's how to do it; it's simple.
In a file dedicated to the helper functions, define the version 3 functions using the names that suit you.
If you do not have a helpers functions file, you can create one for example at the root of your project, which you name helpers.php
and which you fill in the composer.json
file so that it is autoloaded. For example :
This way you can use them like this:
Back to top ^
Use the Gravatar base class
But it is also possible to use the static methods of the base Gravatar class.
Single Gravatar image/profile
If you want to retrieve a single Gravatar image/profile URL you can use the main Gravatar class like this:
Back to top ^
Multiples Gravatar images/profiles
If you want to retrieve multiples Gravatar images/profiles URL you can also use the main Gravatar class with Gravatar::images()
and Gravatar::profiles()
methods.
Note the presence of the "s" character at the end of methods names.
The Gravatar::images()
and Gravatar::profiles()
methods return an array of instances of Gravatar\Image
and Gravatar\Profile
.
Back to top ^
Instanciate the dedicated classes
In fact, either gravatar()
and gravatar_profile()
helpers functions or Gravatar::image()
, Gravatar::images()
, Gravatar::profile()
and Gravatar::profiles()
static methods are just shortcuts for convenient use.
Behind these helpers functions and static methods, there are two classes : Gravatar\Image
and Gravatar\Profile
.
In some case, for some reason, you would use the library in another way. For exemple with a dependency injection container.
Back to top ^
Mandatory parameter
Obviously the email address is a mandatory parameter that can be entered in different ways.
These previous examples are also valid for the profile.
Back to top ^
Optional parameters
Gravatar image size
By default, images are presented at 80px by 80px if no size parameter is supplied. You may request a specific image size, which will be dynamically delivered from Gravatar. You may request images anywhere from 1px up to 2048px, however note that many users have lower resolution images, so requesting larger sizes may result in pixelation/low-quality images.
An avatar size should be an integer representing the size in pixels.
If you want to retrieve the currently set avatar size, you can use one of following methods:
Back to top ^
Default Gravatar image
What happens when an email address has no matching Gravatar image or when the gravatar specified exceeds your maximum allowed content rating?
By default, this:
If you'd prefer to use your own default image, then you can easily do so by supplying the URL to an image. In addition to allowing you to use your own image, Gravatar has a number of built in options which you can also use as defaults. Most of these work by taking the requested email hash and using it to generate a themed image that is unique to that email address. To use these options, just pass one of the following keywords:
- 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
- mp: (mystery-person) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
- identicon: a geometric pattern based on an email hash
- monsterid: a generated 'monster' with different colors, faces, etc
- wavatar: generated faces with differing features and backgrounds
- retro: awesome generated, 8-bit arcade-style pixelated faces
- robohash: a generated robot with different colors, faces, etc
- blank: a transparent PNG image
If you want to retrieve the currently set avatar default image, you can use one of following methods:
Back to top ^
Gravatar image max rating
Gravatar allows users to self-rate their images so that they can indicate if an image is appropriate for a certain audience. By default, only 'g' rated images are displayed unless you indicate that you would like to see higher ratings.
You may specify one of the following ratings to request images up to and including that rating:
- g: suitable for display on all websites with any audience type.
- pg: may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
- r: may contain such things as harsh profanity, intense violence, nudity, or hard drug use.
- x: may contain hardcore sexual imagery or extremely disturbing violence.
If you want to retrieve the currently set avatar max rating, you can use one of following methods:
Back to top ^
Gravatar image file-type extension
If you require a file-type extension (some places do) then you may also specify it.
You can specify one of the following extensions for the generated URL:
- 'jpg'
- 'jpeg'
- 'gif'
- 'png'
- 'webp'
If you want to retrieve the currently set avatar file-type extension, you can use one of following methods:
Back to top ^
Force to always use the default image
If for some reason you wanted to force the default image to always be load, you can do it:
To check to see if you are forcing default image, call the method forcingDefault()
of Gravatar\Image
,
which will return a boolean value regarding whether or not forcing default is enabled.
Back to top ^
Gravatar profile format
Gravatar profile data may be requested in different data formats for simpler programmatic access.
The following formats are supported:
- JSON ; use 'json' as argument
- XML ; use 'xml' as argument
- PHP ; use 'php' as argument
- VCF/vCard ; use 'vcf' as argument
- QR Code ; use 'qr' as argument
Back to top ^
License
This library is licensed under the MIT license; you can find a full copy of the license itself in the file /LICENSE