Skip to content

Instantly share code, notes, and snippets.

@niladam
Last active Sep 24, 2020
Embed
What would you like to do?
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