Skip to content

Instantly share code, notes, and snippets.

@niladam
Last active September 24, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niladam/781719497af24a188af08bce47834279 to your computer and use it in GitHub Desktop.
Save niladam/781719497af24a188af08bce47834279 to your computer and use it in GitHub Desktop.
Laravel 8 use gravatar as default profile photo

This only applies to Laravel 8+.

The easiest way to replace ui-avatars.com with gravatar is to add this method and property to your User class.

If $profile_photo_size is not set, it defaults to Gravatar's default which is 80 x 80px.

<?php
namespace App\Models;
use Illuminate\Support\Str;
class User extends Authenticatable
{
// [...] Your other User methods/properties here
/**
* Default size in pixels for the gravatar image.
*
* Between 1 and 2048. Numbers only.
*
* Defaults to 80x80.
*
* @var string
*/
protected $profile_photo_size = '200';
/**
* Get the default profile photo URL if no profile photo has been uploaded.
*
* @return string
*/
protected function defaultProfilePhotoUrl()
{
$hashedEmail = md5(Str::lower($this->email));
$sizeParameters = $this->profile_photo_size ? '?s='.$this->profile_photo_size : '';
return 'https://www.gravatar.com/avatar/'.$hashedEmail.$sizeParameters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment