Working with User Avatars

All user accounts in concrete5 can have a profile picture associated with them. We call this picture a "User Avatar." By default, this picture is set via standard controls in the Dashboard:

Just click the user avatar image – upload one from your local machine, and your public profile, conversation messages and more will display your user avatar. It's easy to add this type of user avatar functionality to your own custom code.

Displaying User Avatars

Displaying user avatar's using custom PHP code is easy. First, retrieve a UserInfo object, and on that object, call the following:

Getting the path to an Avatar

Determine whether a user has an avatar

if ($user->hasAvatar()) {
print 'User has avatar.';
}

Updating User Avatars Programmatically

Setting or Changing the Image

concrete5 uses the Imagine image handling library for its image manipulation, and the image that you're going to use as the avatar needs to be loaded into it. First, load an uploaded image into Imagine:

$image = \Image::open($_FILES['avatar']['tmp_name']);

This creates the Image object.

Next, let's say you want to resize the avatar down to the standard width and height:

That will return a resized image resource. This we can pass to our UserInfo method:

$user->updateUserAvatar($image);

That's it! The avatar will be updated.

Removing the Avatar

It's easy to remove a user avatar from a UserInfo record. Just use the update() method written about earlier:

$user->update(array('uHasAvatar' => 0));

Advanced: Creating an Alternate Service for Avatar Delivery

In addition to improving some of the behind-the-scenes user APIs in concrete5, version 5.7.5.4 introduced the ability to provide a completely separate system of avatar delivery for users. Historically, avatars and user accounts in concrete5 have been tightly coupled: if you wanted to show a picture next to a user account, you had to use the built-in method of storing pictures against concrete5 accounts. Some minor modifications were introduced to allow for a Gravatar-based fallback – but things were still tightly coupled. With the changes introduced in concrete5 version 5.7.5.4, it's much easier to do the following with custom code:

In fact, this very documentation site employs a custom avatar service: when users login with their concrete5.org account, their avatars are displayed along with those users – even though a user never uploaded an avatar file to this particular website. Here's how it works:

That's it! Finally, you'll need to tell concrete5 to use a separate avatar service, rather than the built-in standard avatar service. You can do this from within a custom package's on_start() method or from within your site's application/bootstrap/app.php file: