Gravatar Macro

Version 24.1 by Vincent Massol on 2021/03/17 17:32

cogDisplays a Gravatar profile image
TypeXAR
Category
Developed by

Vincent Massol

Active Installs26
Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Installable with the Extension Manager

Description

Display a Gravatar profile image.

If you wish to set gravatar images for all users not having avatars defined yet, see this code snippet

Usage

{{gravatar email="email" size="size" default="default"/}}

Where:

ParameterOptionalValue
emailNoEmail value, e.g. [email protected]
sizeYesFrom 1px to 512px, e.g. "50"
defaultYesImage to use when no profile is found for the passed email. Valid values are: "mm", "identicon", "monsterid", "wavatar", "retro" or an URL-encoded URL to an image. See the Gravatar documenation for more details.

Example 1

Display a gravatar:

{{gravatar email="[email protected]" size="100" default="monsterid"/}}

Result

gravatar.png

Example 2

Use the gravatar macro in the user profile to display gravatar instead of avatars found in XWiki. You'll need to edit the XWiki.XWikiUserSheet page in wiki mode and replace the following:

  (% id="avatar" %)
 (((
     #if($request.xpage == 'edituser')
      {{html clean="false"}}
        #resizedUserAvatar($doc.fullName 180)
      {{/html}}
    #else
      ## By specifying the image width we enable server side resizing. The width value we use is greater than the
      ## available space because we don't want to loose too much of the image quality (we rely on the browser to fit the
      ## image in the available space).
      {{attachmentSelector classname="XWiki.XWikiUsers" object="$obj.number" property="avatar" #if ($isMyProfile) savemode="direct" #end defaultValue="[email protected]" width="180" alternateText="$xwiki.getUserName($doc.fullName, false)" buttontext="$services.localization.render('platform.core.profile.changePhoto')" displayImage="true" filter="png,jpg,gif"/}}
    #end
 )))

With:

  (% id="avatar" %)
  (((
     #set ($obj = $doc.getObject("XWiki.XWikiUsers"))
   {{gravatar email="$obj.getValue('email')" size="180" default="monsterid"/}}
  )))

However note that the avatar used in all other places (top level menu, comments, activity stream, etc) will still be using the XWiki avatar image. For this to work you'll also need to override the getUSerAvatarURL macro found in macros.vm. But even that won't solve everything. For example Livetable columns using _avatar will continue to get the avatar from XWiki.

Thus the only real solution is to have a script that sets the user avatar when a user is created, similarly to what is done here.

Source Code

For those interested, here's the macro source code:

{{groovy}}
def hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(xcontext.macro.params.email)
def url = "image:http://www.gravatar.com/avatar/${hash}"

def size = xcontext.macro.params.size
if (size) {
 url = "${url}?s=${size}"
}

def d = xcontext.macro.params.default
if (size && d) {
 url = "${url}&d=${d}"
} else if (size) {
 url = "${url}?d=${d}"
}

println url
{{/groovy}}

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager). Note that installing Extensions when being offline is currently not supported and you'd need to use some complex manual method.

You can also use the following manual method, which is useful if this extension cannot be installed with the Extension Manager or if you're using an old version of XWiki that doesn't have the Extension Manager:

  1. Log in the wiki with a user having Administration rights
  2. Go to the Administration page and select the Import category
  3. Follow the on-screen instructions to upload the downloaded XAR
  4. Click on the uploaded XAR and follow the instructions
  5. You'll also need to install all dependent Extensions that are not already installed in your wiki

Be careful to install this extension with a user having Programming Rights or it won't work

Release Notes

v1.0

Initial version

    

Get Connected