Download the PHP package forxer/laravel-gravatar without Composer
On this page you can find all versions of the php package forxer/laravel-gravatar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download forxer/laravel-gravatar
More information about forxer/laravel-gravatar
Files in forxer/laravel-gravatar
Package laravel-gravatar
Short Description A library providing easy gravatar integration in a Laravel project.
License MIT
Homepage https://github.com/forxer/laravel-gravatar
Informations about the package laravel-gravatar
Gravatar for Laravel
This package provides an easy Gravatar integration in a Laravel project.
This package is built on top of forxer/Gravatar. If you want to dig deeper, you can find additional information on its README file.
Index
- Requirements
- Installation
- Usage
- Retrieve instances
- Retrieve the Gravatar URL
- Show directly in your views
- 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
- Combine them
- Image presets
- Casts
Requirements
- PHP 8.2 or newer
- Laravel 10.0 or newer
If you want to use it with a version earlier than PHP 8.2 and/or a version earlier than Laravel 10, please use version 2 or version 1.
Installation
Install through composer:
Usage
There are three ways to use this library:
- With the
gravatar()
helper fonction - With the facade
Gravatar::create()
- Or by injecting the
LaravelGravatar\Gravatar
service
All of these ways return an instance of the LaravelGravatar\Gravatar
service. The Gravatar service has 3 main methods :
image()
which return an instance ofLaravelGravatar\Image
wich extendsGravatar\Image
from forxer/Gravataravatar()
which is an alias of the firstprofile()
which return an instance ofLaravelGravatar\Profile
wich extendsGravatar\Profile
from forxer/Gravatar
This instances of LaravelGravatar\Image
and LaravelGravatar\Profile
allow you to define specific settings/parameters as needed. So you can use them to build Gravatar images/profiles URL.
Whatever method you use, you could use the url()
method to retrieve it. Or display the URL directly because they implement the __toString()
method.
Retrieve instances
With the helper:
Or with the facade:
Or with the service injection:
Retrieve the Gravatar URL
Simply use the url()
method.
Show directly in your views
As classes implement the toString()
method you can use instances directly.
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
In all the examples below we will use the helper but it obviously works with the facade or the dependency injection of the service.
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.
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
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.
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'
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 LaravelGravatar\Image
,
which will return a boolean value regarding whether or not forcing default is enabled.
Back to top ^
Combine them
You can combine them seamlessly:
Back to top ^
Image presets
It is possible to define groups of defaults parameters, known as presets. This is helpful if you have standard settings that you use throughout your application. In the configuration file, you can define as many gravatar presets as you wish and a default preset to be used.
First, publish the config file of the package using artisan:
And define some presets in the configuration file in the 'presets' array. There are a few predefined presets for the example in the configuration file, but you are of course free to delete them and define ones that suit your needs. For example:
In the configuration file, for the values key name, as these are array keys and we follow naming conventions, you can use either:
'size'
or's'
'default_image'
or'd'
'max_rating'
or'r'
'extension'
or'e'
'force_default'
or'f'
If you wish you can default to one of these presets at the top of the configuration file.
Then, use it in your application with the second argument:
Or you can set it later after instantiation:
Back to top ^
Casts
Let's imagine that your user model has a column "gravatar" which represents the email to use. You can cast this attribute to directly obtain an instance of LaravelGravatar\Image
:
Thus it is easy to access the instance and manipulate it:
You can even define a preset name to be used when casting by appending it's name to the cast:
You can also cast to an instance of LaravelGravatar\Profile
:
Back to top ^